Search code examples
javamethodsenumsvalue-of

How do I get those 2 errors to go away? in and valueOf methods - Java


I have struggling a bit with those 2 small errors because Eclipse IDE "said so." I will point out the errors. To be honest, I don't know how to explain deeply about those errors. I thought they were simple and I cannot get the errors to go away.

    ScheduledExecutorService timer = Executors.newScheduledThreadPool (1);
    timer.scheduleAtFixedRate(new Runnable() 
    {
        public void run() 
        {
            if (lists. size ()> 0) 
            {
                boolean lighted = Lamp.valueOf(Road.this.name).isLighted(); //According to Eclipse, "The method valueOf(Class<T>, String) in the type Enum<Lamp> is not applicable for the arguments (String)"

                if (lighted) 
                {
                    System.out.println(lists.remove(0) + "is traversing!");
                }
            }
        }
    }, 1,1, TimeUnit. SECONDS);

and another error in my different class my package

public Lamp Blackout() 
{
    this.lighted = false;

    if (opposite != null) 
    {
        Lamp.valueOf(opposite).in.Blackout(); //in cannot be resolved or is not a field. It suggests me to create enum constant, which I did and it wouldn't work either. 
    }

    Lamp nextLamp = null;

    if (next != null) 
    {
        nextLamp = Lamp.valueOf(next);
        System.out.println("Green" + name () + "--------> switch to" + next);
        nextLamp.light();
    }
    return nextLamp;
}

Solution

  • Shooting in the dark, because you haven't disclosed all relevant code, but you could try adding the missing parameter for valueOf here:

    boolean lighted = Lamp.valueOf(Lamp.class, Road.this.name).isLighted();
    

    and calling the in() method here:

    Lamp.valueOf(Lamp.class, opposite).in(Blackout());
    

    Please follow Java code style conventions; method names should start with a lowercase letter, so the blackout method signature should look like this instead:

    public Lamp blackout()
    

    Without seeing the code for the Lamp enum, it's impossible to know what the exact problem in the latter case is.