Solved

NULL Pointer Exception

Posted on 2004-04-26
11
524 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
ID: 10924890
check if waiterNameJTextField is null
maybe also check waiter
0
 

Author Comment

by:shanemcf
ID: 10924936
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
ID: 10924944
can u post the stack trace for the npe
0
 

Author Comment

by:shanemcf
ID: 10924996
//////////////////  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
ID: 10925025
Then it looks like waiterNameJTextField is null.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:shanemcf
ID: 10925087
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
ID: 10925094
no worries :)
0
 

Author Comment

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

Expert Comment

by:objects
ID: 10925337
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
ID: 11187298
PAQed, with points refunded (50)

Computer101
E-E Admin
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

896 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

16 Experts available now in Live!

Get 1:1 Help Now