高手进!!Vector问题??
我做在线书店!!
首业程序代码是:
<%request.setCharacterEncoding( "gb2312 ");
if(session.getAttribute( "booklist ")==null){
response.sendRedirect( "/bookshoponline/showbook ");}%>
显示主业信息代码是:
<%
Vector booklist=(Vector)session.getAttribute( "booklist ");
while(booklist.size()> =0){
int i=0;
BookInformation book=(BookInformation)booklist.elementAt(i);%>
<tr>
<td align=center> <%=book.getName()%> </td>
<td align=center> <%=book.getAuthor()%> </td>
<td align=center> <%=book.getPublisher()%> </td>
<td align=center> <%=book.getType()%> </td>
<td align=center> <%=book.getPrice()%> </td>
<%if(session.getAttribute( "userid ")!=null){%>
</tr>
<%i++;}%>
booklist是提交给一个Serlvet响应来的:
代码是:
if(session.getAttribute( "booklist ")!=null){
session.removeAttribute( "booklist ");
}
Vector booklist=new Vector();
//中间为联系数据库以及判断首业提交信息代码,最后把数据放到了booklist里.
其中BookInformation就是普通的JAVABEAN
while(rs.next()){
BookInformation bookinf=new BookInformation(rs.getString( "id "),
rs.getString( "name "),
rs.getString( "author "),
rs.getString( "publisher "),
rs.getString( "price "),
rs.getString( "type "));
booklist.addElement(bookinf);
}
session.setAttribute( "booklist ", booklist);
response.sendRedirect( "/bookshoponline/index.jsp ");
但是响应的数据却报错!!
------解决方案--------------------错误就是在index.jsp中出现了
空指针,你看看你哪个变量引用发生了空指针错误了。我倒是看到另外一个错误:
while(booklist.size()> =0){
int i=0;
BookInformation book=(BookInformation)booklist.elementAt(i);%>
<tr>
<td align=center> <%=book.getName()%> </td>
<td align=center> <%=book.getAuthor()%> </td>
<td align=center> <%=book.getPublisher()%> </td>
<td align=center> <%=book.getType()%> </td>
<td align=center> <%=book.getPrice()%> </td>
<%if(session.getAttribute( "userid ")!=null){%>
</tr>
<%i++;}%>
你这样定义首先你的while循环没有结束条件,另外你的是中取的都是Vector中的第一个元素。