KafkaProducer [String,String]类型的表达式不符合预期类型Null

时间:2016-11-07 16:19:39

标签: scala apache-kafka

我希望能够从函数producer初始化变量setProducer

object MyObj { 
  var producer = null                                                                                                                  

  def setProducer(p: KafkaProducer[String,String]): Unit = {           
    producer = p                                                       
  }   

  //...
}

然而它说Expression of type KafkaProducer[String,String] doesn't conform to expected type Null

那么,我该如何初始化producer

1 个答案:

答案 0 :(得分:2)

您需要为变量提供类型,例如:

object MyObj { 
  var producer: KafkaProducer[String,String] = null                                                                                                                  

  def setProducer(p: KafkaProducer[String,String]): Unit = {           
    producer = p                                                       
  }   

  //...
}