ArrayList
的加法方法在org.apache.jasper.JasperException: java.lang.NullPointerException
中调用cart.jsp
时抛出@Remote
public interface BookSessionBeanRemote {
void persist(Book b);
List<Book> getBookContent();
Book getBook(int id);
}
@Stateful
public class CartBean implements CartBeanRemote {
Book b = new Book();
private ArrayList<Book> bk;
@PostConstruct
public void initialize()
{
bk = new ArrayList<Book>();
}
@Override
public void addBook(Book b)throws Exception
{
if(b==null)throw new Exception("Book is null");
bk.add(b);
}
@Override
public void removeBook(int id)throws Exception
{
BigDecimal bg = new BigDecimal(id);
b.setBookid(bg);
boolean result = bk.remove(b);
if (result == false)
{
throw new Exception("Book not in cart.");
}
}
@Override
public ArrayList<Book> getContent()
{
return bk;
}
}
。请看我的代码。感谢。
<%! CartBeanRemote ct; %>
<%
String path = (String)session.getAttribute("path");
String bkid = (String)session.getAttribute("bookid");
String bookname = (String)session.getAttribute("bookname");
String author = (String)session.getAttribute("author");
String price= (String)session.getAttribute("price");
// out.println(path);
// out.println(bkid);
// out.println(bookname);
// out.println(author);
// out.println(price);
BigDecimal bid = new BigDecimal(bkid);
BigInteger pr = new BigInteger(price);
if(session.isNew())
{
Context ctx = new InitialContext();
ct = (CartBeanRemote)ctx.lookup(CartBeanRemote.class.getName());
session.setAttribute("ct", ct);
}
ct = (CartBeanRemote)session.getAttribute("ct");
Book book = new Book();
book.setAuthor(author);
book.setImagepath(path);
book.setName(bookname);
book.setBookid(bid);
book.setPrice(pr);
ct.addBook(book);
// out.println(book.getAuthor());
// out.println(book.getBookid());
// out.println(book.getPrice());
// response.sendRedirect("index.jsp");
%>
cart.jsp
{{1}}
答案 0 :(得分:0)
您的代码在某处导致NullPointerException。根据您的描述,当您尝试向其添加内容时,我猜您的ArrayList为null。
当您的代码导致NullPointerException时,JSP将捕获它,然后在重新抛出之前将其包装在JasperException中。
看一下Throwable.getCause()。另请参阅Exception的构造函数,它看起来像Exception(String message,Throwable cause)。 Java中的常见做法是捕获一种类型的Exception,并将其包含在带有附加信息的新Exception中。在异常被传播到代码可以做一些有意义的事情来处理它之前,这可能会发生多次。