This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes null pointer exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "null pointer exception" Watch "null pointer exception" New topic
Author

null pointer exception

supriya lal
Greenhorn

Joined: Sep 11, 2012
Posts: 15
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

Joined: Jan 10, 2002
Posts: 61309
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Surapuraju Venkatakishor
Greenhorn

Joined: Dec 27, 2011
Posts: 11
make RequestDispatcher method logic is inside try block
supriya lal
Greenhorn

Joined: Sep 11, 2012
Posts: 15





thankyou it worked
supriya lal
Greenhorn

Joined: Sep 11, 2012
Posts: 15
Bear Bibeault
il be careful now...thanks
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4397
    
    8

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


Not as bad as just catching Exception .
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4474
    
    6

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

Joined: Jan 10, 2002
Posts: 61309
    
  66

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

Joined: Feb 10, 2001
Posts: 637
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


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61309
    
  66

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

Joined: Feb 10, 2001
Posts: 637
Thanks for the link Bear, so it is indeed special to get a cow :-)
 
GeeCON Prague 2014
 
subject: null pointer exception