Java - Problema NullPointExcewption

 
Vista:
sin imagen de perfil

Problema NullPointExcewption

Publicado por Sergi (2 intervenciones) el 26/09/2016 12:38:05
Buenas, tengo 2 clases, en una tengo un metodo y en la otra llamo dicho metodo al pulsar un boton, el problema es que me da un nullPointException, no se porque ya que lo inicializo todo, y el metodo va bien ya que si lo llamo dentro de la misma clase funciona, pongo el codigo de las 2 clases:

Clase A: El metodo que necesito es actualizarTablaTickets()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
public class accionTickets {
 
private DefaultTableModel tablaTickets;// = new DefaultTableModel(new String[]{"ID","FECHA","IMPORTE","ELIMINAR"},0);
public V_frame v_frame;
public bdtickets bdt;
public Vector tickets;
public ResultSet resultado;
 
    public accionTickets(V_frame principal) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        bdt = new bdtickets();
        this.v_frame=principal;
        this.cargarTickets();
        try {
            this.llenarTickets();
        } catch (InstantiationException ex) {
            Logger.getLogger(accionTickets.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(accionTickets.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.actualizarTablaTickets();
    }
 
    public accionTickets() {
 
    }
 
    public void cargarTickets()
    {
        this.tablaTickets = new DefaultTableModel(new String[]{"ID","FECHA","IMPORTE","ELIMINAR"},0);
        v_frame.getTblTickets().setModel(tablaTickets);
 
    }
    public void llenarTickets() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
    {
        ResultSet resultado=null;
        String nombreBoton;
 
        resultado = bdt.consultarTickets();
 
        while (resultado.next())
        {
            nombreBoton = resultado.getString("ID");
            JButton BtEliminar = new JButton(nombreBoton);
            tickets = new Vector();
            tickets.addElement(resultado.getString("ID"));
            tickets.addElement(resultado.getString("FECHA"));
            tickets.addElement(resultado.getString("IMPORTE"));
            tickets.addElement(BtEliminar);
            tablaTickets.addRow(tickets);
        }
    } //ESTE ES EL METODO QUE NECESITO LLAMAR EN LA CLASE B
    public void actualizarTablaTickets() throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException
       {
        int tamañoTabla = tablaTickets.getRowCount();
        {
            for (int i = tamañoTabla-1; i >=0; i--)
            {
                System.out.print(i+" ");
                tablaTickets.removeRow(i);
            }
        }
        llenarTickets();
    }
 
}

CLASE B;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import verduleria.aplicacion.V_frame;
import verduleria.bdprincipal.bdtickets;
import verduleria.controlador.accionTickets;
 
public class TicketDiario extends javax.swing.JFrame {
 
    /**
     * Creates new form TicketDiario
     */
    private V_frame principal;
    private bdtickets bd;
    public accionTickets acciontickets;
 
    public TicketDiario() {
        initComponents();
        this.setLocationRelativeTo(null);
    }
 
private void JBGuardarTicketActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            acciontickets = new accionTickets();
 
            bd = new bdtickets();
            String importe = JTImporte.getText();
            String fecha = JTFecha.getText();
            bd.InsertarTicket(fecha, importe);
            acciontickets.actualizarTablaTickets();//AQUI LLAMO AL METODO
 
        } catch (SQLException | ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(TicketDiario.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
 
}

Cuando llamo al metodo me da el null pint exception, las primeras linias me da el error en int tamañoTabla = tablaTickets.getRowCount(); de la clase A y despues en la llamada del metodo en la clase B.

A ver si alguien me puede ayudar, estoy ofuscado xD
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Problema NullPointExcewption

Publicado por YYanes (144 intervenciones) el 26/09/2016 18:11:11
Me llama la atención que en el método "LlenarTickets" el objeto "tickets" lo declaras de tipo "Vector", ¿no debería ser de tipo "Row"?

Este es un ejemplo de la forma en que usualmente yo agrego nuevas filas a una tabla:

1
2
3
4
5
6
7
8
DefaultTableModel tabla = (DefaultTableModel) jTableUds.getModel();
tabla.setNumRows(0); //con esto se limpia completamente la información de la tabla (filas) sin alterar su diseño ni columnas
 
//Llenando la tabla que tiene 2 columnas: "Id" y "Nombre"
for (int i = 0; i < ListaUnidades.size(); i++) {
    Object[] row = {ListaUnidades.get(i).GetId(), ListaUnidades.get(i).GetNombre()};
    tabla.addRow(row);
}

pruébalo si quieres de esa manera, a ver si te resulta
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problema NullPointExcewption

Publicado por Sergi (2 intervenciones) el 26/09/2016 21:46:04
En principio el metodo llenarTickets no da el fallo, me da el al pedir la cuenta de hileras de la tabla.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar