-8

This is a method I'm working on. It needs to return an Array[] of results or return null. However, I'm getting a NullPointerException error when I attempt to test it. I've been searching and getting nowhere the past few hours. Any help for a suffering student would be appreciated.

public Item[] searchByName(String name) {

    String n = name;
    ArrayList<Item> sortName = new ArrayList<Item>();
    Item[] matchArr;
    boolean noResult = false;

    for(int j = 0; j < itemInventory.size(); j++){
        if(itemInventory.get(j).getName().equalsIgnoreCase(n) == true)
            sortName.add(itemInventory.get(j));
        else
            noResult = true;
    }
    matchArr = new Item[sortName.size()];
    for(int j = 0; j < sortName.size(); j++){
        matchArr[j] = sortName.get(j);
    }
    if(noResult == true)
        return null;

    else
        return matchArr;

}

//Revised version //itemInventory is an ArrayList of objects being inventoried and sorted.

public Item[] searchByName(String name) {

    String n = name;
    ArrayList<Item> sortName = new ArrayList<Item>();
    Item[] matchName;

    for(int j = 0; j < itemInventory.size() ; j++){
        if(itemInventory.get(j) != null){
            if(itemInventory.get(j).getName().equalsIgnoreCase(n) == true){
                sortName.add(itemInventory.get(j));
                System.out.println(j + " Is a match");
            }
        }               
    }

    if(sortName.size() == 0)
        return null;
    else{
        matchName = sortName.toArray(new Item[sortName.size()]);
        return matchName;
    }
t0dd
  • 84
  • 8
  • You have not even specified which line causes NPE – Nikolay Kuznetsov Dec 11 '12 at 06:18
  • Can u pls post the error stack trace? – Ayyappan Sekar Dec 11 '12 at 06:18
  • Sounds like you're ready to learn one of the most important skills that you can learn as a Java programmer: how to debug a _NullPointerException_. Your stack trace should tell you _EXACTLY_ which line caused the Exception. Once you know that, you need to look at every variable that is de-referenced on that line (ie has a method called on it) and check if it is null. You will do this thousands of times if you decide to write Java code for a living. – jahroy Dec 11 '12 at 06:19
  • post the error log or console – Hussain Akhtar Wahid 'Ghouri' Dec 11 '12 at 06:19
  • I guess that this line `itemInventory.get(j).getName().equalsIgnoreCase(n) == true` causes NPE. Make sure that `itemInventory.get(j)` is not `null` before you get the name. – Maroun Dec 11 '12 at 06:19
  • @electr0hed: check your stacktrace.. go to the last lines of it.. The follwing link helps [stacktrace](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – kidd0 Dec 11 '12 at 06:22
  • @Maroun Maroun. That was it!! Thanks for the help!! – t0dd Dec 11 '12 at 06:24
  • What is itemInventory? Maybe that's where you're getting a null pointer exception. Could you tell us what line gives you a null pointer exception? – Vyassa Baratham Dec 11 '12 at 06:20

1 Answers1

2

Probably this line itemInventory.get(j).getName().equalsIgnoreCase(n) == true causes NPE. Make sure that itemInventory.get(j) is not null before you get the name:

if(itemInventory.get(j)!=null) //only then get the name
    if(itemInventory.get(j).getName().equalsIgnoreCase(n))
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Thanks, I knew it was something in front of my face. Appreciate the help! – t0dd Dec 11 '12 at 06:34
  • You're welcome, I'm glad it helped :) Next time, try to debug it, that will help you a lot. Debugging your code will help you to understand what's the problem and why it is happening, specially when it comes to `NullPointerException` – Maroun Dec 11 '12 at 06:36
  • @MarounMaroun : `equalsIgnoreCase(n)` already returns true or false, you do not have to check it again. You could directly use `if(itemInventory.get(j).getName().equalsIgnoreCase(n))`. – Nandkumar Tekale Dec 11 '12 at 06:43
  • @NandkumarTekale If `itemInventoy.get(j)` is null, then you are getting a name of a `null` object (`null.getName()`), which will cause a `NullPointerException`. So you have to check weather it is `null` before you get the name. (The exception is thrown before you get to `equalsIgnoreCase(n)`) – Maroun Dec 11 '12 at 07:07
  • 1
    @MarounMaroun : I am talking about 2nd if in your code. `if(itemInventory.get(j).getName().equalsIgnoreCase(n) == true)`. – Nandkumar Tekale Dec 11 '12 at 07:59
  • @NandkumarTekale Sorry I misunderstood you. Yes I know, I just didn't want to change his original line (I copy pasted it). Thanks for clarifying this :) – Maroun Dec 11 '12 at 08:05