# \[SOLVED\] I get a NullPointerException when switching from the default rendering technique to JavaFX 2D

**URL:** <https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601>\
**Category:** Libraries\
**Created:** [August 16, 2023, 8:06pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601 "2023-08-16T20:06:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 16, 2023, 8:06pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/1 "2023-08-16T20:06:32Z")

</div>

Hi everyone,

I have a huge code of about 4000 lines, which works fine, except for the rendering speed. For this reason, I decided to switch from the default rendering technique to JavaFX 2D. I downloaded the library and imported it:  
`import processing.javafx.*;`

Then modified the `size()` in the `setup()`:

```auto
void setup() {
size(1820,660,FX2D); 
frameRate(100);
.
.
.
}

```

When I run my program, as usual, the following window appears, where I have to write my name, and then press ‘enter’.

 ![forum_processing](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/2/1276e978e74a4b73051811e395f653d82ce42d09.png)  
However, when I press ‘enter’, I get a very long `NullPointerException`:

```auto
NullPointerException
NullPointerException
java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "this.name" is null
	at processing.javafx.PGraphicsFX2D$FontCache$Key.hashCode(Unknown Source)
	at java.base/java.util.HashMap.hash(HashMap.java:338)
	at java.base/java.util.HashMap.getNode(HashMap.java:568)
	at java.base/java.util.LinkedHashMap.get(LinkedHashMap.java:441)
	at processing.javafx.PGraphicsFX2D$FontCache.get(Unknown Source)
	at processing.javafx.PGraphicsFX2D.handleTextFont(Unknown Source)
	at processing.javafx.PGraphicsFX2D.textFontImpl(Unknown Source)
	at processing.core.PGraphics.textFont(PGraphics.java:4385)
	at controlP5.ControlFont.adjust(Unknown Source)
	at controlP5.Label$SinglelineLabel.draw(Unknown Source)
	at controlP5.Label$SinglelineLabel.draw(Unknown Source)
	at controlP5.Label.draw(Unknown Source)
	at controlP5.Button$ButtonView.display(Unknown Source)
	at controlP5.Button$ButtonView.display(Unknown Source)
	at controlP5.Controller.draw(Unknown Source)
	at controlP5.ControllerGroup.drawControllers(Unknown Source)
	at controlP5.ControllerGroup.draw(Unknown Source)
	at controlP5.ControlWindow.draw(Unknown Source)
	at controlP5.ControlWindow.draw(Unknown Source)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1309)
	at processing.core.PApplet.handleMethods(PApplet.java:1456)
	at processing.core.PApplet.handleDraw(PApplet.java:2106)
	at processing.javafx.PSurfaceFX$1.handle(Unknown Source)
	at processing.javafx.PSurfaceFX$1.handle(Unknown Source)
	at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:239)
	at javafx.graphics/com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:197)
	at javafx.graphics/javafx.animation.Timeline.doPlayTo(Timeline.java:172)
	at javafx.graphics/javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
	at javafx.graphics/com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:120)
	at javafx.graphics/javafx.animation.Animation.doTimePulse(Animation.java:1189)
	at javafx.graphics/javafx.animation.Animation$1.lambda$timePulse$0(Animation.java:207)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
	at javafx.graphics/javafx.animation.Animation$1.timePulse(Animation.java:206)
	at javafx.graphics/com.sun.scenario.animation.AbstractPrimaryTimer.timePulseImpl(AbstractPrimaryTimer.java:343)
	at javafx.graphics/com.sun.scenario.animation.AbstractPrimaryTimer$MainLoop.run(AbstractPrimaryTimer.java:266)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:571)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:555)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:548)
	at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(QuantumToolkit.java:353)
	at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
	at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
	at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
	at java.base/java.lang.Thread.run(Thread.java:833)
NullPointerException

```

When I go back to the default renderer by removing `FX2D` from `size()`, everything works again…

Does anyone have an idea of what the problem could be?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 16, 2023, 8:13pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/2 "2023-08-16T20:13:58Z")

</div>

Hello @MrImskiy,

You must add the JavaFX library with Processing 4.3:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/e/6/e6a39fe4078216a95777916a6f767bcedc4e3bb4.png)

Test with String.hashCode():

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/0/b07633be3ac29c220bdcc9fedd4222f92a3d0d1d.png)

Important topic related to JavaFX:

> [@JavaFX Unable to add modules to Processing IDE](https://discourse.processing.org/t/javafx-unable-to-add-modules-to-processing-ide/41272):
>
> The following JavaFX code will not run in Processing 4 due to absence of JavaFX modules (runs ok in version 3.5.4 minus the first line). I have tried many times adding the modules to the libraries folder or drag 'n dropping them on the sketch, but neither have worked. Text and graphics work fine with the FX2D renderer, but the controls won’t work. It is possible to create graphic buttons and trap mouse clicks or use SwingNode and add Swing components, but it just seems like we should be able …

`:)`

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 16, 2023, 8:22pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/3 "2023-08-16T20:22:51Z")

</div>

Thanks for your reply @glv !

I have no issue with adding and importing the library, and your test code works fine as well, so the problem is somewhere else… 😕

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 16, 2023, 8:24pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/4 "2023-08-16T20:24:23Z")

</div>

> [@MrImskiy](#):
>
> the problem is somewhere else…

Maybe…

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/2/628e769193d081f207ae116f12364bb7fde665ac.png)

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/7/3/7349dbbb73b848fc9f5d2078b5762420fa7b8201.png)

You will sort it out!

`:)`

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 16, 2023, 8:27pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/5 "2023-08-16T20:27:02Z")

</div>

I have tried P2D, which made the rendering faster, but not as fast as needed. That’s why I wanted to test FX2D.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 16, 2023, 8:43pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/6 "2023-08-16T20:43:06Z")

</div>

Focus on this:

`java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "this.name" is null`

Lots of good stuff here including `NullPointerException`:

> [@I dont know why it keep saying NullPointerException](https://discourse.processing.org/t/i-dont-know-why-it-keep-saying-nullpointerexception/30134/4):
>
> Hello, Some resources that may help now or in future: [https://forum.processing.org/two/discussion/8093/frequently-asked-questions](https://forum.processing.org/two/discussion/8093/frequently-asked-questions) (See Exceptions) [Home · processing/processing Wiki · GitHub](https://github.com/processing/processing/wiki) (See Common Errors)slight_smile

`:)`

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 17, 2023, 11:31am UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/7 "2023-08-17T11:31:24Z")

</div>

Thanks @glv, I will give it a read 🙂

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 17, 2023, 12:08pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/8 "2023-08-17T12:08:08Z")

</div>

I found the error. The problem lies with my cp5 initializations, which is done in the `void keyPressed()` section:

```auto
void keyPressed() {
  cp5 = new ControlP5(this);
  cp5.addButton("open");
  cp5.addButton("closePort");
  cp5.addButton("refresh");
  cp5.addScrollableList("comlist").close();
  String list[] = Serial.list();
  cp5.get(ScrollableList.class, "comlist").addItems(list);
    
  MyController = new ControlP5(this);
  MyController.addSlider("K_factor",0,30,1,slider_K_X,slider_K_Y,slider_W,slider_H); 
  MyController.addSlider("tau_I",0,10,0,slider_tau_I_X,slider_tau_I_Y,slider_W,slider_H);
  MyController.addSlider("tau_D",0,10,0,slider_tau_D_X,slider_tau_D_Y,slider_W,slider_H);
  MyController.addSlider("DB",0,1,0,slider_DB_X,slider_DB_Y,slider_W,slider_H);
}

```

This works perfectly when using the default renderer. But, when I use the JavaFX 2D renderer, that’s when I get the NPE.

So, when I put these initialization lines of code in comment (while JavaFX 2D is on), my program works again, and does not show a NPE.

Then I thought that, maybe if I initialize these cp5 objects in the `setup()` section instead of the `keypressed()` section it will work. But, when I do this, I get a NPE again…

---

<div class="post-metadata">

**Author:** ![MrImskiy](https://avatars.discourse-cdn.com/v4/letter/m/5f8ce5/32.png) [@MrImskiy](https://discourse.processing.org/u/MrImskiy)\
**Post date:** [August 17, 2023, 12:12pm UTC](https://discourse.processing.org/t/solved-i-get-a-nullpointerexception-when-switching-from-the-default-rendering-technique-to-javafx-2d/42601/9 "2023-08-17T12:12:52Z")

</div>

[SOLUTION]

I found a solution for this problem on github:

> In the case of using the FX2D renderer, controlP5’s BitFont can’t be rendered and by attempting to do so the above error message is caused. To work around this, set the font to a non-BitFont by calling `cp5.setFont(createFont("Arial",10));` right after creating an instance of cp5.

> <https://github.com/sojamo/controlp5/issues/36#issuecomment-168880763>
>
> Windows 7, Processing 3.0.1
> 
> Taking a number of examples (e.g. button / slide / …textfield), adding FX2D to the size() constructor consistently results in the following error which prevents the sketch from running:
> 
> \`\`\`
> ControlP5 2.2.5 infos, comments, questions at http://www.sojamo.de/libraries/controlP5
> java.lang.NullPointerException
> at processing.javafx.PGraphicsFX2D$FontCache$Key.hashCode(PGraphicsFX2D.java:1423)
> at java.util.HashMap.hash(HashMap.java:338)
> at java.util.LinkedHashMap.get(LinkedHashMap.java:440)
> at processing.javafx.PGraphicsFX2D$FontCache.get(PGraphicsFX2D.java:1377)
> at processing.javafx.PGraphicsFX2D.handleTextFont(PGraphicsFX2D.java:1470)
> at processing.javafx.PGraphicsFX2D.textFontImpl(PGraphicsFX2D.java:1446)
> at processing.core.PGraphics.textFont(PGraphics.java:4232)
> at controlP5.ControlFont.adjust(Unknown Source)
> at controlP5.Label$SinglelineLabel.draw(Unknown Source)
> at controlP5.Label$SinglelineLabel.draw(Unknown Source)
> at controlP5.Label.draw(Unknown Source)
> at controlP5.Textfield.draw(Unknown Source)
> at controlP5.ControllerGroup.drawControllers(Unknown Source)
> at controlP5.ControllerGroup.draw(Unknown Source)
> at controlP5.ControlWindow.draw(Unknown Source)
> at controlP5.ControlWindow.draw(Unknown Source)
> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> at java.lang.reflect.Method.invoke(Method.java:497)
> at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1395)
> at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1388)
> at processing.core.PApplet.handleMethods(PApplet.java:1582)
> at processing.core.PApplet.handleDraw(PApplet.java:2412)
> at processing.javafx.PSurfaceFX$1.handle(PSurfaceFX.java:75)
> at processing.javafx.PSurfaceFX$1.handle(PSurfaceFX.java:1)
> at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:226)
> at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:184)
> at javafx.animation.Timeline.impl\_playTo(Timeline.java:176)
> at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
> at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:110)
> at javafx.animation.Animation.impl\_timePulse(Animation.java:1102)
> at javafx.animation.Animation$1.lambda$timePulse$26(Animation.java:186)
> at javafx.animation.Animation$1$$Lambda$102/1585508389.run(Unknown Source)
> at java.security.AccessController.doPrivileged(Native Method)
> at javafx.animation.Animation$1.timePulse(Animation.java:185)
> at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
> at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
> at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:521)
> at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:505)
> at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$400(QuantumToolkit.java:334)
> at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$56/874026615.run(Unknown Source)
> at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
> at com.sun.glass.ui.win.WinApplication.\_runLoop(Native Method)
> at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
> at com.sun.glass.ui.win.WinApplication$$Lambda$52/1125138434.run(Unknown Source)
> at java.lang.Thread.run(Thread.java:745)
> handleDraw() called before finishing
> Could not run the sketch (Target VM failed to initialize).
> For more information, read revisions.txt and Help ? Troubleshooting.
> \`\`\`
