java代码不会在JSP中运行

时间:2013-12-18 17:58:45

标签: java jsp

我有一个带有两个选项卡的JSP页面,我试图在第二个选项卡中运行一些Java代码,但我一直得到一个空指针异常。我发誓我的Java代码是正确的,但第二个标签也不会出现。

<%
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出于某种原因不喜欢char语句,也不喜欢if语句。这是我的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)

我感谢任何帮助, 谢谢

4 个答案:

答案 0 :(得分:1)

是。 fn为空。

// 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");
}

答案 1 :(得分:1)

抛出NullPointerException的行是fn.toCharArray();这意味着fn为空。

尝试

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

答案 2 :(得分:1)

在执行fn.toCharArray()之前,请检查fn是否为null。你以后要检查它。但是如果fn为null,你将只在fn.toCharArray()上获得异常,其余代码不会执行。

答案 3 :(得分:0)

更改标签时,您可能会丢失请求范围内的数据(只是猜测)。作为实验,您可以考虑将数据放入会话范围。如果它有效,你可以将天气与天气相提并论,将其作为会议范围。