Reputation: 11
I'm trying to debug an issue with some code given to me by my professor. Ultimately I need to take the input from the text field in the LoginPanel
and reference this later on as the username
.
Here is my code:
import java.awt.*;
import java.awt.event.*;
public class MyCardFrame extends Frame{
LoginPanel lp;
ChatPanel cp;
public MyCardFrame(){
setLayout(new CardLayout());
setTitle("Chat");
setSize(500,500);
lp = new LoginPanel(this);
cp = new ChatPanel();
add(lp, "login");
add(cp, "chat");
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
setVisible(true);
}
public static void main(String[] args){
MyCardFrame mcf = new MyCardFrame();
}
}
class LoginPanel extends Panel implements ActionListener{
TextField tf;
MyCardFrame mcf;
public LoginPanel(MyCardFrame mcf){
this.mcf = mcf;
tf = new TextField(20);
tf.addActionListener(this);
add(tf);
}
public void actionPerformed(ActionEvent ae){
//using setName instead of setUserName *********
mcf.cp.setUserName(tf.getText()); //call setUserName of chatpanel which is a member of mycardframe
CardLayout cl = (CardLayout)(mcf.getLayout());
cl.next(mcf);
tf.setText("");
}
}
class ChatPanel extends Panel{
Label label1,label2;
String userName;
public ChatPanel(){
label1 = new Label("Chat Panel: ");
label2 = new Label("Name will go here");
add(label1);
add(label2);
}
public void setUserName(String userName){
this.userName = userName;
label2.setText(getUserName());
}
public String getUserName(){
return userName;
}
}
After this runs I get the following error:
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError:
finalproject.ChatPanel.setUserName(Ljava/lang/String;)V
This is coming from the line:
mcf.cp.setUserName(tf.getText());
From what I can tell I have correctly called setUserName
, and while the IDE provides no errors, on run it fails.
Also to clarify: Yes I know nobody uses AWT, but its required for this class and we are not permitted to use Swing.
Upvotes: 1
Views: 2458
Reputation: 86774
NoSuchMethodError
means something very specific. It says that when the code was compiled the class and method were successfully located, but when the code is being executed, even though the class was loaded, it did not contain the requested method.
This almost always means that the version of a jar file that was used during compilation is different from the one that is being provided at runtime. The version at runtime is missing that method, or the signature is different and a matching method can not be found.
From the Javadoc for NoSuchMethodError
:
Thrown if an application tries to call a specified method of a class (either static or instance), and that class no longer has a definition of that method.
Normally, this error is caught by the compiler; this error can only occur at run time if the definition of a class has incompatibly changed.
The solution is to double-check that you have the same jar file at both compile and runtime.
Upvotes: 1