0
votes

I am assigning my combobox to handle the Value or the Car Name so i make this code and NullPointerException is always the error does my code is wrong?

 public CarRent() 
{
    initComponents();
    setLocationRelativeTo(null);

    try
    {
     AddItem();
    }
    catch(Exception e)
    {
       e.printStackTrace();
    }




}


public void Conn() throws Exception
{

  Class.forName("com.mysql.jdbc.Driver");
  conn = DriverManager.getConnection(conn1,user,pass);   

}

public void AddItem() throws Exception
{
            String s = "Select Car_Name from Car_Tbl";
            rs = st.executeQuery(s);

            while(rs.next())
            {
                System.out.println(cboCars);
                cboCars.addItem(rs.getString("Car_Name"));
            }



}

run: java.lang.NullPointerException at trypo.CarRent.AddItem(CarRent.java:77) at trypo.CarRent.(CarRent.java:50) at trypo.CarRent$4.run(CarRent.java:413) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745) at java.awt.EventQueue.access$300(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:706) at java.awt.EventQueue$3.run(EventQueue.java:704) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:715) 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) BUILD SUCCESSFUL (total time: 3 seconds)

1
post the full error and tell us the line where your error occured - Madhawa Priyashantha
and also please post the stack trace. - Paul Sasik
@shampoo did you initialize cboCars ? - Madhawa Priyashantha
@shampoo put a System.out.println(cboCars); before cboCars.addItem(rs.getString(1)); and tell the output - Madhawa Priyashantha

1 Answers

0
votes

Try

 Resultset rs = st.executeQuery(s)
 While(rs.next())
{
 cboCars.addItem(rs.getString("Car_Name");
}

Your rs variable needs to be initialized as a ResultSet.

I also don't know your code above the try. Should be Statement

Statement st = null;
String s = "Select Car_Name from Car_Tbl";

After you populated the array cboCars.

JComboBox carList = new JComboBox(cboCars);

.....

Statement stmt = null;
String q = "select Car_Name from Car_Tbl"
String[] cars ;
int i = 0;
try { 
    stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(query);
    while(rs.next()) {
    cars[i] = rs.getString("Car_Name");
    i++;
   } catch (SQLException e) {
    }finally{
     if(stmt !=null) { stmt.close();}
   }
}
// Now You have an array populated with cars . Create your combo box with the data

JComboBox carList = new JComboBox(cars);