如何修复NullpointerException错误Scala

时间:2018-08-16 03:55:10

标签: scala rabbitmq akka

因此,当我收到来自Rabbitmq的消息时,我想将其发送给actor,但是当我尝试匹配消息时,我得到了nullpointerexception。当我匹配“ msg”并尝试将其发送给actor时,出现麻烦。如果没有这部分,则“味精匹配{             情况_ =>抽屉! Drawer.Data(msg)           }“一切正常。如何解决?

import java.util.UUID

import Client.{GameBullet, GameTank}
import akka.actor.{Actor, ActorLogging, ActorRef}
import com.rabbitmq.client.AMQP.BasicProperties
import com.rabbitmq.client.{AMQP, ConnectionFactory, DefaultConsumer, Envelope}
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
import org.json4s.native.Serialization.{read, write}



object MessageSender {

  case object Left

  case object Right

  case object Up

  case object Down

  case object StartGame

  case object MakeShot

  case object Start


}

case class Message(id:String,content:String)

class MessageSender(drawer:ActorRef) extends Actor with ActorLogging{
 import  MessageSender._
  val factory = new ConnectionFactory()
  factory.setHost("localhost")
  val connection = factory.newConnection()
  val channel = connection.createChannel()
  val replyQueueName: String = channel.queueDeclare().getQueue
  val corrId = UUID.randomUUID().toString
  channel.queueBind(replyQueueName, "myDirect", replyQueueName)
  val props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build()

  var currentId = 0
  var message = ""

  implicit val formats = Serialization.formats(ShortTypeHints(List(classOf[Message])))

  override def receive: Receive = {

    case StartGame =>
      message="startGame"
      var response: String = null
      var msg:String =""
      val code = pretty(render(Extraction.decompose(Message(currentId.toString,message))))
      println(code)
      channel.basicPublish("myDirect", "service", props, code.getBytes("UTF-8"))
      println(replyQueueName)
      println(corrId)
      while(response == null){
        val consumer = new DefaultConsumer(channel) {
          override def handleDelivery(consumerTag: String,
                                      envelope: Envelope,
                                      properties: AMQP.BasicProperties,
                                      body: Array[Byte]) {
            msg = new String(body, "UTF-8")
            println(properties.getCorrelationId)

           println(s"message is $msg")
            if(properties.getCorrelationId == corrId){
              response = new String(body, "UTF-8")
            }
          }
        }
        channel.basicConsume(replyQueueName, true, consumer)
      }

      currentId=response.toInt
      log.info(s"Session started [$currentId]")
     msg match {
        case _ => drawer ! Drawer.Data(msg)
      }
      self ! Start    
  }
}

错误: This i get when i try match msg

1 个答案:

答案 0 :(得分:0)

根据所附的屏幕截图,该类的第83行出现异常。 因此,问题在于drawer成员为null,因此drawer ! Drawer.Data(msg)抛出NPE。 我猜想actor的初始化(使用props)是通过一个变量进行的,该变量稍后在相同的作用域中定义,并产生“前向引用”。

在其他相关问题(Scala Akka NullPointerException error)中查看我的示例