-1

I'm trying to open a thread in Java, but for some reason I get an exception every time. my syntax :

Thread receiveThread2;
receiveThread2 = new Thread(() -> {
    try {
        receiveMessageNoLoop();
    }
    catch (IOException ex) {
        Logger.getLogger(ModelClient.class.getName()).log(Level.SEVERE, null, ex);
    }
});    
receiveThread2.start();

My function receiveMessageNOLoop() is in the same Java class:

public void receiveMessageNoLoop() throws IOException {
    String stringData;
    inFromServer.read(dataForLoop2, 0, 1024);
    takeCareOfJason(dataForLoop2);
}

If I do that without a thread, it works perfectly. problem is, I must use thread and for some reason it gives me nullPointerException.

Termininja
  • 6,620
  • 12
  • 48
  • 49
Zohar Argov
  • 143
  • 1
  • 1
  • 11
  • You say you get an exception every time. The answer probably is in the exception stack trace. The stack trace will tell you exactly which line of your program it was thrown from. – Solomon Slow Jun 02 '16 at 21:13

1 Answers1

0

Try setting recieveThread2 to null first.

    Thread recievThread2 = null;

But I would recommend using the executor service to start your threads

    ExecutorService executorService = Executors.newSingleThreadExecutor();
    executorService.execute(() -> {/*Your code*/});
tmj010
  • 34
  • 3