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.

Results 1 to 6 of 6

Thread: Exception in thread "Thread-0" .. Please help me!

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Location
    Greece
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "Thread-0" .. Please help me!

    Hello all. I'm new in Java and i have a problem like
    " Exception in thread "Thead-0" java.lang.NullPointerException at clientThread.run<Server.java:123>

    I dont know why .. i try ( users.isEmpty() || users == null ) but no.
    Do you have any idea to solve this problem?

    if( users.isEmpty()){
    users.put(name , addr);

    }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Exception in thread "Thread-0" .. Please help me!

    users.isEmpty() || users == null
    Swap these.
    users == null || users.isEmpty()
    You want to evaluate null BEFORE calling isEmpty() on a potentially null variable.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (April 7th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Location
    Greece
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "Thread-0" .. Please help me!

    oh thanks a lot!
    But no..again exception

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "Thread-0" .. Please help me!

    Post the full text of the message and the source line where it happened.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Apr 2014
    Location
    Greece
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "Thread-0" .. Please help me!

    The code is :

    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    /*
     * A chat server that delivers public and private messages.
     */
    public class Server {
     
      // The server socket.
      private static ServerSocket serverSocket = null;
      // The client socket.
      private static Socket clientSocket = null;
      private int serverPort = 10000;
     
      // This chat server can accept up to maxClientsCount clients' connections.
      private static final int maxClientsCount = 10;
      private static final clientThread[] threads = new clientThread[maxClientsCount];
     
      private Hashtable< String, String > users;
     
      public static void main(String args[]) {
     
        // The default port number.
        int serverPort = 4242;
        /*
         * Open a server socket on the serverPort (default 4242). Note that we can
         * not choose a port less than 1023 if we are not privileged users (root).
         */
        try {
     
            // dimiourgia enos socket na akouei sto port
          serverSocket = new ServerSocket(serverPort);
     
          System.out.println("Server is now online");
          try {
              Thread.sleep(200);
           } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
           }
           System.out.println("Waiting for clients...");
        } catch (IOException e) {
          System.out.println(e);
        }
     
        /*
         * Create a client socket for each connection and pass it to a new client
         * thread.
         */
        while (true) {
          try {
            clientSocket = serverSocket.accept();
            // eisodos kai elegxos gia ton arithmo twn clients
            int i = 0;
            for (i = 0; i < maxClientsCount; i++) {
              if (threads[i] == null) {
                (threads[i] = new clientThread(clientSocket, threads)).start();
                break;
              }
            }
            //clients = 10
            if (i == maxClientsCount) {
              PrintStream os = new PrintStream(clientSocket.getOutputStream());
              os.println("Server too busy. Try later.");
              os.close();
              clientSocket.close();
            }
          } catch (IOException e) {
            System.out.println(e);
          }
        }
      }
    }
    /*
     * The chat client thread. This client thread opens the input and the output
     * streams for a particular client, ask the client's name, informs all the
     * clients connected to the server about the fact that a new client has joined
     * the chat room, and as long as it receive data, echos that data back to all
     * other clients. When a client leaves the chat room this thread informs also
     * all the clients about that and terminates.
     */
    class clientThread extends Thread {
     
      private DataInputStream is = null;
      private PrintStream os = null;
      private Socket clientSocket = null;
      private final clientThread[] threads;
      private int maxClientsCount;
      private Hashtable<String,String> users;
     
      public clientThread(Socket clientSocket, clientThread[] threads) {
        this.clientSocket = clientSocket;
        this.threads = threads;
        maxClientsCount = threads.length;
      }
     
     
      public void run() {
        int maxClientsCount = this.maxClientsCount;
        clientThread[] threads = this.threads;
     
        try {
          /*
           * Create input and output streams for this client.
           */
          is = new DataInputStream(clientSocket.getInputStream());
          os = new PrintStream(clientSocket.getOutputStream());
          os.println("Enter your name.");
          String name = is.readLine().trim(); // onoma
          InetAddress address = InetAddress.getLocalHost(); //address
          String addr = address.toString();
          byte[] ip = address.getAddress(); // ip
     
          // periptwsi poy den uparxei users
          if( users== null || users.isEmpty()){
                users.put(name , addr);
     
          } 
     
     
          os.println("Hello " + name
              + " to our chat room.\nTo leave enter '/quit' in a new line");
          for (int i = 0; i < maxClientsCount; i++) {
            if (threads[i] != null && threads[i] != this) {
              threads[i].os.println("*** A new user " + name
                  + " entered the chat room !!! ^_^ ***");
            }
          }
          while (true) {
            String line = is.readLine();
            if (line.startsWith("/quit")) {
              break;
            }
            for (int i = 0; i < maxClientsCount; i++) {
              if (threads[i] != null) {
                threads[i].os.println("(" + name + ")" + line);
              }
            }
          }
          for (int i = 0; i < maxClientsCount; i++) {
            if (threads[i] != null && threads[i] != this) {
              threads[i].os.println("*** The user " + name
                  + " is leaving the chat room !!! ***");
            }
          }
          os.println("*** Bye " + name + " *** ^_^");
     
          /*
           * Clean up. Set the current thread variable to null so that a new client
           * could be accepted by the server.
           */
          for (int i = 0; i < maxClientsCount; i++) {
            if (threads[i] == this) {
              threads[i] = null;
            }
          }
     
          /*
           * Close the output stream, close the input stream, close the socket.
           */
          is.close();
          os.close();
          clientSocket.close();
        } catch (IOException e) {
        }
      }
     
    }

    and the message :

    Exception in thread "Thead-0" java.lang.NullPointerException at clientThread.run<Server.java:123>
    Last edited by kateyoz; April 7th, 2014 at 04:39 PM.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "Thread-0" .. Please help me!

    Exception in thread "Thead-0" java.lang.NullPointerException at clientThread.run<Server.java:123>
    There is a variable with a null value on line 123. Look at line 123 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 123 and print out the values of all the variables on that line.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. Replies: 2
    Last Post: August 30th, 2012, 09:45 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM