Solved

NULL Pointer Exception

Posted on 2004-04-26
11
522 Views
Last Modified: 2012-06-27
Objective: select all columns in a table for the selected item in the JComboBox that matches a table number in a table,
                use values  from table for JTextFields, call displayTotal meth with column called subtotal from table in dbase

Thanks......


 private void tableNumberJComboBoxItemStateChanged(
      ItemEvent event )
   {
            String waiter="";
            int tableNum = 0;
            double subT = 0.0;
;

            //query db for all columns for selected item from jcombobox
            try
            {
                  //select all columns from restaurantTable table
                  myRS=mySt.executeQuery( "select * from restauranttables " +
                  "where tablenumber = " + ItemEvent.SELECTED );


                  while( myRS.next() )
                  {
                              //match waiter name to table number
                              waiter = ( myRS.getString ( "waitername" ));
                              tableNum = ( myRS.getInt( "tablenumber" ));
                              subT = ( myRS.getDouble( "subtotal" ));

                              // throws null pointer exception
                              waiterNameJTextField.setText( waiter );
                  }

                  myRS.close();

            }

            catch( SQLException sqe )
            {
                  sqe.printStackTrace();
            }


   } // end method tableNumberJComboBoxItemStateChanged
0
Comment
Question by:shanemcf
  • 5
  • 4
11 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
check if waiterNameJTextField is null
maybe also check waiter
0
 

Author Comment

by:shanemcf
Comment Utility
waiterNameJTextField not exclusively set to NULL,
 shouldn't var waiter contain the waitername from the dbase query?
 
 does my SQL statement look as if it is matching (int)tablenumber (from dbase)  to (int)tablenumber(from JComboBox) ?

not sure how to implement, or for that matter is it necessary to:

if( event.getStateChange() == ItemEvent.SELECTED ) ..............

If so, how so, or as I see it, ItemEvent.SELECTED seems to be the same, however, NOT working.

Oh yeah, I get duplicates when I System.out.println() it.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
can u post the stack trace for the npe
0
 

Author Comment

by:shanemcf
Comment Utility
//////////////////  Line 534  ///////////////////////
Line 534 contains: waiterNameJTextField.setText( waiter );

Exception in thread "main" java.lang.NullPointerException
        at RestaurantBillCalculator.tableNumberJComboBoxItemStateChanged(Restaur
antBillCalculator.java:534)
        at RestaurantBillCalculator.access$400(RestaurantBillCalculator.java:10)

        at RestaurantBillCalculator$5.itemStateChanged(RestaurantBillCalculator.
java:296)
        at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1161)
        at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1218)
        at javax.swing.JComboBox.contentsChanged(JComboBox.java:1265)
        at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.j
ava:100)
        at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel
.java:88)
        at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java
:126)
        at javax.swing.JComboBox.addItem(JComboBox.java:690)
        at RestaurantBillCalculator.createWaiterJPanel(RestaurantBillCalculator.
java:304)
        at RestaurantBillCalculator.createUserInterface(RestaurantBillCalculator
.java:126)
        at RestaurantBillCalculator.<init>(RestaurantBillCalculator.java:104)
        at RestaurantBillCalculator.main(RestaurantBillCalculator.java:741)
Press any key to continue . . .
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Then it looks like waiterNameJTextField is null.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:shanemcf
Comment Utility
I figured it out....

I was trying to use ItemEvent meths but trying to hard, teaches you to keep it simple.....

 Thanks Objects for your replies
////////////// Modified Code  ///////////////////////

try
            {
                  //select all columns from restaurantTable table
                  myRS=mySt.executeQuery( "select * from restauranttables " +
                  "where tablenumber = " + tableNumberJComboBox.getSelectedIndex() );


                  while( myRS.next() )
                  {
                              //match waiter name to table number
                              waiter = ( myRS.getString ( "waitername" ));
                              tableNum = ( myRS.getInt( "tablenumber" ));
                              subT = ( myRS.getDouble( "subtotal" ));

                              // throws null pointer exception
                              waiterNameJTextField.setText( waiter );
                  }

                  myRS.close();

            }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
no worries :)
0
 

Author Comment

by:shanemcf
Comment Utility
obj, how do these points systems work, and how should it work in this case?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
up to u. u can ask CS for a refund or accept one of my comments as an answer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
Comment Utility
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now