If you have a variable of an Object extending type(anything that is not primary type-short,int,char,long,double,float) it will be initiallised to null by default, and unless you initialise yourself to something else, when you will try to apply a method to it or call a field of it's class, you are going to receive this exception. The code here is to little to be able to see exactely where the problem is, is names or inputName initiallised? Are you sure your object gets to be initiallised when "Cancel" option is used? Cause even if it shouldn't go inside a loop body if "Cancel" option is used, the boolean test is still performed.
On Jun 23, 11:29 pm, Al Catraz <[email protected]> wrote: > Can't find ho to resolve this EXCEPTION. > > I have some code that uses the JOptionPane.showInputDialog to > accept some data > When the CANCEL option is used -- and I expected to habe a null in the > receiving string (inputName as seen in next snippet of code ) > > Flow of program never gets to the IF statement when CANCEL option is > used > > while ((nameCount < names.length) && (endLoop != true)) { > inputName = JOptionPane.showInputDialog("Enter First & Last > name (up to three)"); > if (inputName.isEmpty( )) { > endLoop = true; > } else { > > this exception is generated, > > Exception in thread "main" java.lang.NullPointerException > at TwoDimArrayOfNames.main(TwoDimArrayOfNames.java:31) > Java Result: 1 > BUILD SUCCESSFUL (total time: 6 seconds) > > when data is entered properly and the OK option used -- all's well --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
