2

In my Java 8 code,

public ChangePersonsName(String email, final String password, final String wantedUsername, final String uuid, final long time, int latency, int[] requests, int[] proxyRequests) throws IOException {
    final AtomicReference<Object> token = new AtomicReference<Object>();
    final AtomicReference<ArrayList<?>> newHeaders = new AtomicReference<ArrayList<?>>();
    new Thread(() -> {
        boolean lock = true;
        while (lock) {
            if (time - System.currentTimeMillis() > 60000) continue;
            Map<Header[], String> loginResults = null;
            try {
                loginResults = this.login(email, password, uuid);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
           String token = loginResults.entrySet().iterator().next().getValue();
            Header[] headers = loginResults.entrySet().iterator().next().getKey();
            newHeaders.set(new ArrayList<Object>());
            for (Header header : headers) {
                if (!header.toString().startsWith("Set-Cookie:")) continue;
                ((List<BasicHeader>)newHeaders.get()).add(new BasicHeader("Cookie", header.toString().split("Set-Cookie: ")[1]));
            }
            lock = false;
        }
    }
    ).start();
    new Timer().schedule(new TimerTask(){

You'll notice that

String token = loginResults.entrySet().iterator().next().getValue(); 

throws a compile error,

Lambda expression's local variable token cannot redeclare another local variable defined in an enclosing scope.

My question is, How would one go about fixing this? I'm pretty new to Java, I should probably know how to fix this, but i don't.

3
  • final AtomicReference<Object> token = new AtomicReference<Object>(); You have two variable with the same name (token). This is said in the message local variable token cannot redeclare another local variable defined in an enclosing scope. Commented Jan 13, 2017 at 8:41
  • 2
    "How would one go about fixing this?" Name it something else. Commented Jan 13, 2017 at 8:46
  • 1
    Possible duplicate of variable is already defined in method lambda Commented Jan 13, 2017 at 8:54

1 Answer 1

5

You already have variable with name token in this scope. You've declared it in 2nd row. To fix just rename 2nd variable:

String newToken = loginResults.entrySet().iterator().next().getValue(); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.