If the variable: name has a null value, the code should test its value before trying to use it where it will cause a NPE.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
If the variable: name has a null value, the code should test its value before trying to use it where it will cause a NPE.
If you don't understand my answer, don't ignore it, ask a question.
ManInTheMiddle (May 16th, 2013)
my private String name = null. I think this is causing it.
I'v noticed that "name" should be reading in data just before the NPE error ( name = in.readInput), so, I think my client code needs to be edited as the
doesn't seem to be receiving anything.in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
If you do not know which variable is null when line 36 is executed, add a println just before line 36 that prints out the values of all the variables used in line 36.
If you don't understand my answer, don't ignore it, ask a question.
ManInTheMiddle (May 16th, 2013)
when I print out name it prints data but the ArrayList "nameList" is null, so, I think that's causing it.
I'v tried a few other things and the same error, it doesn't want to let me add data to the ArrayList nameList..
It needs to be assigned a value so it is not null.the ArrayList "nameList" is null,
If you don't understand my answer, don't ignore it, ask a question.
ManInTheMiddle (May 16th, 2013)
That should be better.
If you don't understand my answer, don't ignore it, ask a question.
ManInTheMiddle (May 16th, 2013)
Cheers, rooky mistake. Much appreciated..
hi,
I have this completed and working now except for an error when a client disconnects..
import java.net.*; import java.io.*; import java.util.*; class ServerThread extends Thread { private Socket socket = null; private String name = null; private String message = null; private ArrayList <ServerThread> clientList = new ArrayList<>(); private ArrayList <String> nameList = new ArrayList<>();; private PrintWriter out = null; private BufferedReader in = null; ServerThread(Socket socket, ArrayList <ServerThread> clientList) { super("ServerThread"); this.socket = socket; this.clientList = clientList; } public void sendToAll(String message) { for(ServerThread c: clientList) { c.out.println(message); } } public void run() { try { out = new PrintWriter(socket.getOutputStream(), true); // be able to echo back message to client in = new BufferedReader (new InputStreamReader(socket.getInputStream())); in.readLine(); name = in.readLine(); synchronized(this){ sendToAll( name + " has joined the chatroom ....\n"); } if(!nameList.contains(name)) { nameList.add(name); } while((message = in.readLine()) != null) synchronized(this) { sendToAll(name+" says: "+ message); } synchronized(this){ clientList.remove(currentThread()); } System.out.println("Clients :" + clientList.size() + "\n"); out.close(); in.close(); socket.close(); } catch(IOException e) { e.printStackTrace(); } } } // The server public class ChatServer { public static final int PORT = 7777; public ArrayList <ServerThread> clientList = new ArrayList<>(); public static void main(String[] args) throws IOException { new ChatServer().run(); } public void run() throws IOException{ ServerSocket serverSocket = new ServerSocket(PORT); // wait for request from PORT System.out.println("Server is listening for connections..."); while(true){ try{ Socket socket = serverSocket.accept(); ServerThread client = new ServerThread(socket,clientList); //add client to clientList and start the thread clientList.add(client); System.out.println("Clients :" + clientList.size() + "\n"); client.start(); } catch (IOException e) { e.getMessage(); } serverSocket.close(); } } }
here's the error messages
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream. java:189)
at java.net.SocketInputStream.read(SocketInputStream. java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.j ava:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.ja va:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 77)
at java.io.InputStreamReader.read(InputStreamReader.j ava:184)
at java.io.BufferedReader.fill(BufferedReader.java:15 4)
at java.io.BufferedReader.readLine(BufferedReader.jav a:317)
at java.io.BufferedReader.readLine(BufferedReader.jav a:382)
at ServerThread.run(ChatServer.java:39)
ManInTheMiddle (May 16th, 2013)
Think of it like a phone call. The phone cord got cut (breaking the connection) and the server lost communication to the client without hearing the client say goodbye. The server is trying to talk/listen to whats not there. This is not what is expected to happen, the server expects a polite goodbye before they disconnect.
An exception of this type is in place to cause an alert to the fact that it has happened so your program can respond correctly.
If you were sending routing information for some money for someone, and the connection was broken, it may be very important to reconnect and attempt to send again.
On the other hand if it was the last line of text in a chat room, it would not be important to reconnect and send. (Some chat programs will send the message the next time the user connects)
What ever you decide to do, the exception being thrown is your indication it is time to take care of the problem.
One solution would be to just ignore it. You are not required to actually do anything about it when it happens, but the syntax requires you to specify you intend to do nothing.
That is as simple as an empty catch block.
My suggestion is to read about this exception to understand why it has happened, and decide what you think your program should do when this triggering event takes place.
(that goes for all exceptions)
ManInTheMiddle (May 16th, 2013)
Cheers thanks for that..
Got it working
Greatly appreciated...