0

I'm trying to learn chain of responsibility pattern in Java, In below code if the successor does not exist, it gives a NullPointerException. I want to check if the successor exists like this

if(this.sucessor exists) {
  this.successor.check(Home) 
}

How can I do this?

public void next(HomeStatus Home){
   this.successor.check(Home);
}
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48
lasan
  • 365
  • 2
  • 5
  • 13

1 Answers1

3

Isn't it obvious ?

public void next(HomeStatus Home){
      if(this.successor != null){
          this.successor.check(Home);
     }
}
Emre Acar
  • 920
  • 9
  • 24