Raylib-J icon indicating copy to clipboard operation
Raylib-J copied to clipboard

`GetMouseWheelMove` results in null pointer exception

Open ejenk0 opened this issue 1 year ago • 4 comments

Describe the bug Calling Raylib.core.GetMouseWheelMove results in a:

Exception in thread "main" java.lang.NullPointerException: Cannot read field "x" because "this.input.mouse.currentWheelMove" is null
	at com.raylib.java.core.rCore.GetMouseWheelMove(rCore.java:2316)

Code

  private void zoomCamera(float delta) {
    camera.zoom = Math.clamp(camera.zoom + delta, ZOOM_MIN, ZOOM_MAX);
  }

  public void update(Raylib r) {
    // Mouse drag camera controls
    if (r.core.IsMouseButtonDown(MOUSE_BUTTON_MIDDLE.ordinal())) {
      Vector2 delta = r.core.GetMouseDelta();
      delta = Vector2Scale(delta, -CAMERA_PAN_ZOOM_FACTOR / camera.zoom);

      shiftCamera(delta);
    }

    // Mouse zoom camera controls
    float mouseWheelMovement = r.core.GetMouseWheelMove();
    if (mouseWheelMovement != 0) {
      zoomCamera(mouseWheelMovement * ZOOM_SPEED);
    }
  }

Desktop (please complete the following information):

  • OS: MacOS Sonoma

Additional context Raylib-J Version: Release 0.5.2

Known workaround This only seems to be a problem on the first frame/invocation for me, so just checking that the result of GetMouseWheelMoveV is not null is sufficient.

ejenk0 avatar May 19 '24 11:05 ejenk0

Similarly with GetMouseWheelMoveV:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.raylib.java.raymath.Vector2.getY()" because the return value of "com.raylib.java.core.rCore.GetMouseWheelMoveV()" is null

ejenk0 avatar May 19 '24 11:05 ejenk0

Screenshot 2024-05-19 at 9 57 04 pm

It appears to only be null on the first invocation. Here I am printing it out every frame

ejenk0 avatar May 19 '24 11:05 ejenk0

Screenshot 2024-05-19 at 9 59 31 pm

This is weird. In the decompiled Mouse class it has an empty initialiser, despite the fact that I just compiled it from source and I can see that it should have one...

ejenk0 avatar May 19 '24 12:05 ejenk0

The empty constructor is due to code optimization during compilation. You can see the lines that were previously in the default constructor have been moved to be inline with the variables.

However you can see I forgot to add the currentWheelMove and previousWheelMove initialization. Easy fix, so I'll be able to get this fixed rather quick.

CreedVI avatar May 19 '24 13:05 CreedVI