0

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.

4
  • does the delete() and reading from the array "get()" happen at same time ? say on different threads for example ? Commented Sep 29, 2014 at 19:28
  • I looked thru the exception list and it didn't appear so. I didn't see a reference to any of the get() methods in the runtime errors, but i'm not 100% sure :/ The first exceptions were at searchArray() and insert() which, ironically enough, and the methods that work fine under normal circumstances. It's only when I delete an item and attempt to insert() another object or display() said data do the runtime errors occur. This is why I think it's an issue with the way i'm shifting the array. Commented Sep 29, 2014 at 19:42
  • i dint mean get() precicely , i meant any kind of access to the array simultaneous to the deleting , cuz if u delete thus shift and read at the same time , the data will be corrupted , had this problem before...stackoverflow.com/questions/24917931/… Commented Sep 29, 2014 at 20:05
  • Oh alright. Well if anything I learned something. I appreciate the input. Commented Sep 29, 2014 at 20:12

1 Answer 1

0

I have only one general remark, and that's not to use Java primitive Arrays. Save yourself a lot of trouble and use ArrayList. That way you can simply remove an element at an index, there's no need then to manually manage elements in a primitive array.

Without a clear exception trace and a code snippet that shows how things are displayed I'm just speculating, but when you delete an item you shift everything manually and append a null value at the end of the array. When your array is then displayed you are most likely hitting a NullPointerException when dereferencing a null object.

In general you should only rarely have the need for primitive arrays. Use an ArrayList instead and delete items using its remove method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.