views:

129

answers:

7

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;
}
+5  A: 

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.

Peter Lang
Thank you, apparently i didn't make sure that getData() always returns a not null value.
John Pope
+2  A: 
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){
    ...
}
Thilo
+1  A: 

The getData() or getZodie() returns null.

thelost
+1  A: 

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;
}
PeterMmm
+1  A: 

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.

Michael Borgwardt
+1  A: 

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;
}
polygenelubricants
+1  A: 
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

giri