get之前判断取出的对象是否为null

解决方案 »

  1.   

    <%@ page contentType="text/html; charset=GBK" %>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ page import ="java.util.*"%>
    <%@ page import ="questionbasesystem.*"%>
    <%
       //获取题型分类的信息以及试题的基本信息集合
        ArrayList QTypeList = (ArrayList)session.getAttribute(Constants.QUESTIONTIXING_LIST_KEY);
        ArrayList QuestionList=(ArrayList)session.getAttribute(Constants.QUESTION_LIST_KEY);
        Question question = null;
        QuestionType QType= null;
        SearchStatus schSta = (SearchStatus)session.getAttribute(Constants.QUESTION_SEARCH_STATUS_KEY);
    %>
    <html>
    <head>
    <title>
    SearchBook
    </title>
    <META http-equiv=Content-Type content="text/html; charset=gb2312">
    </head>
    <body bgcolor="#ffffff">
    <h1>
    <TABLE cellpadding=0 cellspacing=0 width="788" border="0">
     <TR valign="top" bgcolor="#cbd7f3">
       <TD align="center"  width=170  bgcolor=#7387B8 >
       <br><br>
       <TABLE cellpadding="0" cellspacing="0" width="90%" align="center" border="0">
        <TR><TD bgcolor="#FFFFFF">
        <%//循环显示题型分类信息
        if(QTypeList !=null)
        {
          for(int i = 0; i<QTypeList.size();i++)
         {
            QType = (QuestionType)QTypeList.get(i);
            %>
         
         <% }%>   <% }%>
        </TD>
        </TR>
       </TABLE>
       </TD>
       <TD bgcolor="#7387B8">
       <br>   <TABLE cellpadding="0" cellspacing="0" width="90%" align="center" border="0" bgcolor="#fffff">
       <TR><TD>
       <FORM action="QuestionSearchAction.do" method="POST">
       <DIV align="center"><font size="2">请输入关键字:<font>
       <INPUT type="hidden" name=mode value="2">
          <select class="button1" name="field">
             <option value="1" selected="selected">题型</option>
             <option value="2" selected="selected">关键字</option>
          </select>
          <INPUT type="submit" value=搜索 name="Submit">
          <br><br></DIV></FORM>
       </TD></TR>
       <TR><TD width="50%" height=20>&nbsp;&nbsp;试题目录:</TD></TR>   <TR><TD align=right width="48" height="10">共查到
       <%//=schSta.getQuestionCount()%>条记录,显示第<%//=schSta.getPageId()+1%>页/总
       <%//=schSta.getPageCount()%>页</TD></TR>   <TR><TD>
       <table class="txt" cellpadding="1" cellspacing="1" width="515"
       align="center" bgcolor="#005b4b" border="0">
       <tr bgcolor="#bab4a5">
         <td valign="top" width="197" bgcolor="#0099cc" align="center"><b>
         <font color="#ffffff">试题内容</font></b></td>
         <td valign="top" width="40" bgcolor="#0099cc" align="center"><b>
         <font color="#ffffff">题型</font></b></td>
          <td valign="top" width="40" bgcolor="#0099cc" align="center"><b>
         <font color="#ffffff">分值</font></b></td>
          <td valign="top" width="40" bgcolor="#0099cc" align="center"><b>
         <font color="#ffffff">出题人</font></b></td>
         <td valign="top" width="40" bgcolor="#0099cc" align="center"><b>
         <font color="#ffffff">详细</font></b></td>
         </tr>
         <%//循环显示对应试题信息
         if(QuestionList!=null)
         {
         for(int i = 0; i<QuestionList.size();i++)
         {
             question = (Question)QuestionList.get(i);
             if(question!=null)
             {
           %>
           <tr bgcolor="#ffffff">
           <!--只要question.get***就错-->
             <td width="197 " bgcolor="#ffffff"><%=question.getNeirong()%>}%></td>
             <td width="40 " bgcolor="#ffffff"><%=question.getTixing()%></td>
             <td width="40 " bgcolor="#ffffff"><%=question.getFenzhi()%></td>
             <td width="40 " bgcolor="#ffffff"><%=question.getTeacherid()%></td>
            <% }}%>
            </tr>
         <% }%>
         </table>
         <p><TR>
         <TD align="right">|
           <font color="#999999">
         <a href="questionpage.do?pageId=<%=0%>">首页</a></font>|
         <font color="#999999">
         <a href="questionpage.do?pageId=<%=schSta.getPageId()+1%>">上页</a></font>|
          <font color="#999999">
         <a href="questionpage.do?pageId=<%=schSta.getPageId()-1%>">下页</a></font></TD>
          </TR>
          <br>
          <br>
       </TD>
     </TR>
    </TABLE>
    </h1>
    </body>
    </html>
      

  2.   

    我的一本书中说这个错是因为没有获取到控制层传递来的对应属性的值。如果我不用动态的actionform,哪位大师可以解释一下调用对应参数原理和方法吗?
      

  3.   

    <td width="197 " bgcolor="#ffffff"><%=question.getNeirong()%>}%></td>这里已经结束了if(question!=null)的判断,后面的
    <td width="40 " bgcolor="#ffffff"><%=question.getTixing()%></td>
    <td width="40 " bgcolor="#ffffff"><%=question.getFenzhi()%></td>
    <td width="40 " bgcolor="#ffffff"><%=question.getTeacherid()%></td>
    在question为null的情况下也会执行,所以当question为null时就报空指针异常了,楼主知道怎么改了吧?
      

  4.   

    感觉你不是在用struts,而是在用纯jsp在开发
      

  5.   

    起码你的页面没有用到struts的标签库,放着那里只是摆设,呵呵
      

  6.   

    因为是初学,那些标签有什么好处?我参照的例子就是这么写的,我现在就了解Actionform,action,struts配置文件
      

  7.   

    把错误信息贴出来,另建议你找本好点的struts的书看看
      

  8.   

    你从session里取到的arrayList对象本身就是null或者根本没有取到.
    因为:
    1。arrayList对象 中有值且全部为null 通过for 取值 结果均为null
    2。arrayList对象 无值 通过for 取值 无结果 arrayList.get(i);会出现java.lang.IndexOutOfBoundsException 异常。
    所以只要arrayList对象不为null 根本不可能出现 空指针异常。请好好检查你取arrayList对象 的过程。
      

  9.   

    HTTP Status 500 - type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:370)
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    root cause java.lang.NullPointerException
     org.apache.jsp.SearchBook_jsp._jspService(org.apache.jsp.SearchBook_jsp:186)
     org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
     org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
     org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
     org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.