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: Nullpointer exception when sending string

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Nullpointer exception when sending string

    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:
    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:
    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Nullpointer exception when sending string

    java.lang.NullPointerException
    com.jennifersoft.client.lib.Client.sendCommand(Cli ent.java:38)
    What variable at line 38 is null?

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointer exception when sending string

    It's a request parameter which is a string that is send from the JSP page.
    client.sendCommand(command.toString());

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    13
    My Mood
    Cool
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nullpointer exception when sending string

    i solved the issue. Was due a piece of code which closed the server connections.

Similar Threads

  1. Replies: 8
    Last Post: August 9th, 2011, 08:25 PM
  2. Need help with exception handling for a string
    By toppcon in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 6th, 2011, 06:59 AM
  3. Sending large Strings ?! only sending a line
    By camel in forum Java Networking
    Replies: 2
    Last Post: April 19th, 2011, 12:41 PM
  4. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  5. Sending SMS in java
    By pipi in forum What's Wrong With My Code?
    Replies: 12
    Last Post: January 26th, 2011, 08:10 PM