-2

I need to remove an element of the array based on a match. Here is my method to remove the event.

public boolean removeEvent(int year, int month, int day, int start, int end, String activity)
    {
    Event newEvent = new Event(year, month, day, start, end, activity);
    for (int i = 0; i < 5; i++)
    {   
        if (newEvent.equals(events[i]))
        {
            events[i] = null;
            newEvent = null;
            numEvents--;
        }
    }

When I try

calendarTest1.removeEvent(2000, 1, 1, 1, 1, "Cal Test 1");

nothing happens. I have an element in my array with those values but it does not change that element to null.

This is for homework so I don't really want to be told how to do it, just why this does not work. Thank you.

Here is my equals method:

public boolean equals(Object obj){

    Event someEvent = (Event) obj;
        if(
        this.date == someEvent.date
            &&
        this.start == someEvent.start
            &&
        this.end == someEvent.end
            &&
        this.activity.equals(someEvent.activity))

    if(obj == null) 
        return false;
      if(obj instanceof Event)
        return true;
      else
      {
      return false;
      }
}

I have tried a lot of different things but I still get the NullPointerException Error

user2744307
  • 25
  • 1
  • 3
  • 9
    Looks like the `equals` method in your `Event` class is broken. Post its implementation to get further help. – Luiggi Mendoza Sep 11 '13 at 17:26
  • Can you post the overridden `equals()` function for Event? – David says Reinstate Monica Sep 11 '13 at 17:27
  • 3
    Perhaps your array is longer than 5? Trying using a variable that is the length of your array. In addition, are `events` and `numEvents` in scope? – Jordan Sep 11 '13 at 17:28
  • Make sure your equals is checking for all the attributes to be equal and returning true if they are. – JNL Sep 11 '13 at 17:30
  • Haven't seen the implementation of your `equals` method but I can only think the problem is you're comparing `String activity` using `==` instead of `equals` or sending the wrong data to remove the element from your array. – Luiggi Mendoza Sep 11 '13 at 17:32

3 Answers3

0

is your equals method checking for all the attributes?

public boolean equals(Object o){
  if(o == null) return false;

  if(o instanceOf Event){
    Event givenObject = (Event) o;
        if(this.year == givenObject.year)
          if(this.month == givenObject.month)
              .....
                  .....
                     if(this.activity.equals(givenObject.activity)){
                        return true;
                 }
   }
   return false;
}
JNL
  • 4,683
  • 18
  • 29
0

your overridden equal method shoud be somethig like below

   @Override
        public boolean equals(Object o) {

            // If the object is compared with itself then return true  
            if (o == this) {
                return true;
            }

            /* Check if o is an instance of Event or not
              "null instanceof [type]" also returns false */
            if (!(o instanceof Event)) {
                return false;
            }

            // typecast o to Event so that we can compare data members 
            Event e = (Event) o;

            // Compare the data members and return accordingly 
            return year==e.year && month== e.month && day==e.day && start == e.start && end == e.end && activity.equals(e.activity);
        }
    }
Brajesh Kumar
  • 929
  • 6
  • 16
0

You are comparing two instances in this if event[i] has Event instance then comparison way is different then strings comparison.

You need to override equals method in you class ex:

@Override
public boolean equals(Object ob) {
    if (ob == null)
        return false;
    if (ob instanceof Event) {
         Event e = (Event)ob;
         return this.someStringValue.equals(e.someStringValueItHas); // compare all values you want like this
    }
        return false;
}

here we check correct instance of class then its properties if they are equal or not.

Ashwani
  • 3,463
  • 1
  • 24
  • 31