Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:45:37+00:00 2026-06-15T18:45:37+00:00

This is a method I’m working on. It needs to return an Array[] of

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:45:38+00:00Added an answer on June 15, 2026 at 6:45 pm

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This method: boolean containsSmiley(String s) { if (s == null) { return false; }
what this method will return when the db is null for some reason? compile
This method is working totally right in matlab. but, when I compiled it in
Take this method /** * @return List of group IDs the person belongs to
Can this method work? public String sayHello(){ return Hello.jsp?name= + laala; } I am
In this method I return a value of the type Double, but there is
Consider this method (pardon the sad attempt at Chuck Norris humor :) ): public
This method: bool Point::Intersects(const Line& line) const { return (line.ContainsPoint(*this, false)); } causes this
This method return true if one CGRect intersects with another. Is there any possibility
this method keep returning 0. According to the developer docs this method should return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.