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

  • Home
  • SEARCH
  • 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 8821541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:52:53+00:00 2026-06-14T05:52:53+00:00

I am implementing a tcp java client/server app based on a tut on the

  • 0

I am implementing a tcp java client/server app based on a tut on the internet
I changed some methods and get error of nullpointerexception

The error comes from the run(). Its in a Connection class that extends the Thread class. run() is supposed to listen to incoming communication. I start the server class, listen to a port. So I have a succefful connection. Then the app reports error when I try to listen to incoming communication through the run() method

establish a connection and start:

Connection c = new Connection(host,port);
                Scanner chatConsole = new Scanner(System.in);
                String text = "";

                while (!text.equalsIgnoreCase("halt")){         
                    text = chatConsole.nextLine();
                    c.start();

Exception in thread “Thread-1” java.lang.NullPointerException

The run()

public void run(){//watch for incoming communication
    String msg;

    try{//loop reading lines and display msg
        while ((msg = in.readLine()) != null) {
            System.out.println(msg);
        }
    }catch (IOException e) {
        System.err.println(e);
    }   
}

This is the constructor for the class if its helpful to debug.

public Connection(String iniName, int iniPort){
        host = iniName;
        port = iniPort;

        try {
            server = new Socket(host, port);
        }catch (UnknownHostException e){
            System.err.println(e);
            System.exit(1);
        }

        try{
            stdIn = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter out = new PrintWriter(server.getOutputStream(), true); //output stream to server
            BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
        }catch(Exception e){
            System.err.println(e);
        }
    }

Thank you

  • 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-06-14T05:52:54+00:00Added an answer on June 14, 2026 at 5:52 am

    Your in variable is null, where you try to use it:

    while ((msg = in.readLine()) != null) {
    

    possibly because you’re shadowing it where you construct it.

    BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
    

    Instead don’t re-declare the variable locally:

    in = new BufferedReader(new InputStreamReader(server.getInputStream()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write java codes implementing the TCP server & TCP client with
I'm implementing a simple TCP client and TCP server in Java at the moment,
I'm implementing a java TCP/IP Server using ServerSocket to accept messages from clients via
I'm implementing a simple client-server program. Some parts of the i wrote is: int
I'm implementing a client/server pair, commnicating through TCP/IP sockets, using overlapped IO with completion
I'm writing a backend for a mobile web-app based in Java and I was
I'm implementing a small TCP server. How do I know if one of my
Can anyone propose good design idea for implementing Client for processing messages from TCP/IP.Messages
I am implementing a Windows-based web server handling multiple specific HTTP requests from clients
I have the following algorithm implemented in Java which uses TCP/IP: -Client request a

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.