Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null pointer exception: scalar extension of a java class

I'm playing around with using Scala for my processing projects.

I've extended the Rect class from ToxicLibs, but I'm getting a null pointer exception when I try to call rect(toxi.geom.Rect r) from within a method.

import toxi.processing.ToxiclibsSupport
import toxi.geom.{ Vec2D, Rect }

class Scope(x: Float, y: Float, width: Float, height: Float) extends Rect(x, y, width, height) {

  def this(r: Rect) {
    this(r.x, r.y, r.width, r.height)
  }

  def draw(gfx: ToxiclibsSupport) {
    gfx.rect(this) // null pointer exception occurs here 
  }

}

This is the code that calls the draw method:

import processing.core.PApplet
import toxi.processing.ToxiclibsSupport
import toxi.geom.Rect

class ScalaP5Test extends PApplet {

  var gfx = new ToxiclibsSupport(this)

  override def setup() {
    size(1000, 800)
  }

  override def draw() {
    var scope = new Scope(100, 200, 400, 300)
    scope.draw(gfx) // draw called here
   }
}

any ideas?

like image 753
brendanzab Avatar asked Dec 21 '25 07:12

brendanzab


1 Answers

Ahh, I got it:

class ScalaP5Test extends PApplet {

  var gfx = null

  override def setup() {
    gfx = ToxiclibsSupport(this)

    ...

  }

  ...

}

Then you can reuse the variable in other functions. I'm not sure if that's the best way, so if you have a better way, let me know!

like image 195
brendanzab Avatar answered Dec 22 '25 21:12

brendanzab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!