mercredi 12 février 2020

NullPointerException on Scala reflection with typeOf[T]

I am trying to process something from given type in Scala generics like this:

import scala.reflect.runtime.universe._

implicit private val tag: TypeTag[T] = typeTag[T]
private def getTypeString(implicit tag: TypeTag[T]): String = typeOf[T] match {
  case t if t =:= typeOf[OvalTest] => "tests"
  case t if t =:= typeOf[OvalObject]=> "objects"
  case t if t =:= typeOf[OvalState]=> "states"
  case t if t =:= typeOf[OvalVariable] => "variables"
}

I am getting exception:

Exception in thread "main" java.lang.NullPointerException
    at scala.reflect.api.TypeTags.typeOf(TypeTags.scala:357)
    at scala.reflect.api.TypeTags.typeOf$(TypeTags.scala:357)
    at scala.reflect.api.Universe.typeOf(Universe.scala:73)
    at composer.OvalIndexer$ElementsIndexer.getTypeString(OvalIndexer.scala:142)
    at composer.OvalIndexer$ElementsIndexer.<init>(OvalIndexer.scala:148)
    at composer.OvalIndexer$.createIndex(OvalIndexer.scala:48)
    at Main$.main(Main.scala:11)
    at Main.main(Main.scala)

Null Pointer happens on line private val ovalType = getTypeString.





Aucun commentaire:

Enregistrer un commentaire