Error Message 'Cannot Be Resolved or Is Not a Field'

Right now I'm studying the chain of responsibility design pattern and am using Eclipse.

And I'm trying to compile this code, but I have the compiling error "isLast cannot be resolved or is not a field":

public class OverFive implements Discount {
    private Discount next; //
    public boolean isLast = false;

    public void setNext(Discount next, boolean Last) {
        this.next = next;
        this.next.isLast = Last; // Here is the error.
    }

    public double DoDiscount(Budget budget) {
        if (budget.getValue() > 500) {
            return budget.getValue() * 0.10;
        }
        if (this.next.isLast == false) {
            return next.DoDiscount(budget);
        }
        else {
            return 0;
        }
    }
}

And now, here is the interface:

public interface Discount {

    double DoDiscount(Orcamento orcamento);
        void setNext(Discount next, boolean Last);
    }

2 Answers

Here's one recommendation: study the Sun Java coding standards and take them to heart. You're breaking them too frequently in this small code sample.

Java is case sensitive: "discount" is not the same as "Discount"; "dodiscount" is not the same as "DoDiscount".

public interface Discount {

    double doDiscount(Orcamento orcamento);
    void setNext(Desconto next, boolean last);
    void setLast(boolean last);
} 

And the implementation:

public class OverFive implements Discount {
    private Desconto next;
    private boolean last = false;

    public void setLast(boolean last) {
        this.last = last;
    }

    public void setNext(Desconto next, boolean last) {
        this.next = next;
        this.setLast(last);
      }

    // this method is utter rubbish.  it won't compile.
    public double doDiscount(Orcamento budget){
        if (budget.getValue() > 500){
            return budget.getValue() * 0.10;
        }if (this.next.isLast == false){
            return next.discount(budget);
        }else{
            return 0;
        }   
    }
}

I think this code is more than a little confusing. No wonder you're having problems.

6

I'm not sure if this is the same type of issue as the above, but I had the same error. In my case , I was using an older version of Eclipse that apparently didn't like having a class with same name as the package. I fixed the issue by giving the package a different name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest