NullPointerException从方法返回数组

时间:2013-01-18 18:22:03

标签: java mysql arrays

我的数据库上有一个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;
}

我收到错误:

  

线程“main”中的异常java.lang.NullPointerException at   logictesting.mySQLManager.GetDevices(mySQLManager.java:130)at   logictesting.LogicTesting.main(LogicTesting.java:29)Java结果:1

4 个答案:

答案 0 :(得分:4)

问题是Devices位于null

        Devices[index + 0] = resultSet.getString(1);
        Devices[index + 1] = resultSet.getString(2);

最简单的解决方法可能是将Devices变为ArrayList,从那以后动态扩展它会很容易。

public String[] GetDevices(){
    List<String> Devices = new ArrayList<String>();
    String query = "SELECT * FROM devices";
    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        while(resultSet.next()){
            Devices.add(resultSet.getString(1));
            Devices.add(resultSet.getString(2));
        }
    } catch (SQLException ex) {
        System.out.println("\nNão foi possivel ler os dispositivos do database");
        System.out.print(ex.getMessage());

    }
    return Devices.toArray(new String[0]);
}

答案 1 :(得分:2)

在插入元素之前初始化设备数组。

public String[] GetDevices(){
    String[] Devices = new String[size];

如果在插入元素之前不知道数组的大小,那么最好选择动态数组,例如ArrayList

答案 2 :(得分:1)

两个数组(设备当前导致空指针和数组Dispositivos也需要初始化)为空;在使用它们之前初始化数组。

此外,我建议将两个字符串值包装到一个对象中,然后使用此对象的列表 - 代码看起来会更清晰。

答案 3 :(得分:1)

String[] Devices = null;您未初始化设备。并将值插入空Devices[index + 0]抛出NullPointerException。你可以试试这个。因为数组是固定的。

public String[] GetDevices(){
    List<String> Devices = new ArrayList<String>();
    String query = "SELECT * FROM devices";
    try {
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);
        while(resultSet.next()){
            Devices.add(resultSet.getString(1));
            Devices.add(resultSet.getString(2));
        }
    } catch (SQLException ex) {
        System.out.println("\nNão foi possivel ler os dispositivos do database");
        System.out.print(ex.getMessage());

    }
    return Devices.toArray(new String[Devices.size()]);
}