问题出现所在servlet:
private String add(HttpServletRequest req) {
String id = req.getParameter("entityId");
String quantityStr = req.getParameter("quantity");
System.out.println(quantityStr);

long quantity = 1;
if (Validator.isEmpty(quantityStr)){
//Validator是一个调用的类.isEmpty是类中的一个判断为空的方法
}
if (!Validator.isEmpty(quantityStr))
quantity = Long.parseLong(quantityStr); Product product = productService.findById(id);
ShoppingCart cart = getCartFromSession(req);
System.out.println(quantity);
cart.addItem(product, quantity);//出错行84
return "addSuccess";
}上面ShoppingCart类中的addItem方法:
public void addItem(Product product, long quantity) {
items.add(new ShoppingCartItem(product, quantity));//出错行16
}上面所调用的ShoppingCartItem中的有参构造函数:
private Product product;
private long quantity;
private float cost; public ShoppingCartItem(Product product, long quantity) {
this.product = product;
this.quantity = quantity;
this.cost = product.getPrice() * quantity;//出错行39
}product类:
public class Product implements java.io.Serializable { // Fields private Integer id;
private String name;
private String description;
private Float price;
private Integer stock;
private Integer epcId;
private Integer childId;
private String fileName; // Constructors /** default constructor */
public Product() {
} /** minimal constructor */
public Product(Integer id) {
this.id = id;
} /** full constructor */
public Product(Integer id, String name, String description,
Float price, Integer stock, Integer epcId, Integer childId,
String fileName) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.stock = stock;
this.epcId = epcId;
this.childId = childId;
this.fileName = fileName;
} // Property accessors public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} public String getDescription() {
return this.description;
} public void setDescription(String description) {
this.description = description;
} public Float getPrice() {
return this.price;
} public void setPrice(Float price) {
this.price = price;
} public Integer getStock() {
return this.stock;
} public void setStock(Integer stock) {
this.stock = stock;
} public Integer getEpcId() {
return this.epcId;
} public void setEpcId(Integer epcId) {
this.epcId = epcId;
} public Integer getChildId() {
return this.childId;
} public void setChildId(Integer childId) {
this.childId = childId;
} public String getFileName() {
return this.fileName;
} public void setFileName(String fileName) {
this.fileName = fileName;
}}
报错:null是通过req.getParameter("quantity");获取到的quantity值,1是经过处理后打印的(System.out.println(quantity);)要传入84行的值
null
2013-5-2 21:21:06 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet Cart threw exception
java.lang.NullPointerException
at com.mipo.vo.ShoppingCartItem.<init>(ShoppingCartItem.java:39)
at com.mipo.vo.ShoppingCart.addItem(ShoppingCart.java:16)
at com.mipo.servlet.CartServlet.add(CartServlet.java:84)
at com.mipo.servlet.CartServlet.doPost(CartServlet.java:55)
at com.mipo.servlet.CartServlet.doGet(CartServlet.java:39)
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.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
1