日期:2014-05-17  浏览次数:20648 次

NullPointerException帮我找找错误,纠结死了
Java code

      public BasicInf getBasicInf(String staId){
          BasicInf BasicInf=null;
          try{
              ps = connection.prepareStatement("select * from BasicInf where staId=?");
              ps.setString(1,staId);
              ResultSet rs = ps.executeQuery();
              while (rs.next()) {
                  System.out.println(rs.getString(1));
                  System.out.println(rs.getString(2));
                  System.out.println(rs.getString(3));
                  System.out.println(rs.getString(4));
                  
                  BasicInf.setStaId(rs.getString(1));
                  BasicInf.setBirthday(rs.getString(2));
                  BasicInf.setNation(rs.getString(4));
                  BasicInf.setSex(rs.getString(3));
                  return BasicInf;
              }
          }catch(Exception e)
          {
              e.printStackTrace();
          }
          return BasicInf;
      }


System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(rs.getString(4));正确输出了数据库里的数据
可是到了BasicInf.setStaId(rs.getString(1));就直接catch了,也不知道错在哪里,求大神们帮帮忙看下,分不多笑纳

------解决方案--------------------
你又把BasicInf设置成null了
探讨

改成如下Java code
public BasicInf getBasicInf(String staId){
BasicInf BasicInf=new BasicInf();
try{
ps = connection.prepareStatement("select * from BasicInf where staId……