3

In the following code, i have a method to get a Vector of persons with the same zodiac sign. persoane is a Vector<Persoana>. I keep getting a NullPointerException at the if condition (persoane is definetly not null). I am unable to see why. Any help would be greatly appreciated

public Vector<Persoana> cautaDupaZodie(String zodie) { Vector<Persoana> rezultat= new Vector<Persoana>(); for(int i=0; i<persoane.size(); i++) { if(persoane.get(i).getData().getZodie().equals(zodie)) //the exception occurs here { rezultat.add(persoane.get(i)); } } return rezultat; } 
6
  • No offense, but any API methods (i.e. any that are public) should have standardized English names to be understandable to a broader audience of programmers. Commented Mar 24, 2010 at 8:36
  • Breaking up the if statement and printing intermediate results will help you to find out which part is null. Commented Mar 24, 2010 at 8:36
  • @Michael: according to what standard? And who says that this is part of some API? It may very well be code from a in-house project (or even some homework). Commented Mar 24, 2010 at 8:40
  • There isn't a standard... it's just easier to follow code where the words make sense. When you use nonsensical words, it can be difficult to follow. And if I were someone asking for help, I would want my code to be very readable to the people I"m asking. Commented Mar 24, 2010 at 9:03
  • 2
    @David: calling words in a language that you don't understand "nonsensical" points at a very narrow world view. And for this problem the domain knowledge isn't relevant. It would be equally hard to understand if the words where jargon from some scientific area. Would you insist that those words be replace with "everyday words" as well? Commented Mar 24, 2010 at 9:22

7 Answers 7

5

NullPointerException occurs, when you try to call a method on an Object that is null.

This means that one of the following returns null:

  • get(i)
  • getData()
  • getZodie()

Add them one by one to find out what actually is causing your exception.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, apparently i didn't make sure that getData() always returns a not null value.
2
persoane.get(i).getData().getZodie() 

break that down into several lines to see where the NPE occurs.

Also, consider using the for-each loop.

for (Persoana persoana: rezultat){ ... } 

Comments

1

The getData() or getZodie() returns null.

Comments

1

You may add some "tracing" code:

public Vector<Persoana> cautaDupaZodie(String zodie) { Vector<Persoana> rezultat= new Vector<Persoana>(); for(int i=0; i<persoane.size(); i++) { System.err.println(persoane.get(i)); System.err.println(persoane.get(i).getData()); System.err.println(persoane.get(i).getData().getZodie()); if(persoane.get(i).getData().getZodie().equals(zodie)) //the exception occurs here { rezultat.add(persoane.get(i)); } } return rezultat; } 

Comments

1

It could be any of the following:

  • persoane contains a null at any index
  • Any of the elements of persoane returns null for getData()
  • Any of the getData() results retruns null for getZodie()

To investigate further, you'd best break up that chain of methods and add a conditional breakpoint. Additionally, rethink your design - this kind of "method operating on deeply nested, behaviourless data structure" is bad for this and other reasons.

Comments

1
if(persoane.get(i).getData().getZodie().equals(zodie)) 

break the above line into several parts.Here getData must be returning null so you are getting NullPointerException

Comments

1

First of all, you should use List<Person> if possible. Then, I recommend breaking the method chain to a smaller steps to see which exact step is failing.

public List<Person> searchZodiac(String zodiac) { assert zodiac != null; // if it fails here, zodiac == null List<Person> result = new ArrayList<Person>(); for (Person p : persons) { Data d = p.getData(); // if it fails here, p == null String z = d.getZodiac(); // if it fails here, d == null if (z.equals(zodiac)) { // if it fails here, z == null result.add(p); } } return result; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.