1

I want to do some parallel computation and I'm getting a really strange java.lang.NullPointerException on calling ANY functions outside the object I have.

Take a look:

case class Return(session: String, job: Int)
case class Ready(n: Int)
case class DoJob(session: String, job: Int)
case class NotReady


object Notifications extends Controller with Secure {

  class AtorMeio extends Actor {
    import scala.collection.mutable.{Map => MMap}
    val job: MMap[(String, Int), Option[Int]] = MMap()

    def act {
      loop {
        react {
          case DoJob(session, jobn) =>
            if (job.get((session, jobn)).isEmpty) {
              jobn match {
                case 1 =>
                  job.put((session, jobn), None)

                    val n = Messaging.oi //Messaging.retrieveNumberOfMessages(new FlagTerm(new Flags(Flags.Flag.SEEN), false))
                    job.put((session, jobn), Some(n))

                case 2 =>
                  // do!
              }
            }
          case Return(session, jobn) =>
            if (job.get((session, jobn)).isDefined && job.get((session, jobn)).get.isDefined) {
              val ret = job.get((session, jobn)).get.get
              job.remove((session, jobn))

              reply(Ready(ret))
            }
            else
              reply(NotReady)
        }
      }
    }
  }

  private var meuator: AtorMeio = null
  lazy val ator = {
    if (Option(meuator).isEmpty) {
      meuator = new AtorMeio
      meuator.start
    }

    meuator
  }

  def pendingNotifications = {    
    ator ! DoJob(session.getId, 1)

    ator !? Return(session.getId, 1) match {
      case Ready(ret) =>
        if (ret.toString != Option[String](params.get("current")).getOrElse("-1")) "true" else Suspend("80s")
      case _ =>
    }
  }
}

I'm getting an error in executing Messaging.oi which is basically an object with:

def oi = 4

Here is the stacktrace:

controllers.Notifications$AtorMeio@1889d53: caught java.lang.NullPointerException
java.lang.NullPointerException
    at controllers.Messaging$.oi(Messaging.scala:108)
    at     controllers.Notifications$AtorMeio$$anonfun$act$1$$anonfun$apply$1.apply(Notifications.scala:38)
at controllers.Notifications$AtorMeio$$anonfun$act$1$$anonfun$apply$1.apply(Notifications.scala:31) at scala.actors.ReactorTask.run(ReactorTask.scala:34)
at scala.actors.ReactorTask.compute(ReactorTask.scala:66)
at scala.concurrent.forkjoin.RecursiveAction.exec(RecursiveAction.java:147)
at scala.concurrent.forkjoin.ForkJoinTask.quietlyExec(ForkJoinTask.java:422)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.mainLoop(ForkJoinWorkerThread.java:340)
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:325)

Line 108 is exactly this oneliner def. Ahh entrance point is def pendingNotifications.

Anyone can help? Thanks a lot!

3 Answers 3

1

Have you tried replacing

 private var meuator: AtorMeio = null 

by either:

private var meuator: AtorMeio = None 
3
  • thx for the answer.. but why would that compile? and I don't access it directly.. I use a lazy val called ator for that.
    – José Leal
    Jul 26, 2011 at 13:27
  • What I wanted to say was to replace the null by a value of Option. It seems your error is related to that null in there. I know it's using a lazy val, but having a starting value of Null may not be the best idea . Jul 26, 2011 at 13:30
  • changed it to private var meuator: Option[Actor] = None .. still not working =/ Any more ideas?
    – José Leal
    Jul 26, 2011 at 15:11
0

Configure your breakpoints view in your debugger to halt/break on NullPointerExceptions ...

And: you did see you set this to null here:

  private var meuator: AtorMeio = null 

?? Or?

1
  • please check: "lazy val ator" and I start from "def pendingNotifications"
    – José Leal
    Jul 26, 2011 at 13:26
0

Ok people, after digging a lot I discovered the problem: Somehow, somewhere if you have the "Controller" class from play framework mixed in, it crashes mercifully. So I just wrapped this thing into a 'clean' class and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.