This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Null Pointer Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Null Pointer Exception " Watch "Null Pointer Exception " New topic
Author

Null Pointer Exception

subodh varshney
Greenhorn

Joined: Aug 01, 2005
Posts: 11
Dear All,
Please help me I m facing NullPointerException.

In following code i m retreiving some values from a table and storing them in a vector.
In case of response when it get null then loop goes to NullPointerException
so remaining variables(responseflag,url,urlflag,) does't get value so again it next code gives nullpointerexception.



for (int loop = 0; loop < result.size(); loop++) {
String keyword = null; String suffix = null; String username = null;
String password = null; String response =""; String responseflag = null;
String url =""; String urlflag = null;

try {
keyword = result.elementAt(loop).toString();
suffix = result.elementAt(loop + 1).toString();
username = result.elementAt(loop + 2).toString();
password = result.elementAt(loop + 3).toString();
response = result.elementAt(loop + 4).toString();
responseflag = result.elementAt(loop + 5).toString();
url = result.elementAt(loop + 6).toString();
urlflag = result.elementAt(loop + 7).toString();

}
catch (NullPointerException ex) {
}
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3011
    
    9
You need to split each line so that you can check if has returned null before calling toString()
i.e.

becomes


Joanne
Sathya Srinivasan
Ranch Hand

Joined: Jan 29, 2002
Posts: 379
To expand on that, here's the Sherlock Holmes' rule to finding null pointers.

1. Print the stack trace (in your code you don't print anything. NEVER swallow a stack trace).
2. Look at the line where the exception happens.
3. Look at each segment in the code. A segment is essentially an object between dots. For example, in a code like a.someMethod().someOtherMethod(), there are 3 segments.
4. Take all but the last segment into consideration.
5. Eliminate segments that CANNOT be null (see if you have declared them already or if such methods will definitely return a value). Add log statements if needed.

Whatever remains, however confident you are, must be null and hence causing the NPE.


Cheers, Sathya Srinivasan - SCJP 1.2, SCWCD 1.2, SCMAD 1.0
Co-Author of Whizlabs SCMAD Certification Exam Simulator and SCMAD Exam Guide Book
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Null Pointer Exception
 
Similar Threads
Error on threads
Not able to forward servlet to
Help with this code!
how to make a method in database access class for login
data grid with edit and delete options