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:
The Client class 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(); } %>
Code:
I've been searching for hours now and I hope that somebody can help me.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(); } } } }
With kind regards,
Sander