return Array[] or return null Java
<p>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.</p>

<pre><code>public Item[] searchByName(String name) {

    String n = name;
    ArrayList&lt;Item&gt; sortName = new ArrayList&lt;Item&gt;();
    Item[] matchArr;
    boolean noResult = false;

    for(int j = 0; j &lt; 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 &lt; sortName.size(); j++){
        matchArr[j] = sortName.get(j);
    }
    if(noResult == true)
        return null;

    else
        return matchArr;

}
</code></pre>

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

<pre><code>public Item[] searchByName(String name) {

    String n = name;
    ArrayList&lt;Item&gt; sortName = new ArrayList&lt;Item&gt;();
    Item[] matchName;

    for(int j = 0; j &lt; 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;
    }
</code></pre>
