Solved

NULL Pointer Exception

Posted on 2004-04-26
11
527 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 45
How to convert from xls to xlsx using java 7 140
Print Rhino Java Array in Javascript 1 53
glassfish admin console 1 44
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

740 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