2

I am using an ObjectInputStream and ObjectOutputStream for my client to talk to a server over TCP. The client is not able to receive a response back from the server... when I step through in debug mode the response goes through fine. However, if I run the program without a break point it will throw a NullPointerException exception.

Initialization:

ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Socket socket = null;

socket = new Socket(server, port);
oos = new ObjectOutputStream(socket.getOutputStream());
oos.flush();
ois = new ObjectInputStream(socket.getInputStream());

Code that breaks:

          try
            {
                oos.writeObject(request);   
                serverResponse = (Message) ois.readObject();
                output.append(serverResponse.data + "\n");
            } 
            catch(Exception ex) 
            {
                System.err.println("Error adding car to server: " + ex.getMessage());
                return;
            }

The code above is throwing the NullPointerException. If I use a break point and step through, I get a server response just fine. I have confirmed that in every instance the server is reading the

Any help would be greatly appreciated!

EDIT STACK TRACE AT EX:

java.lang.NullPointerException
    at CarInventoryClient.actionPerformed(CarInventoryClient.java:251)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
12
  • you mentioned ex is turning out to be NULL. ... are you sure that is what you meant (ex is the exception!!)? Or did you mean serverResponse is turning out to be NULL. ? Commented Sep 18, 2011 at 20:05
  • I don't believe it's possible for ex to be null. I'm guessing you mean your println() prints "null", and that would be becuase there's no message. Commented Sep 18, 2011 at 20:11
  • I've added the stack trace to my original post. I meant that the message was null. it is throwing a NullPointerException... what does this mean?? I commented out the output.append line... just incase the response data was null... that did not fix the null pointer. Commented Sep 18, 2011 at 20:16
  • 1
    what is line 251 in CarInventoryClient.java ? Commented Sep 18, 2011 at 20:16
  • 1
    @DaveC, this is probably obvious... it seems like whatever event (in the broad sense) that's triggering initialization isn't always happening before the button is pressed. Or, less likely, something is nulling it out. You haven't shown where/why/when the init is called. I'm going to hazard a guess that it's in the static init of another class that isn't necessarily loaded yet. Consider replacing "ois" with "getOis()" which will init it if it's not already done (or some other singleton-like mechanism). Commented Sep 18, 2011 at 23:34

4 Answers 4

1

I suggest you flush the output stream before attempting to read the input stream.

The variable ex shouldn't be null. Can you give us the exact line the exception occurs on.

You should print the whole message. I suspect not printing the type and line the exception is occurring is not helping. If you have an exception with no message getMessage() will return null

try

 ex.printStackTrace();
Sign up to request clarification or add additional context in comments.

3 Comments

I've added the stack trace to my original post. I meant that the message was null. it is throwing a NullPointerException... what does this mean???
Can you tell us which line is 251?
This means ois is null i.e. not set. Is it possible ois is set in a different thread?
1

I think the output of this line is probably confusing you:

System.err.println("Error adding car to server: " + ex.getMessage());

An exception does not require a message so it is perfectly normal (as well as a bit inconvenient) that this prints:

Error adding car to server: null

By only printing the exception message you are missing out on logging crucial information such as the exception type as well as the exceptions stack trace. You are better off calling ex.printStackTrace(). Which prints the exception class, message and the stack trace of the exception.

Comments

1

(An elaboration of a comment above, for didactic reasons. OP has confirmed.)

If you get a stack trace that begins with a given line, it means the throw happened on that very line - not within a method called from that line. So looking at the line in question:

> java.lang.NullPointerException
>     at CarInventoryClient.actionPerformed(CarInventoryClient.java:251)

serverResponse = (Message) ois.readObject();

The only way this very line could throw an NPE is if ois is null.

Comments

1

This was a race condition.

ois = new ObjectInputStream(socket.getInputStream());

The above line was causing the thread to block in my class's constructor. Java's documentation states that it will block until input headers are received. The action handler was sending then receiving a packet from the server - this woke the constructor code back up and finished the initialization.

When there was a thread sleep, or the debugger was attached, the constructor code would complete before the data was received. However, in real time execution, readObject was being called before the unblocked initialization could finish. This is why ois was being seen as null.

Comments

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.