microsoft / vscode-java-debug

Java Debugger for Visual Studio Code.
Other
515 stars 326 forks source link

Cannot watch the value of a 2d array "the type of the expression must be an array type but it resolved to int" #1244

Open nathan-gt opened 1 year ago

nathan-gt commented 1 year ago

If you try to watch the value of a 2 dimensional array it will give you "the type of the expression must be an array type but it resolved to int"

image

Environment
Steps To Reproduce
  1. Create a static or dynamically allocated 2d array and give it some random values
  2. Add a breakpoint somewhere
  3. try to access a specific value of the array in the watch window (ex: array[0][1])
Current Result

Unable to access via watch the value of a 2d array when debugging.

Expected Result

Being able to do that

oarcher commented 5 months ago

How is declared your array ? is it double myArray[][], or double[][] myArray ?

From my tests, the debugger works better with double myArray[][], but give error Cannot evaluate because of compilation error(s): The type of the expression must be an array type but it resolved to double. with the other.

testforstephen commented 5 months ago

Could anyone provide some snippet to reproduce the problem?

oarcher commented 5 months ago

Surprisingly, it was little hard to produce a minimal exemple: Using instance variable for array_okand array_nok seems to be mandatory.

public class Test {

  private double[][] array_nok;
  private double array_ok[][];

  public Test(){
    array_nok = new double[][]{{0.,0.},{0.,0.}};
    array_ok = new double[][]{{0.,0.},{0.,0.}};
  }

  public void run() {
    System.out.printf("array_nok[0][0]: %f\n", array_nok[0][0]);
    System.out.printf("array_ok[0][0]: %f\n", array_ok[0][0]);
    System.out.println("breakpoint here");
  }  

  public static void main (String[] args) {
    Test test = new Test();
    test.run();
  }
}

So, after setting a breakpoint on the line System.out.println("breakpoint here");, and using the debug console:

> array_nok[0].length
    Cannot evaluate because of compilation error(s): length cannot be resolved or is not a field.
> array_nok[0][0]
    Cannot evaluate because of compilation error(s): The type of the expression must be an array type but it resolved to double.

It works as expected with array_ok.

Test environment:

java --version
openjdk 17.0.10 2024-01-16
OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
OpenJDK 64-Bit Server VM Temurin-17.0.10+7 (build 17.0.10+7, mixed mode, sharing)

and

Debugger for Java v0.55.0 & pre-release v0.55.2023121302
testforstephen commented 5 months ago

@oarcher thanks for the sample code. I can reproduce it as well. Defining the 2d array as a local variable works, but does not work when accessing it as a field member.