求前辈解决一下
空指针异常的问题哈!!谢谢
@SuppressWarnings("unused")
@Override
public int compair(String username) {
Vector<String> items = new Vector<String>();
Connection conn;
PreparedStatement ps = null;
String[] name = null;
try {
conn = DBUtil.getConnection();
String strSql = "select 用户名 from infom";
ps = conn.prepareStatement(strSql);
ps.execute();// 执行语句
ResultSet rs = ps.getResultSet();// 获得结果
while (rs.next()) {
items.add(rs.getString(1));
//System.out.println("=9999999========" + username);
for (int i = 0; i < name.length; i++) {
if (name[i].equals(Integer.parseInt(username)))
//System.out.println("======222222222" + name[i]);
return i + 1;
}
return -1;
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
------解决方案--------------------name 没有初始化,肯定报
空指针异常. 看代码是否不需要这个数组,用item.
------解决方案--------------------if (name[i].equals(Integer.parseInt(username)))
出现Null指针异常,对于数组name不仅需要初始化name,还需初始化每个元素,如
name = new String[5];
name[0] = "1";
name[1] = "2";
...