Results 1 to 2 of 2
- 08-25-2011, 08:16 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 5
- Rep Power
- 0
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:
The Client class 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(); } %>
I've been searching for hours nowJava 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(); } } } }

and I hope that somebody can help me.
With kind regards,
Sander
- 08-26-2011, 09:38 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
That will still allow a null command through. Do you actually mean AND not OR?Java Code:if(request.getParameter("command") != null || request.getParameter("command") != ""){
That doesn't appear to be the problem, as:
that pointless toString() call would throw an NPE before getting into the sendCommand code.Java Code:client.sendCommand(command.toString());
You haven't told us which line is line 38 in the CLient code, but:
You create a new Socket if the socket isn't null.Java Code:if(socket != null){ socket = new Socket(HOSTNAME, PORT); } wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
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
-
Nullpointer Exception with BufferedWriter?
By Solarsonic in forum New To JavaReplies: 3Last Post: 03-23-2011, 12:08 AM -
Nullpointer Exception???
By kipcorn91 in forum AWT / SwingReplies: 5Last Post: 10-28-2010, 11:19 PM -
NullPointer exception
By bdario1 in forum New To JavaReplies: 15Last Post: 03-17-2010, 04:44 AM -
nullpointer exception in jsp
By fiero in forum JavaServer Pages (JSP) and JSTLReplies: 6Last Post: 11-07-2008, 01:44 PM -
NullPointer Exception
By Preethi in forum New To JavaReplies: 8Last Post: 02-06-2008, 03:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks