Error I get:
Null pointer access: The userlist variable 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" from my table to display in a JComboBox. With this, you can select one and then show all your data in the Textfield such as your name, surname, password, etc.
I receive the error inside the while (rs.next())
.
Same listadoUsuarios
as null
but, the error is still there.
Any ideas?, or is it better to use ArrayList?.
You have not given a useful value to
listadoUsuarios
. Your code, to the compiler, looks like this:It's best to use a
List<String>
because it can grow dynamically:Also, your method loses usefulness if it is of type
void
and the data returned only lives in the scope of the method. It would be better if the method returns the list of obtained users:Additionally, remember your question Error closing Connection, Statement and ResultSet inside try-catch-finally in Java, cannot be resolved to variable and what the accepted answer explains. Take advantage of the benefits of Java 7 and the
try-with-resources
: