Results 1 to 2 of 2
  1. #1
    sanderstad is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default Nullpointer exception when sending command

    Hi there,

    I hope someone can help me because I'm struggeling to solve this issue.

    I have a client/server application where I send a command to a server where is executes a certain method. The client is a JSp page which initializes a Client class where the command is send to. It is this Client class which gives me a nullpointer exception where I can't lay my finger on.

    The issue comes when I use the "sendCommand(String c)" method from the Client class which gives the following error in Tomcat 7:
    -----------------------------------------------------------------------------------
    HTTP Status 500 -

    --------------------------------------------------------------------------------

    type Exception report

    message

    description The server encountered an internal error () that prevented it from fulfilling this request.

    exception

    org.apache.jasper.JasperException: java.lang.NullPointerException
    org.apache.jasper.servlet.JspServletWrapper.handle JspException(JspServletWrapper.java:548)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:471)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)


    root cause

    java.lang.NullPointerException
    com.jennifersoft.client.lib.Client.sendCommand(Cli ent.java:38)
    org.apache.jsp.JANEClient_jsp._jspService(JANEClie nt_jsp.java:75)
    org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)
    org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:433)
    org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:389)
    org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:333)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:722)


    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.20 logs.
    -----------------------------------------------------------------------------------

    The JSP page contains the following code:
    Java Code:
    <%
    	//String req = request.getParameter("command");
    
    	if(request.getParameter("command") != null || request.getParameter("command") != ""){
    		
    		String command = request.getParameter("command");
    		Client client = new Client();
    		
    		//client.connectServer();
    		out.println(command);
    		client.sendCommand(command.toString());
    		client.disconnect();
    	}
    %>
    The Client class code:
    Java Code:
    public class Client {
    
    	private static final String HOSTNAME = "127.0.0.1";
    	private static final int PORT = 20000;
    
    	private Socket socket = null;
    	private BufferedWriter wr = null;
    	
    	public Client(){
    		try {
    			socket = new Socket(HOSTNAME, PORT);
    			Thread.sleep(2000);
    		} catch (UnknownHostException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (InterruptedException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    	
    	public void sendCommand(String c){
    		
    		try{
    			if(socket != null){
    				socket = new Socket(HOSTNAME, PORT);
    			}
    			wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    		    wr.write(c);
    		    wr.flush();
    		    wr.close();
    		}catch(IOException iex){
    			
    		}
    	}
    	
    	public boolean getConnectionStatus(){
    		return socket.isConnected();
    	}
    	
    	public void disconnect(){
    		if(socket != null){
    			try {
    				socket.close();
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    	}
    }
    I've been searching for hours now and I hope that somebody can help me.

    With kind regards,

    Sander

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    Java Code:
    	if(request.getParameter("command") != null || request.getParameter("command") != ""){
    That will still allow a null command through. Do you actually mean AND not OR?
    That doesn't appear to be the problem, as:
    Java Code:
    		client.sendCommand(command.toString());
    that pointless toString() call would throw an NPE before getting into the sendCommand code.

    You haven't told us which line is line 38 in the CLient code, but:
    Java Code:
    			if(socket != null){
    				socket = new Socket(HOSTNAME, PORT);
    			}
    			wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    You create a new Socket if the socket isn't null.
    So if it is null you would then happily continue to create a writer based on the output stream of a null pointer.

    Considering your socket should never be null (it's initialised in the constructor) then that whole check is pointless (even if it was correct).
    Your constructor should deal with it, probably by throwing the exceptions. After all, if it fails to create the socket then you can't continue processing.

Similar Threads

  1. Nullpointer Exception with BufferedWriter?
    By Solarsonic in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 01:08 AM
  2. Nullpointer Exception???
    By kipcorn91 in forum AWT / Swing
    Replies: 5
    Last Post: 10-29-2010, 12:19 AM
  3. NullPointer exception
    By bdario1 in forum New To Java
    Replies: 15
    Last Post: 03-17-2010, 05:44 AM
  4. nullpointer exception in jsp
    By fiero in forum JavaServer Pages (JSP) and JSTL
    Replies: 6
    Last Post: 11-07-2008, 02:44 PM
  5. NullPointer Exception
    By Preethi in forum New To Java
    Replies: 8
    Last Post: 02-06-2008, 04:40 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •