尝试显示查询中的值后出现 java.lang.NumberFormatException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5111369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NumberFormatException after trying to present values from a query
提问by Skyzer
public List findCatalog() {
Query query = getEntityManager().createQuery("SELECT pc.productCatalog, p.name, p.product FROM ProductCatalog pc JOIN pc.products p");
return query.getResultList();
}
Hello, with such query my application compiles okay. But when i open the page where this query is executed i get the following error:
您好,通过这样的查询,我的应用程序编译正常。但是当我打开执行此查询的页面时,出现以下错误:
org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "productCatalog"
org.apache.jasper.JasperException:java.lang.NumberFormatException:对于输入字符串:“productCatalog”
The productCatalog is my primary key, but same happens when i just include the name column
productCatalog 是我的主键,但是当我只包含 name 列时也会发生同样的情况
exception org.apache.jasper.JasperException: java.lang.NumberFormatException: For input string: "name" root cause
java.lang.NumberFormatException: For input string: "name"
异常 org.apache.jasper.JasperException:java.lang.NumberFormatException:对于输入字符串:“name”根本原因
java.lang.NumberFormatException:对于输入字符串:“名称”
If i don't make the join in my query then the results from 1 table are printed okay. I'm clueless for now where the problem could be, i have read Pro JPA 2 book, official java EE 6 tutorial, googled alot. The tables design is ManyToMany. I have producttable, *product_catalog* table and the binding table *product_product_catalog. I don't have foreign keys in the binding table. I have created the entity and facade classes with the NetBeans EclipseLink wizard.
如果我不在我的查询中进行连接,那么 1 个表中的结果就可以正常打印。我现在不知道问题出在哪里,我已经阅读了 Pro JPA 2 书,官方 Java EE 6 教程,谷歌搜索了很多。表设计是多对多。我有产品表、*product_catalog* 表和绑定表 *product_product_catalog。我在绑定表中没有外键。我已经使用 NetBeans EclipseLink 向导创建了实体和外观类。
Here is how i mapped on my ProductCatalog class many to many relationship
这是我如何映射到我的 ProductCatalog 类的多对多关系
@ManyToMany
@JoinTable(name = "product_product_catalog",
joinColumns = {
@JoinColumn(name = "product_catalog")
},
inverseJoinColumns = {
@JoinColumn(name = "product")
})
private Collection<Product> products;
product and product_catalog are the primary keys for corresponding tables. Now i have no idea where i could be wrong, perhaps is the mapping wrong? Though many examples which i have seen, have the same mapping implementation, but in their tables they have foreign keys, could that be an issue? Moreover as i said if i don't join second table, everything works well.
product 和 product_catalog 是对应表的主键。现在我不知道我哪里错了,也许是映射错误?虽然我见过的许多例子都有相同的映射实现,但是在他们的表中他们有外键,这可能是一个问题吗?此外,正如我所说,如果我不加入第二张桌子,一切都很好。
Here is my servlet with initializing that method getServletContext().setAttribute("productCatalog", productCatalogFacade.findCatalog());
这是我的 servlet 初始化该方法 getServletContext().setAttribute("productCatalog", productCatalogFacade.findCatalog());
And here is my JSP page fragment of that
这是我的 JSP 页面片段
<table>
<c:forEach var="list" items="${productCatalog}" varStatus="iter">
<tr>
<td>${list.productCatalog}</td>
<td>${list.product}</td>
<td>${list.name}</td>
</tr>
</c:forEach>
</table>
And here is my native SQL query what i'm trying to achieve
这是我想要实现的本机 SQL 查询
SELECT PPC.product_product_catalog, PPC.product_catalog, PPC.product, P.name, PC.name AS "catalog name", P.code, P.description, P.price, P.producer
FROM product_catalog PC
INNER JOIN product_product_catalog PPC ON PC.product_catalog = PPC.product_catalog
INNER JOIN product P ON P.product = PPC.product
As my last piece of code here is the output which glassfish produces when i go to that page where the query is executed:
作为我在这里的最后一段代码,是当我转到执行查询的那个页面时 glassfish 产生的输出:
WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception
java.lang.NumberFormatException: For input string: "productCatalog"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.parseInt(Integer.java:499)
at javax.el.ArrayELResolver.toInteger(ArrayELResolver.java:375)
at javax.el.ArrayELResolver.getValue(ArrayELResolver.java:195)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175)
at com.sun.el.parser.AstValue.getValue(AstValue.java:116)
at com.sun.el.parser.AstValue.getValue(AstValue.java:163)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:219)
at org.apache.jasper.runtime.PageContextImpl.evaluateExpression(PageContextImpl.java:1007)
at org.apache.jsp.index_jsp._jspx_meth_c_forEach_0(index_jsp.java from :206)
at org.apache.jsp.index_jsp._jspService(index_jsp.java from :137)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:406)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:619)
回答by axtavt
When you execute a request with many variables in SELECT
clause, such as
当您在SELECT
子句中执行带有许多变量的请求时,例如
SELECT pc.productCatalog, p.name, p.product ...
each row of result is returned in form of Object[]
, so you need to use numeric indexes to access its elements:
结果的每一行都以 的形式返回Object[]
,因此您需要使用数字索引来访问其元素:
<c:forEach var="list" items="${productCatalog}" varStatus="iter"><tr>
<td>${list[0]}</td>
<td>${list[2]}</td>
<td>${list[1]}</td>
</tr></c:forEach>
回答by Fast Engy
I had a similar problem in my implementation using Java EE7, JSF2.2, JPA2 and @NamedStoredProcedureQuerywhich @axtavt solution helped solve (+1). I wasn't defining the result class correctly and hence was returning an Object[] instead of a List of the defined entity class.
我在使用 Java EE7、JSF2.2、JPA2 和@NamedStoredProcedureQuery 的实现中遇到了类似的问题,@ axtavt 解决方案帮助解决了 (+1)。我没有正确定义结果类,因此返回一个 Object[] 而不是已定义实体类的 List。
If you create an entity class to handle the data return type you can then use annotations on that entity class and it will handle mapping automagically:
如果您创建一个实体类来处理数据返回类型,那么您可以在该实体类上使用注释,它将自动处理映射:
@NamedStoredProcedureQuery(
name = "getJoinedSiteData",
procedureName = "func_joined_site_data",
resultClasses = Site.class,
parameters = {@StoredProcedureParameter(mode = IN, name = "site_id", type = Integer.class)}
)
public class Site implements Serializable{
...
}
You can call this then in your controller like:
您可以在控制器中调用它,例如:
public List<Site> getItems(Integer siteId){
StoredProcedureQuery query = em.createNamedStoredProcedureQuery("getJoinedSiteData");
return query.setParameter("site_id", siteId).getResultList();
}
This then enables direct binding to the data model as per JSF2.2 you can use the standard ui:repeat or h:dataTable like below. eg.
然后,根据 JSF2.2,您可以直接绑定到数据模型,您可以使用标准的 ui:repeat 或 h:dataTable,如下所示。例如。
<h:dataTable value="#{controller.getItems(someSiteId)}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Site Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
</h:dataTable>
回答by JenEriC
Is this perchance the same as https://glassfish.dev.java.net/issues/show_bug.cgi?id=6806?
这种可能性是否与https://glassfish.dev.java.net/issues/show_bug.cgi?id=6806相同?
回答by Dead Programmer
Somewhere in your code, you are using Integer.parseInt for these columns name and productCatalog,if you have parse methods Then argument to this should be a number. No empty string or character allowed.
在您的代码中的某处,您将 Integer.parseInt 用于这些列名称和 productCatalog,如果您有 parse 方法那么这个参数应该是一个数字。不允许空字符串或字符。
回答by Tim
I don't think the error is where you think it is.
我不认为错误是你认为的地方。
It looks to me like it's this line
在我看来就像是这条线
<td>${list.productCatalog}</td>
that's the problem.
那就是问题所在。
It appears as though Jasper (the JSP engine) thinks that "list" is an array, so it's trying to treat your expression as if it were "list.0" or "list.1"
似乎 Jasper(JSP 引擎)认为“list”是一个数组,因此它试图将您的表达式视为“list.0”或“list.1”
Who do I think this?
我觉得这是谁?
- You are executing the query before you get to the JSP, so if the query was the problem then you would not be getting a JSP exception.
- Your stack trace has
at org.apache.jsp.index_jsp._jspx_meth_c_forEach_0
which means that the exception is occurring inside a foreach - The stack trace also has
at javax.el.ArrayELResolver.toInteger
which means that it's trying to do an array expression resolution.
- 您在到达 JSP 之前执行查询,因此如果查询是问题,那么您将不会收到 JSP 异常。
- 您的堆栈跟踪
at org.apache.jsp.index_jsp._jspx_meth_c_forEach_0
意味着异常发生在 foreach 内 - 堆栈跟踪也
at javax.el.ArrayELResolver.toInteger
意味着它正在尝试进行数组表达式解析。
I'm pretty sure if you change your JSP to
我很确定您是否将 JSP 更改为
<table>
<c:forEach var="list" items="${productCatalog}" varStatus="iter">
<tr>
<td>This is a row</td>
</tr>
</c:forEach>
</table>
your exception will go away.
That will prove that the problem is caused by trying to access the properties of list
, and you'll then need out work out why Jasper thinks that list
is an array.
你的例外会消失。这将证明问题是由尝试访问 的属性引起的list
,然后您需要弄清楚为什么 Jasper 认为这list
是一个数组。