Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: **Constructor in class cannot be applied to given types;...

  1. #1
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default **Constructor in class cannot be applied to given types;...

    I kind of understand why I am getting this error but have no idea how to solve it.

    I think the constructor 'Chatter' requires a String, and the method add does not supply that. I tried adding String n to adds parameters but it just came up with more errors, could someone point me in the right direction?

    I also get an error saying the variable name might not have been initialised, and it points to the curly bracket at the end of the second chatter method below.

    Here are the relevant offending pieces of code.



     
       IN CLASS CHATTERLIST
     
       private void add(Chatter c)
       {
    		theList = new Chatter(c, theList);
       }
     
      IN CLASS CHATTER
     
       public Chatter(String n)
       {
    		name = n;
       }
     
       Chatter(Chatter c, Chatter next)
       {
    		this.next = next;
    		theChatter = c;
       }

    thanks!


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: **Constructor in class cannot be applied to given types;...

    Why are you even calling the Chatter constructor in the add method since a valid Chatter object is already being passed into the method via its parameter? What type of variable is theList? If it's a list of some sort, then you are likely barking up the wrong tree and instead should consider calling methods of theList so that you can add the Chatter object to whatever list theList represents.

    I think that you need to show more code to allow us to come close to understanding what may be happening here. Also if you get compiler errors, please post the entire error so we can see them for ourselves.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    bassie (December 2nd, 2012)

  4. #3
    Junior Member bassie's Avatar
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: **Constructor in class cannot be applied to given types;...

    Hi curmudgeon thanks for your response!

    The problem of the class not being able to be applied to given types seems to have been resolved now, but I am still getting the following:

    error: variable name might not have been initialized
    }
    ^

    here is most of the code from the two classes, with an indicator to where the error is pointing:

     
    public class ChatterList 
    {
     
       /**
        * Prefix for broadcast messages that indicate a Chatter has joined
        * the chatroom.
        */
       private static final String JOIN_PREFIX = "0";
     
       /**
        * Prefix for broadcast messages that indicate a Chatter has left
        * the chatroom.
        */
     
       private static final String LEAVE_PREFIX = "1";
     
       /**
        * The list of  Chatters in the chatroom.
        */
     
       private Chatter theList = null;
     
       /**
        * Creates a new ChatterList instance.
        */
       public ChatterList()
       {
       }
     
       /**
        * Add a Chatter to the list.
        */
     
       private void add(Chatter c)
       {
    		theList = new Chatter(c, theList);
       }
     
       /**
        * Send a message to all Chatters in the chatroom.
        */
     
       public void broadcast(String msg)
       {
          // variable for traversing the linked list
          Chatter tmp = theList;
     
          /*
           * traverse the list
           */
          while (tmp != null) // exit loop at the end of the list
          {
             // send the message
             tmp.theChatter.sendToUser(msg);
             // and move on to the next chatter in the list
             tmp = tmp.next;
          }
       }
     
       /**
        * Add a Chatter to the chatroom, and inform all other Chatters.
        * This implements the operation "connect" in the
        * <a href="chatroom.maude">ChatterList specification</a>.
        * The Chatter's name is broadcast to all other {@link Chatter Chatters}
        * in the chatroom, preceded by {@link #JOIN_PREFIX the appropriate prefix}.
        *
        * @param c the Chatter joining the chatroom
        */
       public void connect(Chatter c)
       {
          // inform all other chatters
          broadcast(JOIN_PREFIX + c.getName());
          // and add the new Chatter to the list
          add(c);
       }
     
    }

     
     
    public class Chatter 
    {
     
       /**
        * The name of the Chatter.
        * This should be the ID chosen by the remote user on joining
        * the chatroom.
        * This value is set in the constructor.
        *
        */
       private final String name;
     
       /**
        * Creates a new <code>Chatter</code> instance with a given name.
        */
     
       public Chatter(String n)
       {
    		name = n;
       }
     
       /**
        * Return the {@link #name name} of the Chatter.
        */
     
       public String getName()
       {
          return name;
       }
     
       /**
        * Send a message across the network to the remote user.
        *
        * @param msg the msg to be sent
        */
       public void sendToUser(String msg)
       {
       }
        Chatter theChatter;
       /**
        * Pointer to the tail of the list.
        * Default scope modifier, so that this field can be efficiently
        * accessed in class ChatterList.
        *
        */
       Chatter next;
       /**
        * Creates a new <code>ChatterNode</code> instance.
        *
        * @param c the Chatter at this node
        * @param next the tail of this list
        */
    	   public Chatter(Chatter c, Chatter next)
       {
    		this.next = next;
    		theChatter = c;
       } //<<<<<<<<<<<<<<this is where the error is occuring
    }

    Any help would be much appreciated, and thanks again.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: **Constructor in class cannot be applied to given types;...

    When posting errors, please copy the full text of the error message. It has valuable information about what the problem is. The one line you posted has left off important data.
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. add a default constructor in abstract class
    By Kifli in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 8th, 2012, 12:42 AM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  3. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  4. [SOLVED] Class constructor default values
    By srs in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2010, 09:51 PM
  5. Construct a class that implement ActionListener with no constructor
    By striko_514 in forum Java Theory & Questions
    Replies: 1
    Last Post: July 5th, 2010, 03:15 PM

Tags for this Thread