I have a method that allows me to search for all the records of my table that have that name and also, in it I create the DefaultTableModel
and also return it. This in order to fill the JTable with updated records.
When running the program I get the error NullPointerException
.
EDIT
My method in my Logic_user class:
public class User_Logic {
private ConexionMySQL MySQL = new ConexionMySQL();
private Connection con = MySQL.conectarMySQL();
private String sSQL = "";
public int totalUsuarios = 0;
public DefaultTableModel buscarRegistro(String buscar) {
DefaultTableModel model = null;
String [] titulosColumnas = {"ID", "Nombre", "Apellido", "Usuario", "Contraseña", "ID Perfil"};
Object [][] registros = new Object[6][6];//6 fila por 6 columnas
sSQL = "SELECT * FROM usuario WHERE nombre LIKE '%" + buscar + "%' ORDER BY id_usuario";
// Java 7 try-with-resources
try (PreparedStatement pstm = con.prepareStatement(sSQL);
ResultSet rs = pstm.executeQuery(sSQL)) {
int i = 0;
while (rs.next()) {
registros[i][0] = rs.getInt("id_usuario");
registros[i][1] = rs.getString("nombre");
registros[i][2] = rs.getString("apellido");
registros[i][3] = rs.getString("usuario");
registros[i][4] = rs.getString("contrasena");
registros[i][5] = rs.getInt("id_perfil");
totalUsuarios++;
i++;
}
model = new DefaultTableModel(registros, titulosColumnas);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "SQLException:\n" + e, "Error: mostrarRegistros(String buscar)", JOptionPane.ERROR_MESSAGE);
}
return model;
}
My procedure in my Window_User class:
void buscarRegistro(String buscar) {
try {
logica_usuario = new Logica_usuario();
DefaultTableModel model;
model = logica_usuario.buscarRegistro(buscar);
table.setModel(model);
setColumnsWidth();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception:\n" + e, "Error: mostrar(String buscar)", JOptionPane.ERROR_MESSAGE);
}
}
When initializing the Frame of my JPanel, I send " "
it to load the JTable with the column titles for the first time:
public Ventana_Usuario() {
setTitle("Mantenimiento de Usuarios");
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 634, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
buscarRegistro("");
But, when starting the program I get a NullPointerException
. At least when you start the program and perform a search, the results are displayed in the JTable, although the column titles do not appear.
EDIT: add StackTrace
StackTrace
java.lang.NullPointerException
at ventanas.Ventana_Usuario.buscarRegistro(Ventana_Usuario.java:66)
at ventanas.Ventana_Usuario.<init>(Ventana_Usuario.java:110)
at ventanas.Ventana_Usuario$1.run(Ventana_Usuario.java:87)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Window_User.java:66
table.setModel(model);
Window_User.java:110
buscarRegistro("");
Window_User.java:87)
Ventana_Usuario frame = new Ventana_Usuario();
I have no idea why, I hope you can help me, thanks for everything in advance.
You have poorly implemented your table. Try this way:
And then the way to present it with him
JScrollPane
would be:In this way you will see the headers, you will have
int
in the columns that they returnint
and we have changed the "magic numbers" for variables.