not enough arguments for constructor : ()
Here Im trying to create new Object from case class in scala, but I get this error withing my following code.
Here Im trying to create new Object from case class in scala, but I get this error withing my following code.
def getTax(id: ObjectId): models.Tax = {
models.Tax.findOne(MongoDBObject("_id" -> id))
.getOrElse(new models.Tax) // this line is generating error
}
It says that it cannot build a new Tax object without providing the constructor arguments. That is correct,
If we want to buld an object we should provide constructor arguments.
Just take a look at my Tax class.
case class Tax(
id: ObjectId = new ObjectId,
var name: String,
var description: String,
var defaultFlag: Boolean,
var rate: List[Rate] = List.empty ){
}
I have specified default values for id field and rate field but what about name, description and defaultFlag field.
There are no default values for them.
So if we want to create and object like this 'new models.Tax' (which means without passing arguments to constructor),
you should provide default values to all the fileds. I changed my Tax class like this.
case class Tax(
id: ObjectId = new ObjectId,
var name: String = "",
var description: String = "",
var defaultFlag: Boolean = false,
var rate: List[Rate] = List.empty ){
}
Now everything is ok and object can be built.
Comments