-1

I'm trying to query my database and display in a combo box. But I constantly get the same error, so Im not sure now where the problem is exactly, I get java.lang.NullPointerException everytime I click on the combobox

this is my code:

``

  public Sell_Tick() 
  {

    initComponents();

    FillRouteCombo();
    con = CreateDB.getConnection();

        if(con!=null)
    {
        System.out.println("Successful DB Connection");
    }

}
private void FillRouteCombo()
    {
        try 
        {
            pst=con.prepareStatement("select * from routes");
            rst= pst.executeQuery();
            while(rst.next())
            {
                String getroutes=rst.getString("destfrom");

                destfrom.addItem(getroutes);


            }
        } catch (Exception e) {
            System.out.println(e);
        e.printStackTrace();
        }

    }

The error i get is this

java.lang.NullPointerException Successful DB Connection at Sell_Tick.FillRouteCombo(Sell_Tick.java:47) at Sell_Tick.<init>(Sell_Tick.java:34) at StartPage.TicketOperationActionPerformed(StartPage.java:1710) at StartPage.access$5300(StartPage.java:52) at StartPage$33.actionPerformed(StartPage.java:1121)

and line 47 is pst=con.prepareStatement("select * from routes");

Rick
  • 15
  • 1
  • 8
  • 1
    Spoiler: ``con`` is null. But please read and understand the question linked above as resolving these Exceptions always follow the same path. – f1sh Jan 03 '17 at 15:05

1 Answers1

0

Just change:

From:

FillRouteCombo();
con = CreateDB.getConnection();

To:

con = CreateDB.getConnection();
FillRouteCombo();
Bruno
  • 2,889
  • 1
  • 18
  • 25