ComboBox in java doesn't work bluej
I have a problem with my ComboBox, I search everywhere and my code is fine. But when I run the program it shows me java.lang.NullPointerException error
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 mix and everything is in a different class and everything is fine. Pleeease, help, I have days with this and I don't know what else I can do!
source to share
You need to create an instance cmbProveedor
before using it in a method ComboProducto()
.
Just add the following line:
cmbProveedor = new JComboBox();
You can either put this line in your constructor before calling the method ComboProducto()
, or add a line inside ComboProducto()
before using cmbProveedor
.
source to share