Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't Initialise Object Streams  RSS feed

 
Bren Reggy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

This is probably a simple mistake/overlook on my part but I can't seem to figure out where I am going wrong. I am working on a simple Chat Server/MultiClient program and use ObjectInput/Output Streams to send Strings across (specified as a requirement )

The Server instantiates and I can get multiple clients to connect, but when I perform an ActionEvent on a TextField to send the data, I get AWT NullPointerExceptions. So Upon trying to find these missing exceptions I think that the streams aren't initialising.



I wrote DEBUG messages to show which parts are displaying and which aren't, (displayMessage is simple method appending the TextArea).

Any help appreciated, I need to get this finished asap!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bren Reggy wrote:I get AWT NullPointerExceptions.


It would help if you'd indicate which lines are causing the NPEs.

I need to get this finished asap!


Please EaseUp(←click).

Because of the asynchronous nature of Swing, I wouldn't use what is or is not showing up there as a guide to what is and is not getting executed. Use System.out.println() calls instead. You might even want to call System.out.flush() after every call, although I think it might be an atuo-flushed stream anyway.
 
Bren Reggy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I get NPE's at line 82, 78 and 58 respectively. My gut tells me that it is initialising an actionperformed method that uses an outputStream before it is actually created, so i tried putting the streams before the buildGUI() method but no luck either

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bren Reggy wrote:I get NPE's at line 82, 78 and 58 respectively.

None of those three lines are in the code that you originally posted. And I don't think they match up exactly to the second version of the code because there's no way lines 78 or 82 can possibly throw a NullPointer directly. So which actual lines of the code throw the exception? And which reference is null? That should be easy to find out - just write the references used in that line to System.out just before the exception happens,
 
Bren Reggy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, in the above code snippet, I meant lines 80, 76 and 58. I was trying to isolate the problem code in the first example rather than post more than was necessary, hence why I was little confused on precise line numbers (didnt include a package and comment lines).

Anyway, my error code comes up as;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pcp2.ChatClient.sendData(ChatClient.java:82)
//corresponding to line 80 above
at pcp2.ChatClient.access$0(ChatClient.java:78)
// corresponding to line 76 above
at pcp2.ChatClient$1.actionPerformed(ChatClient.java:60)
//corresponding to line 58 above
at javax.swing.JTextField.fireActionPerformed(Unknown Source)
at javax.swing.JTextField.postActionEvent(Unknown Source)
at javax.swing.JTextField$NotifyAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(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.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(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$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$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)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bren Reggy wrote:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at pcp2.ChatClient.sendData(ChatClient.java:82)
//corresponding to line 80 above


That's this line:


so outputToServer must be null, since that's the only thing we're dereferencing on that line. If you look at your constructor, your building the GUI, which uses that OutputStream, before you're setting the OutputStream variable.
 
Bren Reggy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bren Reggy wrote:My gut tells me that it is initialising an actionperformed method that uses an outputStream before it is actually created


I had thought that myself but I tried to move the lines around so that the streams were created before the GUI was built but to no avail. After a lot of tooing and froing I moved a ton of stuff around and into self-contained methods, which worked.

My problem was that I wasn't passing information around the program properly and is one of the reasons why the streams, even if initialised before the call to buildGUI(), still were throwing exceptions.

Thanks for the help
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!