spike74
spike74

Reputation: 185

java code wont run in JSP

I have a JSP page with two tabs and I am trying to run some Java code in the second tab but I keep getting a null pointer exception. I swear my Java Code is correct but the second tab wont show up either.

<%
String fn = request.getParameter("fn9");
String ln = request.getParameter("ln9");
String primaryEmail = request.getParameter("primaryemail9");

CreatingTheProviderFile ctpf = new CreatingTheProviderFile();

char[] c = fn.toCharArray();

if(fn != null){
   System.out.println("it is blank");
}else{
   System.out.println("it is something else");
}

if(fn != ''){
ctpf.FillNameArray(fn, ln, primaryEmail); 
}
%>

JSP for some reason doesnt like the char statement nor does it like the if statement. This is my stacktrace

Dec 18, 2013 11:37:33 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/TwoWayPortal] threw exception [An exception occurred processing JSP page /patient_discharge.jsp at line 800

797:                         
798:                       //  CreatingTheProviderFile ctpf = new CreatingTheProviderFile();
799:                         
800:                         char[] c = fn.toCharArray();
801:                         
802:                         System.out.println(c[0]);
803:                         // System.out.println(String.valueOf(fn.charAt(0)));


Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.patient_005fdischarge_jsp._jspService(patient_005fdischarge_jsp.java:999)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at      

org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:205)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1008)
at    

org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1852)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

I appreciate any help, Thank You

Upvotes: 0

Views: 122

Answers (4)

user2810910
user2810910

Reputation: 287

When you change tabs, you may be losing data in request scope (just a guess). As an experiment, you can consider putting data in session scope instead. If it works, you may conider weather or not its of value to leave it as session scope.

Upvotes: 0

AbhinavRanjan
AbhinavRanjan

Reputation: 1646

Before doing fn.toCharArray() check whether fn is null or not. You are checking it later. But if fn is null, you will get exception on fn.toCharArray() only and rest of the code wont execute.

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

The line that throws the NullPointerException is the line with fn.toCharArray(); This implies that fn is null.

Try

if(fn != null){
    //do stuff here
}

Upvotes: 1

Elliott Frisch
Elliott Frisch

Reputation: 201409

Yes. fn is null.

// test for null, init to null if fn is null.
char[] c = (fn != null) ? fn.toCharArray() : null;

if (fn != null) {
  System.out.println("it is not null");
  if (fn.length() > 0) { // fn != '' is illegal, could have used !fn.equals("") - 
                         // note double quotes (not single).
    ctpf.FillNameArray(fn, ln, primaryEmail); 
  }
} else {
  System.out.println("it is something else");
}

Upvotes: 1

Related Questions