Tengo un problema al intentar rellenar un JList en swing, este método es el que utilizo para llenar la lista:
Código:
private void llenarLista(List lista) {
DefaultListModel model = new DefaultListModel();
for(int i=0; i<lista.size(); i++){
model.addElement(lista.get(i));
}
listaExplotaciones.setModel(model);
}
He puesto un botón en la pantalla, y su acción va a ser llenar el JList:
Código:
List lista=miCoordinador.consultarExplotaciones("user2");
llenarLista(lista);
La lista es una consulta que se realiza en la clase objeto, el método es el siguiente:
Código:
public List consultarExplotaciones(String usuario) {
List miLista=null;
Connection connection=null;
Conexion miConexion=new Conexion();
PreparedStatement statement=null;
ResultSet result=null;
ExplotacionesVo miExplotacion=new ExplotacionesVo();
connection=miConexion.getConnection();
String consulta="SELECT * FROM T_EXPLOTACIONES where USUARIO= ?";
try {
if (connection!=null) {
statement=connection.prepareStatement(consulta);
statement.setString(1, usuario);
result=statement.executeQuery();
while(result.next()==true){
miExplotacion=new ExplotacionesVo();
miExplotacion.setUsuario(result.getString("USUARIO"));
miExplotacion.setCodigo(result.getString("CODIGO"));
miExplotacion.setEspecie(result.getString("ESPECIE"));
miExplotacion.setTitular(result.getString("TITULAR"));
miExplotacion.setDireccion(result.getString("DIRECCION"));
miExplotacion.setLocalidad(result.getString("LOCALIDAD"));
miExplotacion.setCodigoPostal(result.getString("CODIGO_POSTAL"));
miExplotacion.setProvincia(result.getString("PROVINCIA"));
miExplotacion.setTelefono(result.getString("TELEFONO"));
miLista.add(miExplotacion);
}
miConexion.desconectar();
}else{
miLista=null;
}
} catch (SQLException e) {
System.out.println("Error en la consulta de la explotación: "+e.getMessage());
}
return miLista;
}
Cuando pulso el botón, me da la siguiente excepción:
Código:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at vista.VentanaExplotaciones.llenarLista(VentanaExplotaciones.java:254)
at vista.VentanaExplotaciones.btnLlenarActionPerformed(VentanaExplotaciones.java:222)
at vista.VentanaExplotaciones.access$000(VentanaExplotaciones.java:15)
at vista.VentanaExplotaciones$2.actionPerformed(VentanaExplotaciones.java:99)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:154)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:182)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:221)
at java.security.AccessController.doPrivileged(Native Method)
at java.awt.WaitDispatchSupport.enter(WaitDispatchSupport.java:219)
at java.awt.Dialog.show(Dialog.java:1082)
at java.awt.Component.show(Component.java:1651)
at java.awt.Component.setVisible(Component.java:1603)
at java.awt.Window.setVisible(Window.java:1014)
at java.awt.Dialog.setVisible(Dialog.java:1005)
at controlador.Coordinador.mostrarVentanaExplotaciones(Coordinador.java:87)
at vista.VentanaInicio.btnExplotacionesActionPerformed(VentanaInicio.java:102)
at vista.VentanaInicio.access$000(VentanaInicio.java:22)
at vista.VentanaInicio$1.actionPerformed(VentanaInicio.java:65)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
La línea 254 es esta:
Código:
model.addElement(lista.get(i));
¿Alguien sabe que es lo que me puede estar fallando?
Muchas gracias de antemano, saludos.