Reputation: 4702
EDIT: The problem I were having was that I were using the wrong socket. It's now solved.
Hi,
I am working on a little chat client in java which include socket programming. However I've got a little problem starting up my connection because I get a NullPointerException and I can't understand why!
This is the code making the troubles:
try {
sock = new Socket(host, port);
Connection DaUberConnection = new Connection(sock);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The variables used for creating the socket is declared at at class level and is fully working as intended (I think). Host and port is set.
Socket sock;
String host;
int port;
I understand that it has something to do with me throwing in a null in the wrong place somewhere, but I can't really see how this is hanging all together.
Sorry for my bad english or nubieness in java :) It would be gold if anyone could explain why this is happening!
Full error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at clientpackage.Connection.<init>(Connection.java:24)
at clientpackage.Client$1.actionPerformed(Client.java:91)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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)
Upvotes: 2
Views: 1589
Reputation: 718798
I think that the problem is that you've not built and deployed the same code that is in your question.
You say that the exception occurs at this line:
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
and that means that sock
MUST be null.
But that is called from here:
sock = new Socket(host, port); // 1
Connection DaUberConnection = new Connection(sock); // 2
and that means that sock
CANNOT be null ... unless something else is going on.
If the code that you are running really is as you have reported, the only possible explanation of those symptoms is if some other thread is assigning null
to sock
in the tiny window between the two statements (1
and 2
). Even assuming some other thread can update sock
, this event will only occur "once in a blue moon".
Go back to your development environment, save all files you are editing, clean out all of the ".class" files, a full build, and run your application over again.
I think I see your problem ... s
versus sock
.
This is a good illustration of why it is BAD PRACTICE for a class to expose its state variables to subclasses. The sock
instance variable should be private
and the subclass should use a getter to access it and should initialize it via constructor chaining.
Get some sleep, then tomorrow start by fixing this design flaw.
Upvotes: 1
Reputation: 40391
Separate the line 24.
Use
if (sock==null) System.out.println ("Sock is null");
else {
is = sock.getInputStream();
if (is==null) System.out.println ("inputStream is null");
else isr = InputStreamReader(is);
}
and see which one is null. What is this line for?
Connection DaUberConnection = new Connection(sock);
Upvotes: 0