Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

Scala Akka NullPointerException error

I have a Boot class and Drawer actor. So when I press button, I call Starter with parameters. In Drawer actor I'm getting Starter and say to self call Fill. I want to fill rectangle, but when I call Fill, I get an error: null java.lang.NullPointerException. How to solve this problem?

Boot class:

import akka.actor.{Actor, ActorSystem, Props}
import javafx.application.Platform
import scalafx.Includes._
import scalafx.application
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.canvas.Canvas
import scalafx.scene.control.Alert.AlertType
import scalafx.scene.control.{Alert, ButtonType}

object Boot extends JFXApp {

  private val system = ActorSystem("MYsystem")
  private val drawer = system.actorOf(Props(new Drawer(g)), "Drawer")

  val wiDth = 500
  val heiDth = 500
  val canvas = new Canvas(wiDth, heiDth)
  val g = canvas.graphicsContext2D

  private val alert = new Alert(AlertType.Information) {
    title = "Hello Player"
    headerText = "Do you want to start game?"
    contentText = null
    buttonTypes = Seq(ButtonType.Cancel, ButtonType.OK)
  }

  private val result = alert.showAndWait()

  result match {
    case Some(ButtonType.OK) => drawer ! Drawer.Starter(50,50)
    case Some(ButtonType.Cancel) => Platform.exit()
  }

  canvas.setFocusTraversable(true)

  new application.JFXApp.PrimaryStage {
    title = "Tanks"
    scene = new Scene(wiDth, heiDth) {
      content = canvas
    }
  }
}

Drawer actor

import akka.actor.{Actor, ActorLogging, ActorRef}
import javafx.scene.canvas.GraphicsContext

object Drawer{
  case object DrawBullet
  case class Starter(x:Int,y:Int)
  case class Fill( x:Int, y:Int)
}

class Drawer(g:GraphicsContext) extends Actor with ActorLogging {
  import Drawer._

  override def receive: Receive = {

    case Starter(newx,newy)=>
      self ! Fill(newx,newy)

    case Fill(newx,newy) =>
      g.fillRect(newx,newy,50,50)
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem is called "forward reference" - you are using g before you've initialized it.

Below code reproduces this.

import akka.actor.{Actor, ActorLogging, ActorRef}
import akka.actor.{Actor, ActorSystem, Props}
import stackoverflow.Drawer.{Fill, Starter}

object AcalaAkkaNPE_51853920 extends App {
  def aFunctionThatCreatesG() = new G()


  private val system = ActorSystem("MYsystem")
  private val drawer = system.actorOf(Props(new Drawer(g)), "Drawer")

  drawer ! Fill(5,2)

  val g = aFunctionThatCreatesG()

}

object Drawer{
  case object DrawBullet
  case class Starter(x:Int,y:Int)
  case class Fill( x:Int, y:Int)
}

class Drawer(g:G) extends Actor with ActorLogging {
  override def receive: Receive = {

    case Starter(newx,newy)=>
      self ! Fill(newx,newy)

    case Fill(newx,newy) =>
      println(g)
  }
}

class G {
  override def toString: String = "I'm G"
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...