Why do I keep getting "Evaluations must contain either an expression or a block of well-formed statements"?
Asked Answered
A

7

23

In my code, I am trying to output the value of src in the expressions window.

public void doIt() {
    String src = "test";
    System.out.println(src);
}

In Eclipse. I set a breakpoint on a line 3, and I open the "Expressions" window.

I add an expression src to evaluate, and I get Evaluations must contain either an expression or a block of well-formed statements

I've used the Expressions features... COUNTLESS times in my years of Java debugging.. Why does this happen now?

I've just recently started using Eclipse Juno.. vs Indigo. Did they change the way Expressions work?

Aw answered 25/6, 2013 at 19:47 Comment(5)
did you take that screenshot while the debugger was on line 2, 3 or 4? if not then src is out of scopeAdduce
@MarcoForberg yessir - i'm positive. If it WAS out of scope, it would say "src cannot be resolved to a variable"Aw
Eclipse has the bad habit of, God knows why, stopping the evaluation of expressions during a debug process without any reasonable cause, event when your expressions are well formed and all. Most likely a memory issue or some sort of corruption behind the scenes. In my case, cleaning the project and rebuilding it from scratch, closing and reopening the projects and closing and reopening Eclipse (in no particular order) get me on my way.Franco
If it's a bug in Eclipse, noting the precise version ID of Eclipse might be helpful.Channa
@sircapsalot, If there is a method named src in the same class, or class named src in the same package, or a top level package by that name, then it might conclude that you're using an identifier that cannot stand alone in an expression.Glebe
R
15

If your code use any generics, you may want to check this bug:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=341232

Occurs in all version of Eclipse up to 4.2. In short certain generics expressions cause Eclipse to completely fail on any evaluation (please see this example: https://bugs.eclipse.org/bugs/attachment.cgi?id=224760). Not sure whether your code uses any generics, but if so, this may be it. Note that it is enough to have one of the troublesome generics somewhere in your class, not necessary in your method.

Relinquish answered 10/1, 2014 at 9:24 Comment(3)
it is using generics.. this is interesting.Aw
Up to 4.6 confirmed - maybe due to another reason but I can't see slip.Horick
I get this as well with v4.23. I debug inside the body of a Comparator<Something>.Overwrite
M
5

I just spent TONS of time to figure out that if you will create a package "Foo" and inside this package you'll create class called "Foo", like this:

package Foo;

public class Foo{
    public Foo () {};
}

After the point when you use this class first time in your program, you will not be able to use expressions anymore:

import Foo.Foo; //this is the devil i think

public static void main(String[] args){
    EventQueue.invokeLater(new Runnable(){
        public void run(){
            //debug expressions works fine
            Foo tmp = new Foo();
            //debug expressions wouldn't work anymore
        }
    });
}

This bug can be reprodused up to current Eclipse Neon 4.7.

Muckworm answered 4/11, 2016 at 3:28 Comment(1)
+1 for the effort to reduce it to this simple test case. I can still reproduce this now on my Eclipse 2021-09 (4.21.0). Is there an issue reported for Eclipse?Alost
P
2

Check if you have updated version of Eclipse, looks like this issue is fixed in Eclipse 3.3

My Eclipse Version is 3.8.2 and if I evaluate the expression on line 2 then I am also receiving the same error but at line 3 its evaluating properly.

Punke answered 25/6, 2013 at 20:23 Comment(0)
H
2

I had the same problem and I remove a generic method in my code. It work for me.

Horticulture answered 3/12, 2014 at 11:37 Comment(0)
I
2

(Assuming you're compiling 1.5+ code)I had the same problem, here's what I did to fix it:

  • Ensure that your java compiler version is 1.5+
  • Fix all syntax/class related errors pertaining to that class. I had a number of missing dependencies in my classpath.
  • make sure the JRE is also 1.5+ for that specific project
Intestinal answered 27/3, 2018 at 19:32 Comment(0)
A
1

According to https://mcmap.net/q/585962/-expression-evaluation-stopped-working-in-eclipse-sts, this happens when the class contains a generic method like

public <T extends something> T myMethod() {};
Aho answered 9/1, 2020 at 18:39 Comment(0)
H
0

Eclipse said I had compilation errors. Even though Maven builds fine and the project runs. I had to go to Properties | Java Build Path | Library | Add External JARs... | Add jar eclipse wants to resolve errors. After the errors were removed, I'm able to evaluate expressions.

Eclipse 4.27

Haematocele answered 6/2 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.