# Wrong line numbers in error messages when using .java files

**URL:** <https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983>\
**Category:** Beginners\
**Created:** [February 23, 2024, 10:33am UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983 "2024-02-23T10:33:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![quirin](https://avatars.discourse-cdn.com/v4/letter/q/dc4da7/32.png) [@quirin](https://discourse.processing.org/u/quirin)\
**Post date:** [February 23, 2024, 10:33am UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/1 "2024-02-23T10:33:20Z")

</div>

My sketch contains two files:

- `BrokenErrorMessage.pde`, whose content is irrelevant, and
- `SomeJavaCodeWithErrors.java`:

```auto
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?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 23, 2024, 10:50am UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/2 "2024-02-23T10:50:51Z")

</div>

> [@quirin](#):
>
> However, I would have expect the line number of the error _not_ to be `6` and instead be `3`.

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 23, 2024, 12:17pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/3 "2024-02-23T12:17:48Z")

</div>

> [@GoToLoop](#):
>
> Just before a function is run, a stack is created w/ enough room to fit all parameters & local variables that function is gonna need.

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 👎

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/8/a83d3afeb100a97a7e7680915366a903ce715f37.png)

This appears to be a Processing issue.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 23, 2024, 1:15pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/4 "2024-02-23T13:15:32Z")

</div>

> [@quark](#):
>
> This appears to be a Processing issue.

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!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 23, 2024, 1:31pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/5 "2024-02-23T13:31:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 23, 2024, 2:50pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/6 "2024-02-23T14:50:11Z")

</div>

> [@GoToLoop](#):
>
> Perhaps b/c the unknown symbol was originally at a `return` statement,

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

---

<div class="post-metadata">

**Author:** ![quirin](https://avatars.discourse-cdn.com/v4/letter/q/dc4da7/32.png) [@quirin](https://discourse.processing.org/u/quirin)\
**Post date:** [February 23, 2024, 6:53pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/7 "2024-02-23T18:53:00Z")

</div>

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.

```processing
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
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 23, 2024, 7:12pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/8 "2024-02-23T19:12:47Z")

</div>

> [@GoToLoop](#):
>
> 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!

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.

---

<div class="post-metadata">

**Author:** ![quirin](https://avatars.discourse-cdn.com/v4/letter/q/dc4da7/32.png) [@quirin](https://discourse.processing.org/u/quirin)\
**Post date:** [February 23, 2024, 7:40pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/9 "2024-02-23T19:40:05Z")

</div>

Should I go ahead an create an issue at [Issues · processing/processing · GitHub](https://github.com/processing/processing/issues)? Is that the right palce?

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 24, 2024, 8:50am UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/10 "2024-02-24T08:50:18Z")

</div>

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 .

---

<div class="post-metadata">

**Author:** ![quirin](https://avatars.discourse-cdn.com/v4/letter/q/dc4da7/32.png) [@quirin](https://discourse.processing.org/u/quirin)\
**Post date:** [February 26, 2024, 5:08pm UTC](https://discourse.processing.org/t/wrong-line-numbers-in-error-messages-when-using-java-files/43983/11 "2024-02-26T17:08:39Z")

</div>

Thanks. I’ll try my best 🙂

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 🙂
