• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Scanner - exit while loop

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello there,

I am trying t exit from a while loop but I got a nullPointerException when I enter any number/string.. Any idea ?

Thank you!!

 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure what your program is supposed to do, but you have set the variable c to null and that is why you are getting a null pointer exception.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanna format a number.

Ok, but if I swap with



I still have the same issue when I enter "ex"...
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line of code is throwing the exception? Hint: the stack trace which tells you what kind of exception happened also tells you what method the error was in and what line number.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I got...
Capture.PNG
[Thumbnail for Capture.PNG]
 
Bartender
Posts: 732
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you enter "ex", cs.hasNext() returns true, but c is still what it was after the previous iteration, so you enter the loop body. then you call cs.nextInt() which throws an exception because "ex" is not an integer.
 
Matt Road
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah! SOlved!




Thank you chaps!!
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Road wrote:Yeah! SOlved!


Well done!

Tip for the future: if you want to check a String that might be null against a literal, swap the test around, viz:
  while ( cs.hasNext() && !"ex".equals(c) ) { ...

I suspect that would also have solved your problem in this case.

Winston
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, using !"ex".equals(c) would not have solved the problem. The problem was not that c was null (it wasn't), but that the next token existed but was "ex", so trying to execute cs.nextInt() threw an exception.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Had there not been some sort of need to check that input equals "ex", it would simply have been possible to use
while (myScanner.hasNextInt()) ...
 
Fred Kleinschmidt
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed. That would be a better way - wouldn't even need to type "ex" - ANY non-integer string would work to exit the loop
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic