0

I am calling a function inside of class constructor but while compiling the code I keep getting an error : not found value : churnPeriodfnc here is the code that I am running

 class CustStoryN (var custId:String,
             var rootEventType:String,
             var rootEventTime:Long,
             var eventStory:mutable.MutableList[StoryEventN]) extends Serializable {
 def this(custID: String,rootEventType: String, rootEventTim: Long, eventStory: mutable.MutableList[StoryEventN], churnPeriod: Boolean, churnMode: Boolean) 
 {
    this(custID,rootEventType,rootEventTim,
      churnPeriodfnc(churnPeriod, churnMode,eventStory))
 }

and here is ChurnPeriodFnc function that the compiler can not recognize, I didnt copy the churn periodfunc , for now just assume that I make some changes to eventstory and out put a new eventstory:

    def churnPeriodfnc(churnPeriod: Boolean, churnMode: Boolean, eventStory: mutable.MutableList[StoryEventN]): mutable.MutableList[StoryEventN] = {
  eventStory  }
2
  • 1
    Where is churnPeriodFnc defined? Commented May 15, 2015 at 20:05
  • Is churnPeriodFnc in an object? You might have not imported its object. Otherwise you need an instance of the declaring class/trait. Commented May 15, 2015 at 20:14

2 Answers 2

2

If churnPeriodfnc is defined within class body (instance method) or it is inherited; you can't call it inside a constructor.

If churnPeriodfnc is defined inside CustStoryN's companion object (like a static method); you must either import it or refer to it as CustStoryN.churnPeriodfnc()

If it's defined in another object, above rule still applies.

Sign up to request clarification or add additional context in comments.

2 Comments

I made some changes : now I have define Buildchurn object inside custstoryN class and have a churnPeriodfncNew function that get called in custStroryN constructor. However I think the code is not working, like the function is not getting called/ or in order to implement the necessary functionality I need to change the code order
the idea is: currentCustStory has an eventtype,eventtime,custid and evetStory which is a mutable.MutableList[StoryEvent]. based on input parameter I need to change the evetStory and construct a new one. I have tried: 1)changing "eventstory" in custStroryN constructor but the end result is unaffected,2)changing "eventstory" somewhere in the code but It throws null pointer (currentCustStory2=buildChurn.churnPeriodfncNew2(isInChurnPeriod,isInChurnMode,currentCustStory)). any idea on how to resolve this issue?
0

I have encountered a similar problem and I don't find this behavior logic (I understand that there is no instance of the class and the function does not exist yet but hey, the function is there inside the class I'm trying to instantiate.)

To fix the problem I suggest you using an apply function in the companion object like this:

case class Human(id: Int, name: String)

object Human {

  def apply(id: Int): Human = new Human(id, withName(id))

  def withName(id: Int): String = "Goku" /* Some behavior to get the name */

}

If you try this in your REPL you should have a behavior like this:

scala> Human(3)
res0: Human = Human(3,Goku)

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.