-1

When I try to compile and run this section of code, I get this erro:

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: listener cannot be resolved to a variable

at FormPanel.setFormListener(FormPanel.java:273)
at MainFrame.<init>(MainFrame.java:37)
at App$1.run(App.java:10)

The code is this:

//  public void setFormListener(DailyFormListener listener) {
//      this.formDayListener = listener;
//      
//  }
public void setFormListener(Object o) {
    if (o instanceof DailyFormListener) {
        this.formDayListener = listener;
    }
    else if (o instanceof GeneFormListener) {
        this.geneFormListener = listener;
    }
    else if (o instanceof LabFormListener) {
        this.labFormListener = listener;
    }
}

That is from the line 273 section. My question is, it works when run with the section above that is commented out, but now in the section that is uncommented. What do I need to change to allow setFormListener to be general enough to handle this? I can provide more code or information as needed.

Stats4224
  • 778
  • 4
  • 13

2 Answers2

5

Your variable is o, not listener.

Change your code to :

public void setFormListener(Object o) {
    if (o instanceof DailyFormListener) {
        this.formDayListener = (DailyFormListener)o;
    }
    else if (o instanceof GeneFormListener) {
        this.geneFormListener = (GeneFormListener)o;
    }
    else if (o instanceof LabFormListener) {
        this.labFormListener = (LabFormListener)o;;
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
0

You have to cast your Object to the concrete listener type

public void setFormListener(Object o) {
    if (o instanceof DailyFormListener) {
        this.formDayListener = (DailyFormListener)o;
    }
    else if (o instanceof GeneFormListener) {
        this.geneFormListener = (GeneFormListener)o;
    }
    else if (o instanceof LabFormListener) {
        this.labFormListener = (LabFormListener)o;
    }
}
Jens
  • 67,715
  • 15
  • 98
  • 113