I'm not sure why Eclipse is giving me this error:
The method
listen()
is undefined for the typeObject
What simple mistake am I making? Also, is my code the right way to write a main
method which instantiates an EchoServer0
object and calls its listen
method?
public class EchoServer0 {
public void listen() {
ServerSocket socket = null;
try{
socket = new ServerSocket(2013);
System.out.println("Opened server socket");
socket.setSoTimeout(2000);
socket.accept();
socket.close();
}
catch (SocketTimeoutException ste){
System.out.println("Timed out after " + 2000 + " ms");
}
catch (Exception e){
System.out.println(e.getClass().getName()+" at server: " + e.getMessage());
}
}
public static void main(String[] args) {
Object EchoServer0;
EchoServer0.listen();
}
}
The line
Object EchoServer0;
says that you are allocating an Object
named EchoServer0
. This has nothing to do with the class EchoServer0
. Furthermore, the object is not initialized, so EchoServer0
is null
. Classes and identifiers have separate namespaces. This will actually compile:
String String = "abc"; // My use of String String was deliberate.
Please keep to the Java naming standards: classes begin with a capital letter, identifiers begin with a small letter, constants and enum
s are all-capitals.
public final String ME = "Eric Jablow";
public final double GAMMA = 0.5772;
public enum Color { RED, ORANGE, YELLOW, GREEN, BLUE, INDIGO, VIOLET}
public COLOR background = Color.RED;
Try this.
public static void main(String[] args) {
EchoServer0 myServer;
myServer = new EchoServer0();
myServer.listen();
}
What you were trying to do was declaring a variable of type Object
, not creating anything for that variable to reference, then trying to call a method that didn't exist (in the class Object
) on an object that hadn't been created. It was never going to work.
Change your main to:
public static void main(String[] args) {
EchoServer echoServer = new EchoServer();
echoServer.listen();
}
When you declare Object EchoServer0;
you have a few mistakes.
new
.It should be like that
public static void main(String[] args) {
EchoServer0 e = new EchoServer0();
// TODO Auto-generated method stub
e.listen();
}
Your variable of type Object
truly doesn't have such a method, but the type EchoServer0
you define above certainly has.
Source: Stackoverflow.com