Results 1 to 6 of 6
  1. #1
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default null pointer exception in multi threading

    Guys, I am getting null pointer exception in my server multi threading code.
    The error occurs towards the end of the code where I close my server (server.close();)

    I have intialized the server with a null value inside run(). But actually i want to close the server which i created inside main(). But I cant access it from outside main(). how do i solve this issue???

    Java Code:
    
    import java.io.*;
    import java.net.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class server implements Runnable {
    
        Socket socket = null;
    
        public server(Socket socket) {
            this.socket = socket;
        }
    
        public static void main(String args[]) {
    
            ServerSocket server = null;
            Socket socket = null;
    
            try {
                //create socket
                server = new ServerSocket(5000, 100);
                System.out.println("Waiting for connection");
    
            } catch (IOException ex) {
                Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
            }
            while (true) {
                try {
                    //accept nxt connection request
                    socket = server.accept();
                    System.out.println("Connection received from " + socket.getInetAddress().getHostName());
    
                    server myServer = new server(socket);
                    Thread t = new Thread(myServer);
                    Thread t1 = Thread.currentThread();
                    String name = t1.getName();
                    t.start();
                    System.out.println(" thread name is:" +name);
    
                } catch (IOException ex) {
                    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    
        public void run() {
    
            ObjectInputStream in = null;
            ObjectOutputStream out = null;
            boolean exit = false;
            ServerSocket server = null;
    
            try {
                //get input and output streams
                out = new ObjectOutputStream(socket.getOutputStream());
                in = new ObjectInputStream(socket.getInputStream());
                System.out.println("Connection successfull");
                
                //reading input string
                String inputStr = (String) in.readObject();
                System.out.println("client> " + inputStr);
                if (!inputStr.equals("exit")) {
                    if (inputStr.equals("vivek")) {
                        out.writeObject("Granted");
                    } else {
                        out.writeObject("Denied");
                    }
                } else {
                    exit = true;
                    out.writeObject("exiting");
                }
                //close connection
                in.close();
                out.close();
                socket.close();
            } catch (IOException ex) {
                Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
            } catch (Exception e) {
                System.err.println("Exception: " + e.getMessage());
                e.printStackTrace();
            } finally {
                try {
                    server.close();
                } catch (IOException ex) {
                    Logger.getLogger(server.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

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

    Default

    Just use basic OOPs techniques. Give the server class a private ServerSocket variable and pass a reference to the ServerSocket created in main into your "server" class object via its constructor or via a public setServerSocket method that you create.

  3. #3
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    how do I do that? this si what I did. is it correct?
    Java Code:
     private ServerSocket server = null;
    
    
    
        public server(Socket socket, ServerSocket server) {
            this.socket = socket;
            this.server = server;
        }

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

    Default

    Quote Originally Posted by niteangell21 View Post
    how do I do that? this si what I did. is it correct?
    Java Code:
     private ServerSocket server = null;
    
    
    
        public server(Socket socket, ServerSocket server) {
            this.socket = socket;
            this.server = server;
        }
    So far looks OK, but bottom line is, does it work? Does it fix your problem?

  5. #5
    niteangell21 is offline Member
    Join Date
    Sep 2010
    Posts
    40
    Rep Power
    0

    Default

    no it didnt. still getting null pointer exception

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

    Default

    Quote Originally Posted by niteangell21 View Post
    no it didnt. still getting null pointer exception
    So debug it. Find the line that has the NPE, see which object is null, and then trace back in your code to find out why the object hasn't been initialized.

Similar Threads

  1. Null pointer exception
    By izzy in forum New To Java
    Replies: 5
    Last Post: 03-22-2010, 05:19 PM
  2. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 03:25 AM
  3. Null pointer exception
    By Stephenmak in forum New To Java
    Replies: 5
    Last Post: 04-01-2009, 02:17 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 05:56 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
  •