Skip to main content

Scala : not enough arguments for constructor : ()

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.
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

Popular posts from this blog

Upload CSV file using opencsv with Play 2.0.2, Scala and MongoDB

Hi Everyone... Today I'm going to explain you that how we could upload a CSV file with list of products to the MongoDB table. The way I'm describing here might me inefficient but still it is useful. We know that Scala is running on JVM and so that we can use any java library with Scala . I will use OpenCSV as a CSV parser for this program. OpenCSV is a CSV parser library for Java. No matter it is for java here we will use it with Scala. First we should have MongoDB up and running. Include following opencsv dependency in your build file. I'm using SBT as building tool. "net.sf.opencsv" % "opencsv" % "2.1" Here next I will create view file. Name of this file will be 'index.scala.html'. @(message: String)(implicit request: RequestHeader, flash:Flash, l: Lang=Lang("en")) @import play.api.i18n._ @import views.html.helper._ @main(message, null) { //print messages @if(flash.data.contains("message")){ ...

Play 2.0.2 and Scala Mapping Form to Case Class with List of Objects

Hi guys.. Here I'm going to explain you how to map play form data to a Case class object. Speciality of this case class is it contains list of object as a field. Here is my class Course (Course.scala). Assume that Course can contain number of Subjects. Course ------------> Subjects case class Course( id: ObjectId = new ObjectId, var name: String = "", var description: String = "", var price: Int = 0, var subjects: List[Subject] = List.empty ){ } case class Subject( var name: String = "", var description: String = "" ){} //This line is for database table creation //I'm Using MongoDB so table will be created at runtime object Course extends ModelCompanion[Course, ObjectId] { val collection = mongoCollection("course") //table name course val dao = new SalatDAO[Course, ObjectId](collection = collection) {} } Now I'm going to map this object to play form (play.api.mvc.Form). First I have Co...