title: How do I find the reason for Exception in thread "main" java.lang.StackOverflowError
body: <p>To run my code, just copy and paste it into your IDE - everything is there. This is a flight monitoring program which uses the Observer-Observable design pattern. FlightStautsMonitor is the Observer which monitors the flight status (an integer) of a Flight. FlightStatusChangeEvent is a class which notes the change of status of a flight. 
  Demo is the driver class, or class with main. </p>
  
  <p>My problem is that I am not sure if I am using the Observer and Observable API. I am getting an output with runtime error -</p>
  
  <pre><code>Old flight status
  F:1, S: 0
  F:2, S: 0
  F:3, S: 0
  F:4, S: 0
  F:5, S: 0
  Exception in thread "main" java.lang.StackOverflowError
  at java.util.Vector.toArray(Unknown Source)
  at java.util.Observable.notifyObservers(Unknown Source)
  at demo.Flight.updateStatus(Demo.java:40)
  at demo.FlightStatusMonitor.update(Demo.java:91)
  at java.util.Observable.notifyObservers(Unknown Source)
  </code></pre>
  
  <p>What is the reason for the problem and how do i fix this code ?</p>
  
  <pre><code>//package demo;
  
  import java.util.*;
  
  class FlightStatusChangeEvent{
  
  int status;
  Flight flight;
  
  FlightStatusChangeEvent(int statusCode, Flight flight){
  
      this.status = statusCode;
      this.flight = flight;
  
  }
  
  public int getStatus(){return this.status;}
  
  public Flight getFlight(){return this.flight;}
  
  
  }
  
  class Flight extends Observable{
  
  int status;// 0 = on time, -1 = late, +1 = before scheduled time
  String flightCode;
  
  Flight(String flightCode){
  
      this.flightCode = flightCode;
  
  }
  
  public void updateStatus(int statusCode){
  
      this.status = statusCode;
      FlightStatusChangeEvent fsce = new FlightStatusChangeEvent(status, this);
      setChanged();
      notifyObservers(fsce);
  
  }
  
  
  public String getFlightCode(){return this.flightCode;}
  
  public String toString(){return "F:" + flightCode + ", S: " + status;}
  
  }
  
  
   class FlightStatusMonitor implements Observer{
  
  public static ArrayList&lt;Flight&gt; flights = new ArrayList&lt;Flight&gt;();
  
  static{
  
      for(int i = 1; i &lt; 6; i++){
  
          Flight ff = new Flight("" + i);
          flights.add(ff);        
      }
  
  }
  
  //keep calling this method every 10 sec to get latest flight status
  public static void displayStatusOfFlights(){
  
      //print all flight statuses in array list - flights
      for(Flight fl : flights){
          System.out.println(fl);
  
      }
  
  }
  
  public void update(Observable flight, Object flightStatusEvent){
  
      FlightStatusChangeEvent fsce = (FlightStatusChangeEvent) flightStatusEvent;     
      Flight fl = fsce.getFlight();
      String code = fl.getFlightCode();
      int status = fsce.getStatus();
  
      //find the flight in array flights and then update its status
      for(int i = 0; i &lt; flights.size(); i++){
  
          Flight fli = flights.get(i);
          String flCode = fli.getFlightCode();
  
          if(flCode.equals(code)){
              fli.updateStatus(status);
              System.out.print("Flight status updated !");
  
              break;
  
          }
  
      }
  
  
  }
  
  }
  
  
   public class Demo{
  
  public static void main(String[]args){
  
      FlightStatusMonitor fsm = new FlightStatusMonitor();
      System.out.println("Old flight status");
      ArrayList&lt;Flight&gt; fllist = fsm.flights;
      fsm.displayStatusOfFlights();
  
      for(Flight fl : fllist ){
  
          fl.addObserver(fsm);
  
      }
  
      fsm.flights.get(1).updateStatus(-1);
      System.out.println("New flight status");        
      fsm.displayStatusOfFlights();
  
  }
  
  
  }
  </code></pre>