xianate
xianate

Reputation: 17

java.lang.NullPointerException PrintWriter

I am getting a NullPointerException when I run my code. I have narrowed down the problem to line 38: when it is commented out the program doesn't give any errors. I just have "test" here for now.

out.println("test")

The run method is immediately started with the following two lines in another class

Client test = new Client();
test.start();

And when a button is pressed the following code is executed which runs the sendToServer method

Client test = new Client();
test.sendToServer(cipherText)

Below is the full code for my Client class.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client extends Thread{

    String line = "";
    String cipherText = "";

    BufferedReader in;
    PrintWriter out;

    public void run(){
            String serverAddress = "00.000.000.000";
        try{
            Socket socket = new Socket(serverAddress, 8888);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
        }
        catch(IOException e){
            e.printStackTrace();
        }
        while (true) {
            try{
                line = in.readLine();
            }
            catch(IOException e){
                e.printStackTrace();
            }
            System.out.println(line);
        } 
    }

    public void sendToServer(String in) {
        out.println("test");
    }

}

Thanks for looking, I am still learning java so excuse any obvious mistakes.

Upvotes: 0

Views: 2437

Answers (3)

justAbit
justAbit

Reputation: 4256

That is because you have used PrintWriter out; as your class member but you have not initialized it. By default, all un-initialized member objects are initialized with null. If sendToServer() is called first, out will be null and you will get NullPointerException.

EDIT

Problem is that you are trying to invoke run() and sendToServer() methods on two separate instances of Client.

Client test = new Client(); // First instance
test.start();               // which calls run() and then initializes out variable.

Then a second instance is created from:

Client test = new Client();    // Second instance
test.sendToServer(cipherText); // since out is not initialized for this instance, you are getting NPE

I suppose you are using Client object in a multi-threaded environment, in that case I would suggest you to make sure that both threads are using same instance of Client object. Also if Client instance is being shared, you might want to make access to out variable synchronized and also make sure that Client's run method is executed first before button is pressed (which in turn calls sendToServer()).

Upvotes: 0

Art Solano
Art Solano

Reputation: 99

Your function assumes out Printwriter will be initialized, but if there is an exception in the socket initialization, it will never by initialized.  Two things I would do...

1. Initialize out to null at the top of the run method.

2. protect your sendToServer code...

public void sendToServer(String in) throws NullPointerException
{
    if (out == null)
    {
          throw new NullPointerException("Out is null.");
    }
    out.println("test");
}

Upvotes: 0

Wakachopo
Wakachopo

Reputation: 149

You have in the beggining of the class:

PrintWriter out;

if you don't call run() [is where you initialize it] before sendToServer(), "out" would never be initialized and will be null.

Upvotes: 1

Related Questions