我正在运行sbt应用程序实例化类MyObject
的对象,该对象使用其伴随对象中的val。这是主类(对象):
object MainClass {
def main(args: Array[String]) {
val a = new MyObject()
}
}
以下是MyObject
的定义:
import java.text.SimpleDateFormat
import java.util.Calendar
class MyObject {
val aValue = MyObject.yesterday
}
object MyObject {
val yesterday = getDaysAgo(1)
val dateNumFormat = new SimpleDateFormat("yyyymmdd")
private def getDaysAgo(n: Int) = {
val today = Calendar.getInstance()
today.add(Calendar.DAY_OF_MONTH, -n)
//println(dateNumFormat.format(today.getTime))
today.getTime
}
}
当我取消注释println
语句时,我得到(确切的)跟随错误:
$ sbt run
[info] Loading project definition from C:\Work\metaswitch\RandomProject\project
[info] Set current project to RandomProject (in build file:/C:/Work/metaswitch/RandomProject/)
[info] Compiling 1 Scala source to C:\Work\metaswitch\RandomProject\target\scala-2.11\classes...
[info] Running MainClass
[error] (run-main-0) java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
at MyObject.<init>(MyObject.scala:21)
at MainClass$.main(MainClass.scala:6)
at MainClass.main(MainClass.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
Caused by: java.lang.NullPointerException
at MyObject$.getDaysAgo(MyObject.scala:13)
at MyObject$.<init>(MyObject.scala:6)
at MyObject$.<clinit>(MyObject.scala)
at MyObject.<init>(MyObject.scala:21)
at MainClass$.main(MainClass.scala:6)
at MainClass.main(MainClass.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
[trace] Stack trace suppressed: run last compile:run for the full output.
java.lang.RuntimeException: Nonzero exit code: 1
at scala.sys.package$.error(package.scala:27)
[trace] Stack trace suppressed: run last compile:run for the full output.
[error] (compile:run) Nonzero exit code: 1
[error] Total time: 2 s, completed 06-Jan-2016 11:21:16
答案 0 :(得分:4)
字段的初始化以文本顺序发生(即,按顺序,字段在类/对象中声明)。
鉴于代码中的声明顺序,首先声明yesterday
,它将首先被初始化,从而调用getDaysAgo
。反过来,该方法会尝试读取(截至尚未初始化的)字段dateNumFormat
,这会产生null
,从而导致表达式dateNumFormat.format(today.getTime)
抛出NullPointerException
。< / p>
最简单的解决方法是将dateNumFormat
的声明(和初始化)移到yesterday
之前 - 正如您已经发现的那样。