• Post Reply Bookmark Topic Watch Topic
  • New Topic

NPE in the API  RSS feed

 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
i have a question about writing classes that can be used by third parties (essentially API and its implementation).
i am confused about following,
1. i observed that all the API methods assume certain things about the input. e.g. the compareToIgnoreCase() etc methods in the String assume that the input strings are not null. if they are null then we get NPE.
2. from C coding background, i would write a code that checks if input is NULL or not. if NULL then take some action to avoid "runtime error"...
(i'm not sure if this is because we don't have exceptions handling or sth in C)
now, which way to go? should my implementation check if some input is NULL or not? of course we don't have to say it will throw NPE at runtime if we are not checking input for NULL values as NPE is runtime exception anyways...
PERSONALLY, i believe - We should assume that input will not be null and let application throw NPE as that would allow the third party to handle such cases more flexible way...
any suggestions??
regards.
maulin
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well what causes a NullPointerException? Bad programming or something that the programmer has no control over? When a programmer passes a null reference into a method, that indicates to me a logic problem. The calling method should be responsible for the validity of the reference not the called method. NPEs are by their nature runtime problems. If you are needing to check null on every reference in every method something is wrong with your program.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!