1

I created a search method for a linked list. It works correctly when I search for something that does exist in the list. If I search for something not in the list, then I get a null pointer exception.

I don't see how its possible to get this exception.

Here is the method:

 // these two search methods return the node containing the substring given
public UNode search(String cityName)
{
    return search(cityName,this);//line 90
}//end search()-------------------------------------------------------------
private UNode search(String cityName, UNode current)
{
    if(current != null)
    {
        if (current.getCity().getName().contains(cityName))
        {
            System.out.println("Node '" + current.getCity().getName() 
                    + "' has been found and returned.");
            return current;
        } else
        {

            return search(cityName,current.next);//line 105
        }
    }
    else
    {
        System.out.println("No node containing the substring '" 
                + cityName + "' can be found, returning null");
        return null;
    }
}//end search()-------------------------------------------------------------

To my understanding, this is what happens when searching for something that does not exist: The method keeps calling search() recursively with current.next, it gets to the last element and calls search(cityName, null), then since current is null, it says it is not found, and returns null.

Am I missing something here? What am I doing wrong? Should I just throw the null pointer exception?

Here is the method where I call the search method:

public static void uNodeTest(City[] cities)
{


    UNode unvisited = new UNode();

    unvisited.addCities(cities);


    String a = unvisited.getNext().getNext().getNext().getCity().getName();

    System.out.println(a);

    UNode current = unvisited;
    while(current.getNext() != null)
    {
        System.out.println(current.getCity().getName());
        current = current.getNext();
    }
    UNode phil = unvisited.search("not a city");
}

Stack Trace:

java.lang.NullPointerException
at cityproject.UNode.search(UNode.java:105)
.
.
.
at cityproject.UNode.search(UNode.java:105)//(omitting repeats)
at cityproject.UNode.search(UNode.java:90)
at cityproject.CityProject.uNodeTest(CityProject.java:104)
at cityproject.CityProject.main(CityProject.java:79)
user2809114
  • 97
  • 4
  • 15
  • 1
    Your recursion is ending when you hit `null`, which means that you did not find the city. It is up to you whether you want to throw an exception or just return `null` to your caller. Other than that your code looks fine. – Tim Biegeleisen Apr 27 '15 at 04:21
  • Well I am currently returning null, but instead of returning null, it crashes and gives me null pointer exception. – user2809114 Apr 27 '15 at 04:24
  • Can you also post your call to `search` along with the stack trace. I am not seeing a problem so far. – Tim Biegeleisen Apr 27 '15 at 04:29
  • 1
    @user2809114 your stacktrace speaks about lines 90 and 105. can you indicate which lines are these (i.e., on the source code you posted which line is 90 and which line is 105)? – Itay Maman Apr 27 '15 at 04:47
  • 1
    I added comments to the code to show the lines, line 90 in the public search(String) body, line 105 is the recursive step in the private search method – user2809114 Apr 27 '15 at 04:51
  • 4
    then something here does not add up. please double check that the source code you posted is the one you're currently running and that the stacktrace is from this source code. perhaps your changes do not get compiled (becuase you have a compiler problem) so you're actually running older code? (why? because if you get an NPE on L.105 it means that you have there an x.y expression where x is null. the only such expression on L.105 is current.next and you already verified that current is not null) – Itay Maman Apr 27 '15 at 05:00
  • Also, you can try rewriting your code to be a little bit cleaner. – Tim Biegeleisen Apr 27 '15 at 05:01
  • Also, use the getter instead of the accessing the `next` property directly. I don't think that would cause an issue... but I think it's a good idea. – shieldstroy Apr 27 '15 at 05:03
  • I am starting to suspect that maybe the problem is not in the method, but the class itself. This is my first try at making a linked list and maybe I am not understanding it completely. I think I will do some more research, and then come back to it. – user2809114 Apr 27 '15 at 05:11

1 Answers1

0

I would add a check before calling the recursive search to see if current.next is null:

if(current.next != null) {
    return search(cityName,current.next);
}
else {
    return null;
}

This might solve the null pointer exception...

mjgoonan
  • 96
  • 3