gpt4 book ai didi

java - 为什么我会从此 Java 代码中得到空指针异常?

转载 作者:行者123 更新时间:2023-11-29 07:23:00 26 4
gpt4 key购买 nike

<分区>

我有一个方法......我找不到错误:

    public String getUsernameforID(int id) {
String statment = "SELECT USERNAME FROM `BENUTZER` WHERE `ID` = ? ;";
String username = null;
try {
PreparedStatement ps = dbCommunicator.getStatment(statment); // HERE : NULL POINTER EXECTION
ps.setInt(1, id);
ResultSet rs = dbCommunicator.readFromDB(ps);

if (rs.first()) {
username = rs.getString("USERNAME");
}
} catch (SQLException ex) {
Logger.getLogger(DBManager.class.getName()).log(Level.SEVERE, null, ex);
}
return username;

我认为这是声明......但我如何才能找到它?我收到空指针异常。

编辑:我的 getStatment 方法:

    public PreparedStatement getStatment(String st) {
connect();
PreparedStatement ps = null;
try {
ps = (PreparedStatement) connection.prepareStatement(st);
} catch (SQLException ex) {
Logger.getLogger(DBCommunicator.class.getName()).log(Level.SEVERE, null, ex);
}
return ps;
}

异常:

Exception in thread "main" java.lang.NullPointerException
at test.DBCommunicator.getStatment(DBCommunicator.java:107)
at test.database.DBManager.getUsernameforID(DBManager.java:359)
at dbtestdrive.Main.main(Main.java:25)

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com