Wrong line numbers in error messages when using .java files

My sketch contains two files:

  • BrokenErrorMessage.pde, whose content is irrelevant, and
  • SomeJavaCodeWithErrors.java:
    public class SomeJavaCodeWithErrors // line 1
    {                                   // line 2
      public static int testMe()        // line 3
      {                                 // line 4
        // provoking an error!          // line 5
        return asdflkj;                 // line 6, error, but it is reported be on line 3
      } 
    }
    

When I hit the “play” button in the Processing-IDE, I, of course, do get an error:
The variable "asdflkj" does not exist, but the source code location refers to line 3.

However, I would have expect the line number of the error not to be 6 and instead be 3.

What I am doing wrong? How I can get processing to report correct line numbers?

Just before a function is run, a stack is created w/ enough room to fit all parameters & local variables that function is gonna need.

So if we happen to have any non-existent variable names, those are gonna be detected at the very 1st line of that function.

1 Like

That is true, a stack frame is created when a function is executed, but that is not the case here because this compilation error will prevent the sketch being executed. If we add another error Processing reports both errors to be on line 3 :-1:

public class SomeJavaCodeWithErrors // line 1
{                                   // line 2
  public static int testMe()        // line 3
  {                                 // line 4
    // provoking an error!          // line 5
    return asdflkj;                 // line 6, variable does not exist
  }                                 // line 7
                                    // line 8 
  int xxx                           // line 9, missing semicolon
}

In Eclipse it highlights the two lines as having errors.

Screenshot 2024-02-23 at 12.09.07

This appears to be a Processing issue.

1 Like

Before blaming Processing, 1st we need to check which line the “javac” compiler itself would report; b/c Processing relies 100% on it!

Eclipse is a full-featured IDE, and can do static analysis even before the compilation step!

3 Likes

This is the result I’ve got from @quirin’s posted example using “javac” 21.0.2:

C:\Users\Gamer>javac SomeJavaCodeWithErrors.java
SomeJavaCodeWithErrors.java:6: error: cannot find symbol
return asdflkj; // line 6, error, but it is reported be on line 3
^
symbol: variable asdflkj
location: class SomeJavaCodeWithErrors
1 error

It infers error is at line 6; but it was 1st reported at line 3!

So Processing is half-right, and it also matches the behavior that all local variables are determined just before a function is actually run.

P.S.: I’ve moved the symbol asdflkj to a println() instead and it only reports that specific line now.
Perhaps b/c the unknown symbol was originally at a return statement, it’s also considered a function error too.

2 Likes

That would make sense because the return statement is an integral part of the function so an error there makes the function in error.

2 Likes

I am not sure whether it is due to the return statement. I get error for void-returning methods, as well as for statements outside methods.

public class SomeJavaCode
{
  // asdoifn;// Uncomment to get an error on line 2  
  
  public void noReturn()
  {
    // Uncomment the following line to get an error on line 3
    // sdjfas       
    System.out.println("Hello!");
  }
  
  public static int testMe()
  {
    int a = 3;
    int b = 5;
    
    // Uncomment the following line to get an error on line 3
    //asdflkj;
    
    int c = a + b;
    return c;   
  } 
  
  public static int testMeAgain()
  {
    int a = 3;
    int b = 5;
    
    // Uncomment the following line to get an error on line 3
    //asdflkj;
    
    int c = a + b;
    return c;      
  }
   //asdoifn; // Uncomment to get an error on line 3
}

javac states that the error is on line 6 and the other bit -
// line 6, error, but it is reported be on line 3

is just a comment in the source code and has no bearing on javac’c error report.

So it seems that javac can find and report the correct error line but Processing reports a different line number for the same error.

Seems to me like an issue for the Processing team to resolve.

2 Likes

Should I go ahead an create an issue at Issues · processing/processing · GitHub? Is that the right palce?

That’s the correct place to raise an issue. First search any open issues to see if anyone else has reported the same problem. I doubt it will be given a high priority because of the low impact on usability .

1 Like

Thanks. I’ll try my best :slight_smile:

I for one, think that reporting errors at the wrong source code location is pretty severe. It dramatically degrates usability and productivity.

But let’s see what happens :slight_smile: