1

现在我被一个错误卡住了,它在 Eclipse 浏览器中显示以下错误。错误看起来像:

13:53:12,294 INFO  [STDOUT] 30
13:53:12,294 ERROR [[Expthsmon]] Servlet.service() for servlet Expthsmon threw exception
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Expthsmon.doGet(Expthsmon.java:44)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:179)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:241)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)

我相信这部分代码

int amountofnoon=0;
    int amountofafternoon=0;
    int noofnoon=0;
    int noofafternoon=0;
    String amountnoon=null;
    String amountafternoon=null;
    amountnoon=request.getParameter("amountnoon");
    amountafternoon=request.getParameter("amountafternoon");
    //if((amountnoon !=null )&&(amountafternoon !=null))

     amountofnoon=Integer.parseInt(amountnoon);
     System.out.println(""+amountofnoon);
     amountofafternoon=Integer.parseInt(amountafternoon);
     System.out.println(""+amountofafternoon);

正在制造问题,其中amountofnoon 和amountofafternoon 应该是整数值。我该怎么办?

我还添加了从中获取值的代码的 jsp 部分:

<form name="expenditurefthsmon" action="Expthsmon">
<div align="center">Enter The Amount of Days Expenditure</div>
 <div align="center"><input type="text"  name="amountnoon"></input></div>
 <div align="center">Enter The Amount of Nights Expenditure</div>
<div align="center"><input type="text" name=amountafternoon"></input></div>
 <div align="center"><input type="submit" value="Calculate"></input></div>"
</form>
4

4 回答 4

2

amountnoonAND/ORamountafternoon有值null,您需要验证请求参数是否为空

于 2012-07-19T08:32:56.907 回答
2

您的 html 中的上述代码缺少 name 属性的引号

<div align="center"><input type="text" name=amountafternoon"></input></div>

它应该是

<div align="center"><input type="text" name="amountafternoon"></input></div>

除此之外,您绝不能相信来自用户的输入,并始终在进入核心逻辑之前验证它是否为无效或空值。

于 2012-07-19T09:01:24.790 回答
1

你应该检查空值。例如

if(amountnoon != null){
    amountofnoon=Integer.parseInt(amountnoon);
}
于 2012-07-19T08:34:11.870 回答
1

最有可能的是,这两个调用之一返回null

amountnoon=request.getParameter("amountnoon");
amountafternoon=request.getParameter("amountafternoon");

在将这两个值传递给Integer.parseInt.

如果你传递nullparseInt,你会得到NumberFormatException: null你描述的错误。

于 2012-07-19T08:35:05.953 回答