?
Solved

NULL Pointer Exception

Posted on 2004-04-26
11
Medium Priority
?
529 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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