My project consists of 2 class files and a test application GUI interface to use the data. The two class files, both created by me, are ToolItem (defines a hardware tool with get() and set() methods, display() method, etc) and HardWareStore, which is used to manipulate the data and place it into an array using methods like insert(), delete(), searchArray() ect.
The GUI interface, which includes the same relative buttons (insert, delete) calls HardWareStore when the corresponding button is pressed.
So far everything works perfectly EXCEPT the delete button/delete() method. My methods are as follows, including the searchArray() method I created which is used inside delete() to evaluate a unique ID and determine if the ID already exists or not:
searchArray:
public int searchArray(int id)
{
for (index = 0; index < toolArray.length; index++)
{
if (toolArray[index].getToolID() == id)
{
System.out.println("ID found at location " + index);
return index;
}
}
return -1;
}//end searchArray
delete:
public int delete(int ID)
{
testArray = searchArray(ID);
if (testArray != -1)
{
toolArray[testArray].setQuality(0);
toolArray[testArray].setToolName("");
toolArray[testArray].setID(0);
toolArray[testArray].setNumberInStock(0);
toolArray[testArray].setPrice(0.0);
//counter for number of items in array. Array holds 10 objects
numberOfItems--;
//shifting cells
for (index = testArray + 1; index < toolArray.length; index++)
{
toolArray[index - 1] = toolArray[index];
}
toolArray[toolArray.length - 1] = null;
System.out.println("Successful deletion");
return 1;
}
else
System.out.println("ID not found");
return -1;
}//end delete
And here is the corresponding GUI button used to delete the data in my test application:
else if (ev.getSource() == deleteBtn)
{
input = idFld.getText();
idInput = Integer.parseInt(input);
responseCode = store.delete(idInput);
if (responseCode == 1)
{
messageFld.setText(idInput + " - Successful deletion");
}
else if (responseCode == -1)
{
messageFld.setText(idInput + " - ID not found");
}
}//end delete button
Now, everything inserts fine, detects duplicate ID's, and so on. The delete method also works, letting me know that the user passed ID has been deleted at toolArray[index] as well as all of it's related data members.
The error occurs when I go to display the items in the array. The first 9 items display fine using JOptionPane, but when I reach item 10(my delete method is supposed to shift all cells to the left after deletion) I get several runtime errors starting with:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.
I'm pretty sure my error starts with the delete() method in HardWareStore, specifically my loop to delete and shift the items to left. I've messed with it for a few hours but I cannot seem to find why, and my attempts to change the loop either result in another runtime error, or the method doesn't behave like it should.