Solved

NULL Pointer Exception

Posted on 2004-04-26
11
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 

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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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 …
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…

717 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