This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Global.Run NullPointerException

1 reply
Guillaume Yziquel 2
Joined: 2011-12-13,
User offline. Last seen 42 years 45 weeks ago.

Hi.

I'm trying to use the scala.tools.nsc.interpreter.IMain class within an
OSGi framework, and am stumbling on the following stack trace (using
scala 2.9.1):

Caused by: java.lang.NullPointerException
at scala.tools.nsc.Global$Run.(Global.scala:663)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(IMain.scala:755)
at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:731)
at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:591)
at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:626)

which is triggered by code like that:

val mapping : Array[String] => (F, L) = {

val code : String = {
val path = "myCode.scala"
var code : String = null
def resource = this.getClass.getClassLoader.getResourceAsStream(_)
val source = scala.io.Source.fromInputStream (resource (path))
source.withClose(() => code = source.mkString); code
}

import scala.tools.nsc._
val settings = new Settings; settings.usejavacp.value = true
val interp = new interpreter.IMain (settings) {
interp =>

def execute (code : String) : Array[String] => (F, L) = {

val result = new Array[Array[String] => (F, L)](1)
/* Blows up on the following line: */
interp.bind(interpreter.NamedParam("res", "Array[Array[String] => (F, L)]", result))

interp.interpret(code)

result(0)
}

}

While this kind of code works almost fine in the REPL, it blows up when
deployed in an OSGi bundle. As it is a constructor of Global.Run that
fails, it looks like I have failed to initialise it properly. Would very
much like to know where the 2.9.1 version of scala.tools.nsc is
documented in detail.

Guillaume Yziquel 2
Joined: 2011-12-13,
User offline. Last seen 42 years 45 weeks ago.
Re: Global.Run NullPointerException

Hi.

After digging a bit into the source code of the IMain class, I noticed
that the NullPointerException is triggered because to access the Run
constructor, you need to already have the englobing Global class
instantiated in the IMain.global lazy val.

In my case, IMain.global is evaluated to null, which means that the
following code fails to initialise properly the IMain.global val.

/** the public, go through the future compiler */
lazy val global: Global = {
if (isInitializeComplete) _compiler
else {
// If init hasn't been called yet you're on your own.
if (_isInitialized == null) {
repldbg("Warning: compiler accessed before init set up. Assuming no postInit code.")
initialize(())
}
// blocks until it is ; false means catastrophic failure
if (_isInitialized()) _compiler
else null /* Execution ends up getting there */
}
}

How do I make sure that the IMain.global value is properly initialised?

Le Thursday 16 Feb 2012 à 09:30:05 (+0100), Guillaume Yziquel a écrit :
> Hi.
>
> I'm trying to use the scala.tools.nsc.interpreter.IMain class within an
> OSGi framework, and am stumbling on the following stack trace (using
> scala 2.9.1):
>
> Caused by: java.lang.NullPointerException
> at scala.tools.nsc.Global$Run.(Global.scala:663)
> at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compileAndSaveRun(IMain.scala:755)
> at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.compile(IMain.scala:731)
> at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:591)
> at scala.tools.nsc.interpreter.IMain.bind(IMain.scala:626)
>
> which is triggered by code like that:
>
> val mapping : Array[String] => (F, L) = {
>
> val code : String = {
> val path = "myCode.scala"
> var code : String = null
> def resource = this.getClass.getClassLoader.getResourceAsStream(_)
> val source = scala.io.Source.fromInputStream (resource (path))
> source.withClose(() => code = source.mkString); code
> }
>
> import scala.tools.nsc._
> val settings = new Settings; settings.usejavacp.value = true
> val interp = new interpreter.IMain (settings) {
> interp =>
>
> def execute (code : String) : Array[String] => (F, L) = {
>
> val result = new Array[Array[String] => (F, L)](1)
> /* Blows up on the following line: */
> interp.bind(interpreter.NamedParam("res", "Array[Array[String] => (F, L)]", result))
>
> interp.interpret(code)
>
> result(0)
> }
>
> }
>
> While this kind of code works almost fine in the REPL, it blows up when
> deployed in an OSGi bundle. As it is a constructor of Global.Run that
> fails, it looks like I have failed to initialise it properly. Would very
> much like to know where the 2.9.1 version of scala.tools.nsc is
> documented in detail.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland