**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.
Code Java:
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!
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.
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:
Code java:
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);
}
}
Code java:
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.
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:
Code :
TestSorts.java:138: cannot find symbol
symbol : variable var
location: class TestSorts
var = 2;
^