0
System.out.print("Enter the Item number of what is to be changed");
        String itemNO = in.next();

        for (int i = 0; i<NewItem.itemArray.length; i++)
        {
            String value = NewItem.itemArray[i][3];

            if(value.equals(itemNO))
            {
                //Rest of the code here
            }
}

When running the program, it throws an exception at "if(value.equals(itemNO))". This is the error it shows.

Exception in thread "main" java.lang.NullPointerException
    at DeleteItem.deleteItem(DeleteItem.java:34)
    at EditItemDetails.editItem(EditItemDetails.java:25)
    at CD_Universe.editItemDetailsActions(CD_Universe.java:84)
    at CD_Universe.main(CD_Universe.java:109)
savidude
  • 73
  • 2
  • 11

1 Answers1

1

value can still be null in your code, and therefore attempting to invoke equals() on it will throw an exception. Just because you are able to iterate over given element does not mean it has been initialized. I assume you have created an array of given size, but certain elements of that array have never been assigned a value.

You may fix this by populating the elements or adding a null check: if (value != null)...

In Java, only arrays of primitives are initialized with default values (since primitive type cannot be null. Arrays of reference types will not have their values initialized.