0

I am getting the following exception:

java.lang.NullPointerException at
 gr.test.asterisk.AsteriskServerSocket.run(AsteriskServerSocket.java:72)

Here's my code:

public class AsteriskServerSocket  extends Thread{

    private Socket server = null;
    private ServerSocket serverSocket = null;
    private String serverPortString = null;

    public AsteriskServerSocket(){
         serverPortString = "50050";

        try {
            serverSocket = new ServerSocket(Integer.parseInt(serverPortString));
            serverSocket.setSoTimeout(1000000000);
        } catch (SocketTimeoutException s) {
            WriteExceptions.writeToFile(s);
        } catch (IOException e) {
            WriteExceptions.writeToFile(e);
        }
    }

    public void run() {
        while (true) {
            try {
                server = serverSocket.accept();<---- This is the line which I get the exception
                ......
            }
            } catch (SocketTimeoutException s) {
                System.err.println("Σφάλμα στην Server run. Socket timed out. \n" + s);
                break;
            } catch (IOException e) {
                System.err.println("Σφάλμα στην Server run.\n" + e);
                break;
            }
        }
    }
}

And AsteriskServerSocket, I call it in the Main Class:

public static void main(String[] args) {
    AsteriskServerSocket aSS = new AsteriskServerSocket();
}

My Operating System is CentOS release 5.10 (Final) and my JDK version is 1.8.0_45. I don't know why this exception is thrown. Am I missing something here?

My exceptions in the constructor is:

My exception in the constructor is 0 :java.net.BindException: Address already in use at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382) at java.net.ServerSocket.bind(ServerSocket.java:375) at java.net.ServerSocket.(ServerSocket.java:237) at java.net.ServerSocket.(ServerSocket.java:128)

2
  • Where are you starting the thread from? Commented May 13, 2015 at 17:25
  • if you want very large timeout, just give a 0. A timeout of zero is interpreted as an infinite timeout. docs.oracle.com/javase/7/docs/api/java/net/… Commented May 13, 2015 at 17:32

1 Answer 1

1

You are ignoring exceptions in the constructor and writing them to the logs, therefore serverSocket is null. Check the logs and/or rethrow those exceptions.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your advise! I will follow it and came back with the results.
My exception in the constructor is 0 :java.net.BindException: Address already in use at java.net.PlainSocketImpl.socketBind(Native Method) at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:382) at java.net.ServerSocket.bind(ServerSocket.java:375) at java.net.ServerSocket.<init>(ServerSocket.java:237) at java.net.ServerSocket.<init>(ServerSocket.java:128)
Something else is already using that port. Either kill the other application or use a different port. Two applications cannot use a single port.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.