3

I have a problem with my ComboBox, I search everywhere and my code is fine.. But when I execute the program it shows me an error java.lang.NullPointerException

Here's my Code

    package InventarioGUI;

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.sql.SQLException;
    import java.sql.PreparedStatement;
    import java.sql.*;



public class VentanaInventario extends JFrame implements ActionListener
{

    private JComboBox cmbProducto, cmbProveedor;
    ConexionInventario Con = new ConexionInventario();
    private PreparedStatement PST = null;

    DefaultTableModel md;
    JTable tabla;
    Object data [][] = {};
    String cabeza[] = {"Cantidad", "Fecha Entrada", "Precio"};
    JScrollPane scroll;    

   public VentanaInventario()
   {
       super ("Inventario");   

       ComboProducto();
       setLayout(null);

       cmbProducto = new JComboBox();
       cmbProducto.setMaximumRowCount(5);
       cmbProducto.setBounds (120, 10, 150, 20); 
       add(cmbProducto);
    }

    private void ComboProducto(){
        try
        {
           String Sql = "SELECT Nombre_Producto FROM Producto";
           Con.ExeSql(Sql);

            while(Con.RS.next()){
                String pat = Con.RS.getString("Nombre_Producto");
                cmbProveedor.addItem(pat);

            }
       }

        catch(Exception ex){
            JOptionPane.showMessageDialog(null, ex);
        }
    }

   public static void main (String args[])
   {
       try
       {
           VentanaInventario frmVentanaInventario = new VentanaInventario();
           frmVentanaInventario.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frmVentanaInventario.setSize (300, 250);
           frmVentanaInventario.setVisible(true);
        }

        catch (Exception ex)
        {
         JOptionPane.showMessageDialog(null, "Error presentado al realizar operación", " VentanaInventario", JOptionPane.ERROR_MESSAGE);
        }
    }
}

All the connection and everything is in another class and it's fine. Pleeease, help, I have days with this and I don't know what more can I do!

EyMarie
  • 41
  • 1
  • 8

1 Answers1

0

You need to instantiate cmbProveedor before using it in your ComboProducto() method.

Just add the following line:

cmbProveedor = new JComboBox();

You can place this line either in your constructor before the ComboProducto() method call, or add the line inside ComboProducto() before you use cmbProveedor.

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
  • Ignore cmbProveedor I'm only using ComboProducto() has the method and cmbProducto has the name of the ComboBox – EyMarie Apr 26 '15 at 21:38
  • You can't ignore cmbProveedor since you use it by cmbProveedor.addItem(pat); inside ComboProducto(). Every time you create an instance of VentanaInventario(); you call ComboProducto(); since it is the second line of your constructor. Just declaring a variable doesn't instantiate it or actually create it. If you are not using cmbProveedor, then remove that line. Otherwise, you must instantiate it before using it by calling new. You will get a null pointer exception since you use cmbProveedor before actually creating it. – Tot Zam Apr 26 '15 at 23:09