3

Say I have a model class as shown below:

class User () {
  var id: Option[java.lang.Long] = _
  var name: Option[String] = _
  var date: Option[java.util.Date] = _

  def this(id: java.lang.Long,name: String, date: java.util.Date){
    this()
    this.id=Option(id)
    this.name=Option(name)
    this.date=Option(date)
  }
}

I tried to create an object by doing

var obj = new User
obj.id=12345
obj.name=user

Obviously obj.date is null, and I kept getting a java.lang.NullPointerException at this point. How should I handle this exception?

4
  • Why do you need a zero-arg constructor? If you're going to initialise the fielsds immediately, have a constructor do that (then that can initialise the date field appropriately -i.e. None) Commented May 26, 2016 at 8:17
  • @JonnyHenly, it's not clear to me that lazy vals are needed or desirable here. Commented May 26, 2016 at 8:18
  • @TheArchetypalPaul I need to initialize because it just wont write to my neo4j if i dont. I am still grasping the enviroment. Commented May 26, 2016 at 8:19
  • So def this(id: java.lang.Long,name: String){this() this.id=Option(id) this.name=Option(name) this.date=None} Commented May 26, 2016 at 8:21

3 Answers 3

3

I would advise you to initialize the values directly through the constructor. This looks like a POSO (Plain Old Scala Object), so maybe you would be better off with an immutable case class:

case class User(id: Option[Long], name: Option[String], date: Option[Date])

Now if you want to initialize only some of the properties, you can either pass None or create an auxiliary constructor via the companion object:

case class User(id: Option[Long], name: Option[String], date: Option[Date])
object User {
  def apply(id: Option[Long], name: Option[String]) = {
    User(id, name, None)
  }
}

Edit:

If Java interop is important, you can define multiple constructors inside the case class which will be visible in Java:

case class User(id: Option[Long], name: Option[String], date: Option[Date]) {
  def this(id: Long, name: String) = {
    this(Some(id), Some(name), None)
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried that in the first attempt, but then it wont let me write to neo4j using spring-data-neo4j driver, but this way seems to do it.
but then it wont let me write to neo4j using spring-data-neo4j driver What does that mean? What happens when you attempt to write something?
successful with no error but the model wasnt inserted
@kenlz Perhaps spring-data-neo4j requires your classes to be complying JavaBeans. To achieve that with scala case classes, either annotate every field with a @BeanProperty, or define your own getters in vanilla java format.
1

One nice thing about Option[T] is that it has a safe default value None. So initialise your vars to None instead of null.

You should also think about making your model class immutable.

2 Comments

I tried that, and I get a java.lang.ClassCastException: scala.None$ cannot be cast to java.util.Date, I think i need to manually initialize a default Date value if null were found
@kenlz, please update the question with your code because that doesn't sound correct. You assign None to an Option[Date], not a Date
0

More easy way to do this is

case class User(id: Option[Long]=None, name: Option[String]=None, date: Option[Date]=None)

In this case if you give a value it takes the value or else it would take None as a default value.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.