Hello! I am creating a program that creates a Bag of different Objects. These can be integers, Strings, doubles, etc. I have created a bag and put in int's and a few Strings. I can display the bag with no errors. However now I am running into a problem. I have this remove method that is supposed to iterate through my array that contains the Objects and remove any Object that collect[i].equals. I am getting a null pointer error. Any suggestions?
Remove method:
Calling it in main method (2, and "CSCI" are both in the bag):
Remove method:
public void remove(Object item){ if (this.size() > 0){ for (int i = 0; i < collect.length; i++){ if (collect[i].equals(item)){ collect[i] = null; count_ItemsRandomlyRemoved++; counter--; } } }else{ System.out.println("The item was not found in the bag"); } }
Calling it in main method (2, and "CSCI" are both in the bag):
bag1.remove(2); bag1.remove("CSCI");