I have a problem, Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

0

I'm doing an application on jframe and when I run it does not connect to my database and this exception comes out:

  

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException     at sistemaventas.salesVer.setFilas (ventaVer.java:43) at   sistemaventas.ventaVer. (ventaVer.java:21) at   sistemaventas.venta.verVentaActionPerformed (venta.java:157) at   sistemaventas.venta.access $ 200 (venta.java:9) at   sistemaventas.venta $ 3.actionPerformed (venta.java:99) at   javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:2022)     at   javax.swing.AbstractButton $ Handler.actionPerformed (AbstractButton.java:2348)     at   javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)     at   javax.swing.DefaultButtonModel.setPressed (DefaultButtonModel.java:259)     at javax.swing.AbstractButton.doClick (AbstractButton.java:376) at   javax.swing.plaf.basic.BasicMenuItemUI.doClick (BasicMenuItemUI.java:833)     at   javax.swing.plaf.basic.BasicMenuItemUI $ Handler.mouseReleased (BasicMenuItemUI.java:877)     at java.awt.Component.processMouseEvent (Component.java:6533) at   javax.swing.JComponent.processMouseEvent (JComponent.java:3324) at   java.awt.Component.processEvent (Component.java:6298) at   java.awt.Container.processEvent (Container.java:2236) at   java.awt.Component.dispatchEventImpl (Component.java:4889) at   java.awt.Container.dispatchEventImpl (Container.java:2294) at   java.awt.Component.dispatchEvent (Component.java:4711) at   java.awt.LightweightDispatcher.retargetMouseEvent (Container.java:4888)     at   java.awt.LightweightDispatcher.processMouseEvent (Container.java:4525)     at java.awt.LightweightDispatcher.dispatchEvent (Container.java:4466)     at java.awt.Container.dispatchEventImpl (Container.java:2280) at   java.awt.Window.dispatchEventImpl (Window.java:2746) at   java.awt.Component.dispatchEvent (Component.java:4711) at   java.awt.EventQueue.dispatchEventImpl (EventQueue.java:758) at   java.awt.EventQueue.access $ 500 (EventQueue.java:97) at   java.awt.EventQueue $ 3.run (EventQueue.java:709) at   java.awt.EventQueue $ 3.run (EventQueue.java:703) at   java.security.AccessController.doPrivileged (Native Method) at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80)     at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:90)     at java.awt.EventQueue $ 4.run (EventQueue.java:731) at   java.awt.EventQueue $ 4.run (EventQueue.java:729) at   java.security.AccessController.doPrivileged (Native Method) at   java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege (ProtectionDomain.java:80)     at java.awt.EventQueue.dispatchEvent (EventQueue.java:728) at   java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:201)     at   java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:116)     at   java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:105)     at   java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:101)     at   java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:93)     at java.awt.EventDispatchThread.run (EventDispatchThread.java:82)

and this is my code:

package sistemaventas;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class ventaVer extends javax.swing.JFrame {

    DefaultTableModel modeloTabla;
    conector.conector con = new conector.conector();
    public ventaVer() {
        modeloTabla = new DefaultTableModel(null, GetColumnas());
        setFilas();
        initComponents();
        jTable1.setDefaultEditor(Object.class, null);
        total.setEditable(false);

    }

    private String[] GetColumnas() {
        String columna[] = new String[]{"Id","Articulo","Nombre","Cantidad","Precio","Fecha" };
        return columna;
    }

    private void setFilas()
    {

        if (modeloTabla.getRowCount() > 0) {
            for (int i = modeloTabla.getRowCount() - 1; i > -1; i--) {
                modeloTabla.removeRow(i);
            }
        }
             try {
        String sql = "SELECT VEN.Id, VEN.Articulo, ART.Nombre , VEN.Cantidad, VEN.Precio, VEN.Fecha FROM 'ventas' VEN JOIN articulos art ON (art.Id = Articulo) Order by VEN.Id";
        PreparedStatement us = con.conector().prepareStatement(sql);
        ResultSet res = us.executeQuery();


        Object datos[] = new Object[6];

        while (res.next()) {
            for (int i = 0; i < 6; i++) {
                datos[i] = res.getObject(i + 1);
            }
            modeloTabla.addRow(datos);

       }

        res.close();

        } catch (SQLException ex)
        {

        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        borrar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        fecha = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        total = new javax.swing.JTextPane();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        desde = new org.jdesktop.swingx.JXDatePicker();
        hasta = new org.jdesktop.swingx.JXDatePicker();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(modeloTabla);
        jScrollPane1.setViewportView(jTable1);

        borrar.setText("Eliminar venta");
        borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                borrarActionPerformed(evt);
            }
        });

        jButton3.setText("Actualizar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel1.setText("Sus ventas");

        fecha.setText("Buscar por fecha");
        fecha.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fechaActionPerformed(evt);
            }
        });

        jLabel2.setText("Desde:");

        jLabel3.setText("Hasta:");

        jLabel4.setText("Total fecha");

        jScrollPane2.setViewportView(total);

        jLabel5.setText("$");

        jSeparator1.setForeground(new java.awt.Color(204, 204, 204));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addComponent(jLabel5)
                                .addGap(3, 3, 3)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 539, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(borrar, javax.swing.GroupLayout.DEFAULT_SIZE, 132, Short.MAX_VALUE)
                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(26, 26, 26))
            .addGroup(layout.createSequentialGroup()
                .addGap(388, 388, 388)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 364, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(desde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(11, 11, 11)
                            .addComponent(jLabel3)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(hasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(13, 13, 13)
                            .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(26, 26, 26)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 4, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(30, 30, 30)
                            .addComponent(jLabel4)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jScrollPane2))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(borrar, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void borrarActionPerformed(java.awt.event.ActionEvent evt) {                                       

        int reply = JOptionPane.showConfirmDialog(rootPane, "¿Está seguro de eliminar esta venta?", "Eliminar venta", JOptionPane.YES_NO_OPTION);

        if (reply == JOptionPane.YES_OPTION) {

            int row = jTable1.getSelectedRow();
            String value = jTable1.getModel().getValueAt(row, 0).toString();
            try
            {
                String del = "DELETE from ventas where id = ?";
                PreparedStatement stat = con.conector().prepareStatement(del);
                stat.setString(1, value);
                stat.executeUpdate();

            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,
                    "No se pudo eliminar",
                    "Error",
                    JOptionPane.WARNING_MESSAGE);
            }
            JOptionPane.showMessageDialog(null, "Venta eliminada", "Éxito al borrar", JOptionPane.INFORMATION_MESSAGE);
        }

        setFilas();
    }                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        setFilas();
    }                                        


    private void EliminarTodasLasFilas(DefaultTableModel dtm)
    {
        if (dtm.getRowCount() > 0) {
            for (int i = dtm.getRowCount() - 1; i > -1; i--) {
                dtm.removeRow(i);
            }
        }

    }
    public void totalFecha(){
           try {


            java.util.Date desdeD = desde.getDate();
            DateFormat dDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dDate = dDateFormat.format(desdeD);

           java.util.Date hastaH = hasta.getDate();
           DateFormat hDateFormat = new SimpleDateFormat("yyyy-MM-dd");
           String hzDate = hDateFormat.format(hastaH);

String sql = "SELECT SUM(VEN.Cantidad * VEN.Precio) \n" +

"FROM 'ventas' VEN \n" +
//"JOIN articulos ART ON (ART.Id = VEN.articulo)\n" +
"WHERE CAST(VEN.Fecha as DATE) BETWEEN '"+dDate+"' AND '"+hzDate+"'";
            PreparedStatement us;
            us = con.conector().prepareStatement(sql);
            ResultSet res = us.executeQuery();
            //String resultado = res.getObject(0).toString();


          Object datos[] = new Object[1];
            while (res.next()) {
                for (int i = 0; i < 1; i++) {
                datos[i] = res.getObject(i + 1);
                //JOptionPane.showMessageDialog(null, "El total entre el "+ dDate +" y el "+ hzDate +" es: "+datos[i].toString(),"total",JOptionPane.INFORMATION_MESSAGE);
                total.setText(datos[i].toString());
                }
                //modeloTabla.addRow(datos);

            }

         res.close();
          } catch (SQLException ex) {
            Logger.getLogger(ventaVer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void fechaActionPerformed(java.awt.event.ActionEvent evt) {                                      


        try {

            java.util.Date desdeD = desde.getDate();
            DateFormat dDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String dDate = dDateFormat.format(desdeD);

           java.util.Date hastaH = hasta.getDate();
           DateFormat hDateFormat = new SimpleDateFormat("yyyy-MM-dd");
           String hzDate = hDateFormat.format(hastaH);

String sql = "SELECT VEN.Id, VEN.Articulo, art.Nombre, VEN.Cantidad, VEN.Precio,VEN.Fecha FROM 'ventas' VEN JOIN articulos art ON (art.Id = Articulo) WHERE CAST(VEN.Fecha as DATE) BETWEEN '"+dDate+"' AND '"+hzDate+"' ORDER BY VEN.Id";
            PreparedStatement us;
            us = con.conector().prepareStatement(sql);
            ResultSet res = us.executeQuery();


          Object datos[] = new Object[6];
          EliminarTodasLasFilas(modeloTabla);
            while (res.next()) {
                for (int i = 0; i < 6; i++) {
                    datos[i] = res.getObject(i + 1);
                }
                modeloTabla.addRow(datos);
            }

         res.close();
         totalFecha();   

        } catch (SQLException ex) {
            Logger.getLogger(ventaVer.class.getName()).log(Level.SEVERE, null, ex);
        }

    }                                     

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ventaVer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ventaVer().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton borrar;
    private org.jdesktop.swingx.JXDatePicker desde;
    private javax.swing.JButton fecha;
    private org.jdesktop.swingx.JXDatePicker hasta;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextPane total;
    // End of variables declaration                   
}
    
asked by Hernan 22.05.2018 в 00:31
source

0 answers