使用NumberFormatException

时间:2011-06-11 12:38:36

标签: java jsp jstl el numberformatexception

在我的网络应用中,我有一个带

的Item类
public class Item {
    private Long item_id;
    private String name;
...
}

...
<servlet-mapping>
  <servlet-name>edititemservlet</servlet-name>
  <url-pattern>/edititem</url-pattern>
 </servlet-mapping>
...

在列出项目时,我尝试使用

<a href="<c:url value="editItem?itemId=${item.item_id}" />" >${item.name}</a>

这样可行。但是,当我尝试以下内容时使用

<c:url value="editItem" var="editItemurl">
    <c:param name="itemId" value="${item.item_id}" />
</c:url>
<c:forEach var="item" items="${items}" varStatus="itemslistCount">
<tr><td>${itemslistCount.count} </td>
<td><a href="${editItemurl}" >${item.name}</a></td>

我希望得到相同的结果。 但这会导致NumbeFformatException。

java.lang.NumberFormatException: For input string: ""
    java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    java.lang.Long.parseLong(Long.java:424)

我试着打印 $ {editItemurl}并显示一个空字符串。

我知道如何纠正这个问题吗?

感谢

标记

P.S: 这是堆栈跟踪

java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Long.parseLong(Long.java:424)
    at java.lang.Long.parseLong(Long.java:461)
    at myshop.servlets.ItemEditServlet.doGet(Unknown Source)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:619)

1 个答案:

答案 0 :(得分:3)

<c:url>必须进入<c:forEach>才能访问${item}

但是,我无法解释为什么NFE被抛出。为此,我需要知道整个堆栈跟踪。