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 4 of 4

Thread: null pointer exception on extending a server class

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default null pointer exception on extending a server class

    Hey everyone, I am getting a null pointer exception (I will highlight the location after the code is shown). I am trying to extend a class that is a server:

     
    import java.io.IOException;
    import java.io.*;
    import java.net.*;
    public class MultiChatServer extends MultiEchoServer{
     
    	static private ServerSocket serverSocket;
    	 static private Socket connection;
     
    	//serversocket instanciation
    	public MultiChatServer(int port) throws IOException{
    		super(port);
    		//create a list to store all the the connections
    	}
     
    	public static void main(String args[])throws IOException{
     
    		new MultiChatServer(4433);//construct a serversocket
     
    		while(true){
    			//STEP2: Do a blocking listen to the established server socket
    			//when a new connection occurs, the accept method will trigger and and another thread will be instantiated
    			connection = serverSocket.accept(); 
    			System.out.println("New client requesting to connect on connection "+ (connection.getInetAddress().getHostName()));
    			HandlerThread newThread = new HandlerThread(connection); //pass connection to be threaded
    			System.out.println("Started a new thread for new client");
    			newThread.start(); //envokes run method	
    		}
    		//serverSocket.close();
     
     
    	}
     
     
    }

    The line
    connection = serverSocket.accept();

    causes the nullPointerException.

    Just to be complete, here is the class i am extending:

    import java.io.*;
    import java.net.*;
     
    public class MultiEchoServer{	
    //Initialise variables
    		static private ServerSocket serverSocket = null;
    		static private Socket connection = null;
     
    	//constructor
    	public MultiEchoServer(int port) throws IOException{
    		try{
    			System.out.println("Server waiting for a client to connect on port 4433" );
    			serverSocket = new ServerSocket(port);
    		}catch(IOException e){
    			System.err.println("Could not listen on port: 4433");
    			System.exit(1);
    		}
    	} //end constructor
     
     
    	public static void main(String args[])throws IOException{
     
    		new MultiEchoServer(4433);
     
    		while(true){
     
    			connection = serverSocket.accept(); 
    			System.out.println("New client requesting to connect on connection "+ (connection.getInetAddress().getHostName()));
    			HandlerThread newThread = new HandlerThread(connection); 
    			System.out.println("Started a new thread for new client");
    			newThread.start(); 
    		}
    		//serverSocket.close();
     
    	}
    }

    All I am trying to do is be in a position to have the same functionality as the superclass. I want to redefine the while loop in the main in the subclass.


  2. #2
    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: null pointer exception on extending a server class

    connection = serverSocket.accept();
    If the error is on this line, it must be that serverSocket is null.
    Where is serverSocket given a non null value?

    I see more than one definition for serverSocket which one gets a value?
    Last edited by Norm; March 23rd, 2012 at 12:24 PM.
    If you don't understand my answer, don't ignore it, ask a question.

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

    u-will-neva-no (March 24th, 2012)

  4. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    20
    Thanked 0 Times in 0 Posts

    Default Re: null pointer exception on extending a server class

    When I extend MultiEchoServer, am I correct to say that the main method is not inherited? I thought that when I extend a superclass then I only get the functionality of the previous constructor.

    If this is not the case, how do I modify the main? What I want to do is add extra line of code in the main of the subclass.

  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: null pointer exception on extending a server class

    Which variable is getting the value and which one are you using? I see two definitions for the serverSocket. The one in the super class gets a value.
    Before using it print out its value and the super's value to see which has a value.
    Last edited by Norm; March 24th, 2012 at 08:50 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [B]Null Pointer Exception[/B]-- Please Help!!!
    By entwicklerin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 24th, 2012, 11:24 AM
  2. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 09:00 PM
  3. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  4. Null Pointer Exception Help!!
    By puzzledstudent in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 11th, 2010, 06:46 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM