Attempted a bean operation on a null object

jsp错误提示:
org.apache.jasper.JasperException: Attempted a bean operation on a null object.
org.apache.jasper.runtime.JspRuntimeLibrary.handleGetProperty(JspRuntimeLibrary.java:600)
org.apache.jsp.shopping_jsp._jspService(shopping_jsp.java:56)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
jsp代码如下:
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="com.jspdev.ch6.*"%>
<jsp:useBean id="products" class="com.jspdev.ch6.Products" scope="session"/>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<LINK href="hellking2.css" type=text/css rel=stylesheet>
<body>
<center>
<%@ include file="header.jsp"%>
<form action="cart.jsp" method=get>
<table width="75%" border="1" bordercolor="#006633">
  <tr bgcolor="#999999">
    <td>id</td>
    <td>名称</td>
    <td>价格</td>
    <td>是否有库存</td>
    <td>出版社</td>
  </tr>
  <%
  java.util.Vector v=products.getItems();
  java.util.Enumeration e=v.elements();
  while(e.hasMoreElements())
  {
  Item item=(Item)e.nextElement();
        
  %>
  <tr>
    <td><input type="checkbox" name="itemId" value="<%=item.getItemId()%>"></td>
    <td><%=item.getDescription()%></td>
    <td><%=item.getPrice()%></td>
    <td><%=item.getAvailable()%></td>
    <td><%=item.getProducer()%></td>
  </tr>
  <%}%>
<tr align=left><td colspan=5 ><input type=submit value="add" name="action"></td></tr>
<tr align=left><td colspan=5><a href="cart.jsp">购物车</a>『』<a href="logout.jsp">注销</a></td></tr>
</table>
</form>
</center>
<%@ include file="tail.jsp"%>
</body>
</html>
这是一个购物车代码的一部分,如果仅仅把上面代码拷贝到tomcat下运行的话,就会出现如上的错误提示,解决方法是:把<%@ include file="header.jsp"%>这句include删除。
原因如下:
解决Tomcat5.5版本中“Attempted a bean operation on a null object”的错误

Tomcat5.5版本运行使用会话级JavaBean时,有时会出现“Attempted a bean operation on a null object”的错误。这种错误一般不会在Tomcat5.0中出现。

解决方法是 在每个使用会话级JavaBean的网页中都显式声明创建JavaBean的JSP指令,该不能不会被执行,因为已经存在相应的 JavaBean,但是可以确保编辑该JSP网页时,对应Servlet会在Session中寻找相应的JavaBean,而不是默认在Request中寻找相应的JavaBean。

如:

在不同JSP网页间共享会话级JavaBean
index.jsp文件:
<%@page contentType="text/html;charset=GBK"%>
<html>
<head>
</head>
<body>
<jsp:useBean id="t1" class="mypack.Time" scope="session" />
<jsp:setProperty name="t1" property="hour" value="13" />
<jsp:setProperty name="t1" property="minute" value="20" />
<jsp:setProperty name="t1" property="second" value="30" />
<a href="result.jsp">点击</a>
</body>
</html>

result.jsp文件:
<%@page contentType="text/html;charset=GBK"%>
<html>
<head>
</head>
<body>
<jsp:useBean id="t1" class="mypack.Time" scope="session" /><!--此语句在Tomcat5.5中必须要有,否则出现上述错误-->
时间为:<jsp:getProperty name="t1" property="time" />
</body>
</html>

(转载自javaeye “小窝”的文章)

你可能感兴趣的:(职场,null,operation,休闲)