# NPE on a synchronized(this) statement?

**URL:** <https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904>\
**Category:** Coding Questions\
**Created:** [May 4, 2019, 10:40am UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904 "2019-05-04T10:40:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [May 4, 2019, 10:40am UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/1 "2019-05-04T10:40:54Z")

</div>

Tanctikon is an enclosing instance of the class this synchronized statement is called from.

Hmmm…

 ![true](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/414c59f6c9db4a5761b450f7e2f4a29fec88a4a2.png)

---

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [May 4, 2019, 3:59pm UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/2 "2019-05-04T15:59:25Z")

</div>

here another image where you get a null pointer exception directly on a synchronization with this.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/c/cf3f5af5ac9c8015ee3989c4b18afc93731e1896.png)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [May 6, 2019, 4:08pm UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/3 "2019-05-06T16:08:34Z")

</div>

Hmm. I don’t get an error with this:

```auto
void draw(){
  synchronized(this){
  }
}

```

…and I don’t have your code to test / debug – can you try to break your screenshot down to a shareable [MCVE](https://stackoverflow.com/help/mcve) that still exhibits the NullPointerException?

I don’t use synchronized in Processing – but one community member who I believe is a [frequent user of synchronized](https://stackoverflow.com/questions/27805748/explain-the-use-of-the-java-word-synchronized-in-a-processing-sketch) is @quark.

---

<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:** [May 6, 2019, 4:45pm UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/4 "2019-05-06T16:45:51Z")

</div>

The only reason for using the `synchronized` keyword is if you are using multiple threads. If you are not using multiple threads, and I can’t see anything in the images to suggest you are) then you should avoid using the `synchronized` keyword.

When you execute a Processing sketch the code is wrapped up in a class of the same name as the sketch. So in your sketch **Tanctikon** the code will be enclosed in a class called `Tanctikon`. So in your code  
`if(TakTanctikon.this == null) print("wtf");`  
is the same as  
`if(this == null) print("wtf");`  
It is _ **impossible** _ for the value of `this` to be `null` hence the error message in the left image.

As regard to the NPE if you look at your code you have two opening braces `{` following the `synchronized(this)` so I suspect you have included too much code in the scope of synchronised statement. This is born out the last post by @jeremydouglass.

> [@jeremydouglass](#):
>
> I don’t use synchronized in Processing – but one community member who I believe is a [frequent user of synchronized](https://stackoverflow.com/questions/27805748/explain-the-use-of-the-java-word-synchronized-in-a-processing-sketch) is @quark.

Just to set the record straight I don’t use this keyword a lot 😀. In the stackoverflow discussion it is used on secondary windows created with G4P. In early versions of Processing I used to get interference between the multiple draw() methods so made the G4P windows draw methods synchronized. It probably isn’t needed now.

---

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [May 6, 2019, 8:24pm UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/5 "2019-05-06T20:24:15Z")

</div>

Yes, the screenshots may not be very representative, but I just wanted to showcase the weirdness of the example.  
It doesn’t make sense that a synchronized locked on this could produce a NullPointer. A possibility would be that it is a traceback bug in Processing that refers the NullPointer to the line of the synchronized statement when it is thrown inside it, but I doubt it, since the same error occured in different contexts.

I used the qualified name in the example because it was an inner class of Tanctikon, but enclosing instances can’t be null as well, thus the compile-time warning. I used this to demonstrate that it seemed absurd to me how synchronized(this) could throw a NP.  
Even when it did show the NullPointerException in the right picture, the if statement didn’t evaluate to true and print “wtf”, thus pointing more into the direction that “this” is likely not the cause of this NPE.

I’ll try to reproduce this problem in a MCVE after finishing writing this.

---

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [May 7, 2019, 6:16am UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/7 "2019-05-07T06:16:02Z")

</div>

Is there a way to see the full stacktrace within Processing? That would help a lot.

---

<div class="post-metadata">

**Author:** ![LoliProcessor](https://avatars.discourse-cdn.com/v4/letter/l/6a8cbe/32.png) [@LoliProcessor](https://discourse.processing.org/u/LoliProcessor)\
**Post date:** [May 7, 2019, 8:33am UTC](https://discourse.processing.org/t/npe-on-a-synchronized-this-statement/10904/8 "2019-05-07T08:33:16Z")

</div>

I found the culprit, it’s a similar cause to [this SO post](https://stackoverflow.com/questions/12188983/why-would-i-get-a-nullpointerexception-on-a-synchronized-lock).  
When I synchronize on a block and a NPE gets thrown inside it, it refers it to the synchronized statement itself.

Basically my synchronization didn’t cover all concurrent Thread accesses and one Thread would change a variable to null, which would cause a NPE inside the synchronized statement.

Edit:  
I don’t know how I didn’t think of just catching the exception and printing the stack trace myself.  
As I suspected, the NPE from a line of code within the brackets of the statement, but if I don’t catch it Processing refers the reason to the synchronized statement, which could be reasoned since it’s a statement on its own.  
Considering [someone else](https://stackoverflow.com/questions/12188983) faced a similar problem as well, it likely has something to do with the implementation of synchronized and it’s stack tracing in various IDEs.
