0

I'm using BlueJ to create an array. The array is of 10 objects inherited from another class.

Now, my problem is that every time I try to find an object within the array (using the findInventoryItem, I get a java.lang.NullPointerException: null error. Now the "inventoryItem" object comes from another class. I am using inventoryItems as the actual array name. the objects that go into it are the inventoryItem of class InventoryItem

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Random;
    /**
    * THIS IS THE MODIFIED VERSION OF THE ARRAY LIST MANAGER CLASS
    * 
    * @author RAGEED A BASRAWI 
    * @version VERSION 1
    */
    public class HomeInventoryManagerARRAYClass
    {
    private InventoryItem inventoryItem;
    private InventoryItem[] inventoryItems;
    /**
 * Initialise the home inventory manager.
 */
public HomeInventoryManagerARRAYClass()
{
    InventoryItem inventoryItem;
    inventoryItems = new InventoryItem[10];
}

/**
 * Add an inventory item to the list.
 * 
 */
public void addInventoryItem(InventoryItem inventoryItem)
{
    Random random = new Random();
    inventoryItems[random.nextInt(9 - 0 + 1) + 0] = inventoryItem;
}

/**
 * Try to find an inventory item in the inventory list with the given partNumber.
 * 
 */
public InventoryItem findInventoryItem(int partNumber)
{
    for(int index = 0; index < 9; index ++)
    {
        if (inventoryItem.getPartNumber() == partNumber)
        {
            return inventoryItem;
        }
        if (inventoryItem.getPartNumber() != partNumber)
        {
            System.out.println("The entry " + partNumber + " does not exist. Pleast try again.");
        }
    }

    return inventoryItem;
}

/**
 * Locate an inventory item with the given partNumber, and return how
 * many of this item are in inventory. If the partNumber does not
 * match any item, return zero.
 */
public int numberInInventory(int partNumber)
{
    InventoryItem inventoryItems = findInventoryItem(partNumber);
    if(inventoryItems != null)//There aren't too many ways to write a statement to be NOT NULL. Its kind of annoying... -___-
    {
        return inventoryItems.getQuantity();
    }
    else
    {
        return 0;
    }
}

/**
 * Return the total number of items in inventory.
 * 
 */
public int numberInInventoryList()
{
    return inventoryItems.length;
}

/**
 * Print details of all the home inventory items.
 */
public void printInventoryList()
{
    int index = 0;
    while(index < inventoryItems.length)
    {
        System.out.println(inventoryItem = inventoryItems[index]);
        index ++;
    }
}

public int totalNumberInInventory()
{
    int index = 0;
    int absoluteInventory = 0;

    while (index < inventoryItems.length)
    {
        InventoryItem inventoryItem = inventoryItems[index];
        absoluteInventory += inventoryItem.getQuantity();
        index ++;
    }

    return absoluteInventory;
}

}

jahroy
  • 20,588
  • 9
  • 52
  • 104
rgbasrawi
  • 11
  • 2
  • 2
    `inventoryItem` could be null (based on the information revealed) – jmj Mar 27 '14 at 02:06
  • It may be, because when I looked at the object it tells me its null, but I still don't know why, or what to do to fix it.. – rgbasrawi Mar 27 '14 at 02:09
  • 1
    @rgbasrawi post your entire section of code. Right now, all we know is that it's `null` when you're running this, which indicates it was never set. – Snakes and Coffee Mar 27 '14 at 02:10
  • 1
    Yep, make sure you initialize it – Brendan Lesniak Mar 27 '14 at 02:20
  • Initialize what? The object? – rgbasrawi Mar 27 '14 at 02:21
  • 1
    There is a good chance that as @JigarJoshi said, that InventoryItem is just plain never set to any concrete value. Given the pattern in your code, I'm not certain why `inventoryItem` is a separate attribute, considering that you store new InventoryItem objects in the `inventoryItems` array, not the `inventoryItem` attribute. Either way, clarifying your question a bit more would help bring answers, as it's not entirely clear what you are asking. – ProgrammerDan Mar 27 '14 at 02:28
  • This has nothing to do with BlueJ. You need to learn the basics of arrays, namely how to initialize them. Please see [this question](http://stackoverflow.com/q/218384/778118), which will tell you a little about why NullPointerExceptions get thrown (and how to troubleshoot them). – jahroy Mar 27 '14 at 02:32
  • possible duplicate of [What is a Null Pointer Exception?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception) – jahroy Mar 27 '14 at 02:33
  • Well I'm using BlueJ as my IDE. I did initialize the array..?.. – rgbasrawi Mar 27 '14 at 02:34
  • No, you did not initialize the array. You declared it. There is a **huge, important difference** between declaring arrays and initializing them. I'm sure this was covered extensively in the class your taking (or in the reading). The IDE you're using is **irrelevant**! – jahroy Mar 27 '14 at 02:36

2 Answers2

0
inventoryItems[random.nextInt(9 - 0 + 1) + 0] = inventoryItem;

if you initialize the array with the line above, how do you make sure that all elements of the array are initialized? It's generating a random number and doesn't guarantee all elements between 0 to 9 are initialized.

Remember that with this line:

  inventoryItems = new InventoryItem[10];

you are initializing your array, not its elements. When running functions again each array's element you should be sure that you have initialized the element itself.

For example :

   InventoryItem[i].getPartNumber()

this line of code should be executed before

   InventoryItem[i] = new InventoryItem();
ProgrammerDan
  • 881
  • 7
  • 17
Siavosh
  • 2,136
  • 4
  • 20
  • 44
0

As @Jahoy has mentioned, it's likely that the elements in the array that you are accesing are null. It is essential firstly to check if an element exists in the first place.

Your FindInventoryItem function should be like this I think:

public InventoryItem findInventoryItem(int partNumber)
{
    for(int index = 0; index < 9; index ++)
    {
        InventoryItem tempItem = inventoryItems[index]; // ADD THIS LINE HERE
        if(tempItem != null)
        {
           if (tempItem.getPartNumber() == partNumber)
           { 
              return tempItem;
           }
           if (tempItem.getPartNumber() != partNumber)
           {
              System.out.println("The entry " + partNumber + " does not exist. Please try again.");
           }
       }
    }
    // at this stage you've got no result, I dont really know what you want to return
    return inventoryItem;
}
  • Do you know which line is throwing the Exception? If it's the last line of the function, it might be that the inventoryItem was not found at all. – Jalitha De Silva Mar 27 '14 at 02:44
  • This is wrong. How do you know that `inventoryItems[i]` will never be null? This answer just demonstrates the same misunderstanding about arrays that the OP is struggling with... – jahroy Mar 27 '14 at 02:44
  • @jahroy I don't. It's not a misunderstanding about the arrays, but I don't think you can see that even if everything in the array were not null, the function would still break. – Jalitha De Silva Mar 27 '14 at 02:49
  • The line that spits the problem is `if (inventoryItem.getPartNumber() == partNumber)` That gets highlighted and says the error. – rgbasrawi Mar 27 '14 at 02:56
  • I removed my downvote after your edit... I still think the OP needs to find a good [tutorial on arrays](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html). – jahroy Mar 27 '14 at 03:13
  • Holy hell!! Ok, SO it works ALMOST right. What I'm trying to get it to return is the actual object. The array is meant to hold objects of another class. So when I search for one of those objects by PART NUMBER, I should be getting the actual object – rgbasrawi Mar 27 '14 at 03:22
  • Ahh sorry for the delay. I've edited the code again, see if that fixes it up. – Jalitha De Silva Mar 27 '14 at 04:59