永发信息网

JDBC查询空指针 代码如下

答案:2  悬赏:80  手机版
解决时间 2021-02-07 05:02
  • 提问者网友:寂寞梧桐
  • 2021-02-06 21:01
public Hero query(int id){
sql="select * from hero where id="+id;
con=getCon();
Hero hero = new Hero();
try {
st =con.createStatement();
rs = st.executeQuery(sql);
if(rs.next()){
hero.setName(rs.getString("name"));
hero.setPower(rs.getInt("power"));
hero.setMh(rs.getInt("mh"));
hero.setPh(rs.getInt("ph"));
hero.setRaceId(rs.getInt("raceId"));
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{

closeAll(rs, st, con);

}return hero;

}
最佳答案
  • 五星知识达人网友:傲气稳了全场
  • 2021-02-06 22:23
你不说哪里出了空指针?光看这些代码,getCon()方法可能返回null导致st =con.createStatement();异常
全部回答
  • 1楼网友:woshuo
  • 2021-02-06 22:43
将computer c = null;改为computer c = new computer();即可
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯