Null pointer access when using a variable type String [] within a try-catch

1

Error I receive:

  

Null pointer access: The variable listUsers can only be null at   this location

Variable type String []:

String [] listadoUsuarios = null;

Code:

public void mostrarUsuariosExistentes() {
        sSQL =  "SELECT id_usuario FROM usuarios";

        String [] listadoUsuarios = null;
        int numFila = 0;
        PreparedStatement pstm = null;
        ResultSet rs = null;

        try {
            pstm = conn.prepareStatement(sSQL);

            rs = pstm.executeQuery(sSQL);

            while (rs.next()) {
                listadoUsuarios[numFila] = rs.getString("usuario");
                numFila++;
            }

        } catch (SQLException errorSQL) { errorSQL.printStackTrace(); }
        finally { // Cerramos las conexiones, en orden inverso a su apertura
        try { if (rs != null) rs.close(); } catch (Exception errorPSTM) { errorPSTM.printStackTrace(); }
        try { if (pstm != null) pstm.close(); } catch (Exception errorPSTM) { errorPSTM.printStackTrace(); }
        try { if (conn != null) conn.close(); } catch (Exception errorCONN) { errorCONN.printStackTrace(); }
        }
    }

What I'm trying to do is save all the login "users" of my table to show them in a JComboBox. With this you can select one and then show all your data in Textfield as your name, surname, password, etc. ..

I receive the error within while (rs.next()) .

Equal listadoUsuarios as null but, the error is still there.

Any ideas, or is it better to use ArrayList?

    
asked by Robert Gomez 29.11.2016 в 05:19
source

1 answer

5

You have not given a useful value to listadoUsuarios . Your code, for the compiler, looks like this:

String [] listadoUsuarios = null;
listadoUsuarios[0] = "algo";
//No tiene sentido

It is best to use a List<String> because it can grow dynamically:

List<String> listadoUsuarios = new ArrayList<>();
//resto del código hasta la lectura de datos del ResultSet ...
while (rs.next()) {
    listadoUsuarios.add(rs.getString("usuario"));
}

In addition, your method loses utility if it is of type void and the data obtained only live in the scope of the method. It would be better if the method returns the list of users obtained:

public List<String> obtenerUsuariosExistentes() {
    List<String> listadoUsuarios = new ArrayList<>();
    //código para llenar la lista con la consulta a base de datos...
    return listadoUsuarios;
}

//en otro método, en otra capa, muestras los usuarios
public void mostrarUsuariosExistentes() {
    List<String> listadoUsuarios = obtenerUsuariosExistentes();
    listadoUsuarios.forEach(System.out::println);
}

Additionally, remember your question Error closing Connection, Statement, and ResultSet within try-catch-finally in Java, do not You can solve as a variable and what explains the accepted answer. Take advantage of the benefits of Java 7 and the try-with-resources :

public List<String> obtenerUsuariosExistentes() {
    List<String> listadoUsuarios = new ArrayList<>();
    try (PreparedStatement pstmt = conn.prepareStatement(sSQL);
         ResultSet rs = pstm.executeQuery(sSQL)) {
        while (rs.next()) {
            listadoUsuarios.add(rs.getString("usuario"));
        }
    } catch (SQLException e) {
        //maneja la excepción
        //manejo muy elemental
        e.printStackTrace();
    }
    //código para llenar la lista con la consulta a base de datos...
    return listadoUsuarios;
}
    
answered by 29.11.2016 / 05:28
source