我的数据库上有一个 2x2 表(2 列和 2 行填充),我试图将整个表放到我的数组中,但是当我尝试在控制台上打印结果时,我得到NullPointerException。我无法弄清楚我错过了什么。
主要代码:
public static void main(String[] args) {
mySQLManager Manager = new mySQLManager();
Manager.Connect();
String[] Dispositivos = null;
Dispositivos = Manager.GetDevices();
for (int i = 0; i < Dispositivos.length; i++){
System.out.println(Dispositivos[i]);
}
Manager.Disconnect();
}
方法:
public String[] GetDevices(){
String[] Devices = null;
String query = "SELECT * FROM devices";
int index = 0;
try {
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
while(resultSet.next()){
Devices[index + 0] = resultSet.getString(1);
Devices[index + 1] = resultSet.getString(2);
index+=2;
}
} catch (SQLException ex) {
System.out.println("\nNão foi possivel ler os dispositivos do database");
System.out.print(ex.getMessage());
}
return Devices;
}
我得到的错误:
在 logictesting.LogicTesting.main(LogicTesting.java:29) Java 结果中的 logictesting.mySQLManager.GetDevices(mySQLManager.java:130) 线程“main”java.lang.NullPointerException 中的异常结果:1