【发布时间】:2014-11-18 16:55:43
【问题描述】:
我正在尝试解决 Java 中的 List 问题,但我不能。我有以下代码:
Productos producto = ... //come from a database, here there isn't errors
List<Productos> lista = new ArrayList<Productos>();
lista.set(0,producto);
而且我不能在我的列表中介绍“producto”,我不知道为什么。我也尝试过使用"lista.add(producto)",但它不起作用。我能做什么?
Edit1:Producto 不为空,它已初始化,并且具有一些具有正确值的属性。我也使用了 lista.add(0,producto) 但它不起作用:(。错误是:
Grave: java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at clientesCarrito.ClienteCarritoCompraServlet.doPost(ClienteCarritoCompraServlet.java:68)
是的,对 Int 进行“错误转换”似乎是一个错误,但我不认为..这是我的代码的一部分:
EntityManager em = entityManagerFactory.createEntityManager();
.
.
int id = Integer.parseInt(request.getParameter("IdProd"));
Productos producto = em.find(Productos.class,id);
HttpSession session = request.getSession(false);
if(session.getAttribute("carProductos")==null){
List<Productos> lista = new ArrayList<Productos>();
lista.add(producto);
session.setAttribute("carProductos", lista);
}else{
List<Productos> lista = (List<Productos>) session.getAttribute("carProductos");
lista.add(producto);
session.setAttribute("carProductos", lista);
}
我已经在 Eclipse 中进行了调试,“IdProd”是 5,“id”也是 5。现在它看起来像“lista”捕获“producto”并自我介绍,但是当它在底部时突然显示错误并崩溃。在这段代码之后,我不再使用“lista”或“producto”。有人可以帮助我吗?
【问题讨论】:
-
请告诉我们确切的文字错误。
-
怎么不工作?
producto是否为空? -
你试过 lista.add(0, producto)
标签: java list object initialization initializer-list