Results 1 to 4 of 4
- 12-07-2012, 09:36 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
NullPointerException but i cant figure out why
I am making a client/server multithreaded chat. I have this working from the terminal with multiple clients etc. I am now trying to get it working with a GUI, the code is pretty much identical with obvious additions for displaying and reading text from the GUI components. Currently i can get my clients to connect using the GUI, i can see this from my server console. I then get a null pointer exception, that i have posted below. I have tried changing how and where i assign different values but nothing has helped the situation. Below i have just supplied my thread and socket code as i don't believe the GUI code is necessary for this particular problem.
This is the error
Exception in thread "Thread-1" java.lang.NullPointerException
at SimpleChat.run(SimpleChat.java:133)
at java.lang.Thread.run(Thread.java:722)
And here is the code.
Line 133 in SimpleChat refers to this line in the run method
Java Code:serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Java Code:public void ConnectToServer(String port) { String p = port; int portNumber = Integer.parseInt(p.replaceAll(",", "")); try { socket = new Socket("localhost",portNumber); in = new Scanner(System.in); out = new PrintWriter(socket.getOutputStream()); } catch(IOException e) { e.printStackTrace(); } new Thread(new SimpleChat()).start(); } public void sendToServer() { String line = message.getText(); out.println(line); out.flush(); } public void run() { try { serverIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); while(true) { String response = ""; response = serverIn.readLine(); conversation.setText(response); } } catch(IOException e) { e.printStackTrace(); } }
-
Re: NullPointerException but i cant figure out why
The socket variable looks to be null. I assume that the current code is part of the SimpleChat class, and if so then the reason for the problem is obvious. You create a valid socket object in the current SimpleChat instance, but then try to use the socket variable in the new SimpleChat object, one that never had its socket variable initialized. The solution is not to use SimpleChat as your Runnable but instead consider using an anonymous inner Runnable. Either that or create a new Runnable class and pass the socket reference into it via its constructor.
- 12-08-2012, 12:52 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: NullPointerException but i cant figure out why
Perfect thank you. I was confused why it was being set to null after i had initialised it. But when you said i am making a new SimpleChat object then i understood the problem. I have made an inner class and it is working now. Only a slight issue that the new messages overwrite each other in the conversation panel so only the latest message from either client is displayed. Should be an easy fix though.
-
Re: NullPointerException but i cant figure out why
Good deal, glad you've got the major error fixed.
Similar Threads
-
can't figure it out myself
By Doyle Raymond in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-18-2011, 04:34 PM -
NullPointerException error when running the program, I cant figure out why
By hoosierfan24 in forum New To JavaReplies: 4Last Post: 10-26-2011, 03:35 AM -
can someone help me to figure this out >.<
By kissmeeq in forum New To JavaReplies: 2Last Post: 08-01-2011, 04:19 PM -
Cant figure this out
By Shimless12 in forum New To JavaReplies: 1Last Post: 07-10-2011, 10:48 PM -
I can't figure this out
By silvia in forum New To JavaReplies: 3Last Post: 07-20-2007, 05:38 AM
Bookmarks