Tony White
Tony White

Reputation: 197

How do I check if a class' return of a method equals null?

In my program, I have a while loop that will display a list of shops and asks for an input, which corresponds with the shop ID. If the user enters an integer outside the array of shops, created with a Shop class, it will exit the loop and continue. Inside this loop is another while loop which calls the sellItem method of my Shop class below:

  public Item sellItem()
  {
     displayItems();
     int indexID = Shop.getInput();
        if (indexID <= -1 || indexID >= wares.length)
        {
            System.out.println("Null"); // Testing purposes
            return null;
        }
        else
        {
            return wares[indexID];
        }
  }
  private void displayItems()
  {
        System.out.println("Name\t\t\t\tWeight\t\t\t\tPrice");
        System.out.println("0. Return to Shops");
     for(int i = 0; i < wares.length; i++)
     {
        System.out.print(i + 1 + ". ");
        System.out.println(wares[i].getName() + "\t\t\t\t" + wares[i].getWeight() + "\t\t\t\t" + wares[i].getPrice());
     }
  }
  private static int getInput()
  {
     Scanner scanInput = new Scanner(System.in);
     int itemID = scanInput.nextInt();
     int indexID = itemID - 1;
     return indexID;
  }

The while loop in my main class method is as follows:

     boolean exitAllShops = true;
     while(exitAllShops)
     {
        System.out.println("Where would you like to go?\nEnter the number which corresponds with the shop.\n1. Pete's Produce\n2. Moore's Meats\n3. Howards Hunting\n4. Foster's Farming\n5. Leighton's Liquor\n6. Carter's Clothing\n7. Hill's Household Products\n8. Lewis' Livery, Animals, and Wagon supplies\n9. Dr. Miller's Medicine\n10. Leave Shops (YOU WILL NOT BE ABLE TO RETURN)");
        int shopInput = scan.nextInt();
        if(shopInput >= 1 && shopInput <= allShops.length)
        {
           boolean leaveShop = true;
           while(leaveShop)
           {
              allShops[shopInput - 1].sellItem();
              if(allShops == null)
              {
                 System.out.println("still null"); // Testing purposes
                 leaveShop = false;
              }
           }
        }
        else
        {
           System.out.println("Are you sure you want to leave?\n1. Yes\n2. No");
           int confirm = scan.nextInt();
           if(confirm == 1)
           {
              exitAllShops = false;
           }
        }

The problem is here:

       boolean leaveShop = true;
       while(leaveShop)
       {
          allShops[shopInput - 1].sellItem();
          if(allShops == null)
          {
             System.out.println("still null"); // Testing purposes
             leaveShop = false;
          }
       }

No matter what I do, I can't get "still null" to print to confirm that I'm correctly calling the return statement of the method sellItem of the class Shop. What am I doing wrong?

Upvotes: 0

Views: 73

Answers (1)

nneonneo
nneonneo

Reputation: 179442

After calling allShops[...].sellItem(), allShops is still a valid array reference -- there's no way it could be null! You probably want to test the return value from sellItem:

if(allShops[shopInput-1].sellItem() == null)

Upvotes: 5

Related Questions