你的程序中某处对值为null的对象进行了某些操作

解决方案 »

  1.   

    这是源文件,谢谢指教!
    <%@ page contentType="text/html;charset=gb2312"%>
    <%@ page import="java.sql.*"%><html>
    <head>
    <title>查询结果</title>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head><body bgcolor="#CCCCCC" text="#000000">
    <div align="center">
    <p>&nbsp;</p>
    <p> <font size="4"> <b> 成绩查询结果 </b></font></p>
    <p>&nbsp;</p>
    <p>
    <font size="2"><b>
    <%
    int n;
    String temp1=new String();
    String temp2=new String();
    temp1=request.getParameter("txtname");
    temp2=request.getParameter("txtpassword");
    n=temp1.length()*temp1.length();
    %>
    <%
    if(n==0){
    %>
    <p>2请输入正确的姓名和密码。</p>
    <p><a href="student.htm">返回</a></p>
    <%
    }
    else{
    String tempname=new String();
    String tempclass=new String();
    String tempsex=new String();
    String tempage=new String();
    String tempnum=new String();
    String tempenglish=new String();
    String tempchinese=new String();
    String tempmath=new String();
    String tempsum=new String();
    String temporder=new String();
    String url=new String();
    url="jdbc:odbc:student";
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:student");
    java.sql.Statement stmt=con.createStatement();
    String SQL=new String();
    %>
    <%
    SQL="SELECT * FROM stu WHERE name='"+temp1+"'and password='"+temp2+"'";
    ResultSet rs=stmt.executeQuery(SQL);
    %>
    <%
    if(rs.next()){
    tempname=rs.getString("name");
    tempclass=rs.getString("class");
    tempnum=rs.getString("number");
    tempsex=rs.getString("sex");
    tempage=rs.getString("age");
    rs.close();
    %>
    <% 
    SQL="SELECT * FROM res WHERE number="+tempnum;
    ResultSet rs1=stmt.executeQuery(SQL);
    if(rs1.next()){
    tempenglish=rs1.getString("english");
    tempchinese=rs1.getString("chinese");
    tempmath=rs1.getString("math");
    tempsum=rs1.getString("sum");
    temporder=rs1.getString("order");
    }
    rs1.close();
    %>
    <table width="600" border="2"
    cellspacing="2" cellpadding="2" bordercolorlight="#000000"
    bordercolordark="#000000">
    <tr bordercoler=#000000">
    <td height="30" width="100">
    姓名:</td>
    <td height="30" width="200">
    <%=tempname%></td>
    <td height="30" width="100">
    学号:</td>
    <td height="30" width="200">
    <%=tempnum%></td>
    </tr>
    <tr bordercoler=#000000">
    <td height="30" width="100">
    性别:</td>
    <td height="30" width="200">
    <%=tempsex%></td>
    <td height="30" width="100">
    年龄:</td>
    <td height="30" width="200">
    <%=tempage%></td>
    </tr>
    <tr bordercoler=#000000">
    <td height="31" width="100">
    班级:</td>
    <td height="31" width="200">
    <%=tempclass%></td>
    <td height="31" width="100">&nbsp;</td>
    <td height="31" width="200">&nbsp;</td>
    </tr>
    <tr bordercoler=#000000">
    <td height="30" width="100">
    英语:</td>
    <td height="30" width="200">
    <%=tempenglish%></td>
    <td height="30" width="100">
    语文:</td>
    <td height="30" width="200">
    <%=tempchinese%></td>
    </tr>
    <tr bordercoler=#000000">
    <td height="30" width="100">
    数学:</td>
    <td height="30" width="200">
    <%=tempmath%></td>
    </tr>
    <tr bordercoler=#000000">
    <td height="30" width="100">
    总分:</td>
    <td height="30" width="200">
    <%=tempsum%></td>
    <td height="30" width="100">
    排名:</td>
    <td height="30" width="200">
    <%=temporder%></td>
    </tr>
    </table>
    <%}
    else{%>
    <p>请正确输入姓名和密码。</p>
    <p><a href="student.htm">
    返回</a></p>
    <%
    }
    stmt.close();
    con.close();
    }
    %>
    </b>
    </font>
    </div>
    </body>
    </html>
      

  2.   

    问题多多,先指出一个:
    String temp1=new String();
    String temp2=new String();
    temp1=request.getParameter("txtname");
    temp2=request.getParameter("txtpassword");
    n=temp1.length()*temp1.length();这里需要在对temp1和temp2赋值之前判断是否为null,否则当为null时下面的length方法就会出现空指针错误。
      

  3.   

    你可能是这里出错了。
    SQL="SELECT * FROM res WHERE number="+tempnum;
    ResultSet rs1=stmt.executeQuery(SQL);
    if(rs1.next()){
    tempenglish=rs1.getString("english");
    tempchinese=rs1.getString("chinese");
    tempmath=rs1.getString("math");
    tempsum=rs1.getString("sum");
    temporder=rs1.getString("order");
    }
    不要用
    select * from.....
    直接写出字段名比较好。
      

  4.   

    在做getString("")后,应判断取得的值是否为null,不然,如果你对这个null值进行操作,就会出现这种空指针错误。
    也许以后经常要碰到。切记!