Results 1 to 4 of 4
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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();
            }
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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.

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default 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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. can't figure it out myself
    By Doyle Raymond in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-18-2011, 03:34 PM
  2. Replies: 4
    Last Post: 10-26-2011, 02:35 AM
  3. can someone help me to figure this out >.<
    By kissmeeq in forum New To Java
    Replies: 2
    Last Post: 08-01-2011, 03:19 PM
  4. Cant figure this out
    By Shimless12 in forum New To Java
    Replies: 1
    Last Post: 07-10-2011, 09:48 PM
  5. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •