Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7563587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:42:09+00:00 2026-05-30T13:42:09+00:00

Error below are fixed by removing the declaration but another has appeared that did

  • 0

Error below are fixed by removing the declaration but another has appeared that did not previously exist.

Receiving two errors, any insight would be great, thanks.

Exception in thread “main” java.lang.NullPointerException
at ChatClient.(ChatClient.java:27)
at ChatClient.main(ChatClient.java:59)

From the following ChatClient:

import java.net.*;
import java.io.*;

public class ChatClient
{  private Socket socket              = null;
   private BufferedReader  console   = null;
   private BufferedReader  streamIn   = null;
   private DataOutputStream streamOut = null;

   public ChatClient(String serverName, int serverPort, String userName)
   {  System.out.println("Establishing connection. Please wait...");
      try
      {  socket = new Socket(serverName, serverPort);
         System.out.println("Connected: " + socket);
         System.out.println("CTRL+C or type .bye to quit");
         start();
      }
      catch(UnknownHostException uhe)
      {  System.out.println("Host unknown: " + uhe.getMessage());
      }
      catch(IOException ioe)
      {  System.out.println("Unexpected exception: " + ioe.getMessage());
      }
      String line = "";
      while (!line.equals(".bye"))
      {  try
         {  line = console.readLine();
            streamOut.writeBytes(line + '\n'); //Send console data to server socket
            String reply = streamIn.readLine(); //Recieve confirmation msg from server
            System.out.println( reply ); //Print the msg
            streamOut.flush();
         }
         catch(IOException ioe)
         {  System.out.println("Sending error: " + ioe.getMessage());
         }
      }
   }
   public void start() throws IOException
   {  console = new BufferedReader(new InputStreamReader(System.in)); //Changed console to BufferedReader
      streamIn  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      streamOut = new DataOutputStream(socket.getOutputStream());
   }
   public void stop()
   {  try
      {  if (console   != null)  console.close();
         if (streamOut != null)  streamOut.close();
         if (streamIn != null)  streamIn.close(); //Is it good practice to close
         if (socket    != null)  socket.close();
      }
      catch(IOException ioe)
      {  System.out.println("Error closing ...");
      }
   }
   public static void main(String args[])
   {  ChatClient client = null;
      if (args.length != 3)
         System.out.println("Usage: java ChatClient host port username");
      else
         client = new ChatClient(args[0], Integer.parseInt(args[1]), args[2]);
   }
}

and this error:

Exception in thread "Thread-1" java.lang.NullPointerException
        at ChatServerThread.handleClient(ChatServerThread.java:41)
        at ChatServerThread.run(ChatServerThread.java:17)

from ChatServerThread:

import java.net.*;
import java.io.*;

//public class ChatServerThread implements Runnable
public class ChatServerThread extends Thread
{  private Socket          socket   = null;
   private ChatServer      server   = null;
   private int             ID       = -1;
   private BufferedReader streamIn =  null;
   private DataOutputStream streamOut = null;

   public ChatServerThread(ChatServer _server, Socket _socket)
   {  server = _server;  socket = _socket;  ID = socket.getPort();
   }
   public void run() {
   try {
       handleClient();
   } catch( EOFException eof ) {
        System.out.println("Client closed the connection.");
   } catch( IOException ioe ) {
        ioe.printStackTrace();
   }
}

   public void handleClient() throws IOException {
      boolean done = false;
      try {
      System.out.println("Server Thread " + ID + " running.");
      while (!done) {
        String nextCommand = streamIn.readLine();
        if( nextCommand.equals(".bye") ) {
           System.out.println("Client disconnected with bye.");
           done = true;
        } else {
           System.out.println( nextCommand );
           String nextReply = "You sent me: " + nextCommand.toUpperCase() + '\n';
           streamOut.writeBytes ( nextReply );
        }
     }
   } finally {
     streamIn.close();
     streamOut.close();
     socket.close();
   }
   }
   public void open() throws IOException
   {
      streamIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      streamOut = new DataOutputStream(socket.getOutputStream());
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
      if (streamIn != null)  streamIn.close();
      if (streamOut != null) streamOut.close();
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:42:10+00:00Added an answer on May 30, 2026 at 1:42 pm

    Yes, it’s this line:

    line = console.readLine();
    

    console is still null. Even though you’re calling start(), it doesn’t do what you think it does:

    public void start() throws IOException
    {  BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    

    This declares a new local variable called console. It doesn’t change the value of the instance variable called console. To do that, you should remove the declaration part:

    public void start() throws IOException
    {  
        console = new BufferedReader(new InputStreamReader(System.in));
        ...
    

    Even with that change, you could get problems – because if that does throw an exception, here’s what you’re doing with it in the constructor:

    catch(IOException ioe)
    {  System.out.println("Unexpected exception: " + ioe.getMessage());
    }
    

    You’re then continuing as if nothing had happened. Don’t do that. You’re not really “handling” the exception – so you should almost certainly either not catch it in the first place, or rethrow it in your catch block.

    As an aside, your bracing style is very dense, very non-conventional, and inconsistent. I would strongly recommend against including code following an opening brace (on the same line). As it is, your code is pretty hard to read for anyone used to either of the (vastly) more common conventions, of:

    if (foo) {
         // Do something
    }
    

    or

    if (foo)
    {
         // Do something
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm getting exception on Transformer transformer = tFactory.newTransformer(StreamXSL); but the error below is not
I'm trying to build a java application with gcj but getting the error below.
EDIT: Fixed most of the problem (but not too sure why). Check the bottom
Update - I fixed the query below. I had the wrong query/error statement :(
This below codes give me error below: How to generate this codes help me
Anyone familiar with error below? When I run my webapp to generate a dynamic
I am getting the error below when I call my WCF service. What am
We are getting the error below calling c:\windows\syswow64\regsvr32.exe on Windows Server 2008 R2 x64.
I'm getting the error below for this SQL statement in VB.Net 'Fill in the
I will get the error below randomly when I'm running an asp.net application I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.