Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

keep getting NullPointerException error

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i keep getting the following error
java:146: cannot resolve symbol
symbol : constructor NullPointerException (int)
location: class java.lang.NullPointerException
throw new NullPointerException(answer);

and the method is


does anyone know why this error keeps coming?
 
Ranch Hand
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because NullPointerException doesn't take int as an argument. Also, why are you throws a NullPointer anyway? you should create your own Exception in this case.
 
Sally Curtis
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do i make my own exception?
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your own Exception is a class, derived from another class.
Have a look at Exception in the docs.
If you find an appropriate parent in the list of exceptions (this needs some experience), you may extend that specific Exception.
If you need this exception only in one class, you may make it an inner class.

But Joshua Bloch claims to favor Standardexceptions, where they fit.
So you could throw the NullpointerException that way:

But isn't a null-Pointer-Ex thrown anyway?
So why catch it?
You may catch it, to give a more informative message, so the user may learn for the next time.
But you can do better.
You may test for (length < 1), and have the input in a loop:

Conclusio:
If you can catch the exception, and solve the problem, solve it as early as possible.
If you can not solve it be informative about the semantic of the error.
Perhaps the user may solve it.
Use StandardExceptions where appropriate.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic