Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

null pointer exception

 
supriya lal
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


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
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?
 
Surapuraju Venkatakishor
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
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala 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
 
Matthew Brown
Bartender
Posts: 4566
8
  • 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
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala 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
Author and ninkuma
Marshal
Pie
Posts: 64700
86
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.
 
Praful Thakare
Ranch Hand
Posts: 642
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64700
86
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: 642
  • 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 :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic