• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

null pointer exception  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please help me with the following code..i am getting a null pointer exception..what can be the reason??

SERVLET


 
Marshal
Posts: 67225
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.

I've gone ahead and added the code tags for you. See how much easier the code is to read?

Also, what line is the exception occurring on?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
make RequestDispatcher method logic is inside try block
 
supriya lal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





thankyou it worked
 
supriya lal
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault
il be careful now...thanks
 
Sheriff
Posts: 12966
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's bad practice to catch a NullPointerException. You need to fix your code so that you won't get a NPE, either by checking for null before using an object reference or by ensuring that the reference will never be null by initializing it to something.

Again, catching a NullPointerException is BAD coding form
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Again, catching a NullPointerException is BAD coding form



Not as bad as just catching Exception .
 
Junilu Lacar
Sheriff
Posts: 12966
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Matthew Brown wrote:Not as bad as just catching Exception .



Yeah, that's BAD too.

This kind of code is a cop out at best and a sign of programmer laziness at worst. In both cases, it's the programmer's responsibility to do more than just add a catch block. Take the effort to fix the code that causes a NullPointerException. Take time to analyze the code and identify the exact subclass of Exception that can be thrown, then decide whether or not there is something you can do to recover from the problem.
 
Bear Bibeault
Marshal
Posts: 67225
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

supriya lal wrote:thankyou it worked


No it did not work. If the exception is still occurring, then the problem still exists. Catching the exception is not acceptable as already pointed out.

It's like going to a doctor for cancer who gives you a shot of morphine to take away the pain and pronounces you cured.
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It's like going to a doctor for cancer who gives you a shot of morphine to take away the pain and pronounces you cured.



well Said Bear , I see this is a common problem with young developes, most of them just eager to see some code running instead of understanding the root cause at first place..

BTW, how to earn those holy cows on JR? you have highest (10) i have seen so far.

-P
 
Bear Bibeault
Marshal
Posts: 67225
169
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Praful Thakare wrote:well Said Bear


Thanks!

BTW, how to earn those holy cows on JR? you have highest (10) i have seen so far.


See this topic in Ranch Office.

There are a number of people with more (well-deserved) cows than I.
 
Praful Thakare
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link Bear, so it is indeed special to get a cow :-)
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!