mham
mham

Reputation: 145

Convert array to WrappedArray

I have tried to use WrappedArray and initialize it by null

var classes:WrappedArray [String]= null
 var classe = ENTEleveClasses.apply(0)
         if(!classe.isEmpty()) {
           var cls = classe.split(",")
           for(c <- cls){
             classes =   classes :+ c  //  the error 
           }
           classes
         }else ENTEleveClasses

Error :

Caused by: java.lang.NullPointerException
    at comptesInvites.EleveENT$$anonfun$1.apply(EleveENT.scala:19)
    at comptesInvites.EleveENT$$anonfun$1.apply(EleveENT.scala:16)
    ... 16 more

when I add an element a WrappedArray an Error occurred

Thanks

Upvotes: 1

Views: 2055

Answers (2)

Ramesh Maharjan
Ramesh Maharjan

Reputation: 41957

You should always try to avoid initializing variables with null as it leads to numerous nullpointerExceptions and can even damage a system as a whole.

You should initialize the WrappedArray classes as

var classes:mutable.WrappedArray [String]= mutable.WrappedArray.empty[String]

Upvotes: 1

C4stor
C4stor

Reputation: 8026

when I add an element a WrappedArray an Error occurred

You're not adding an element to a WrappedArray, you're adding an element to the null value, which is indeed impossible, and generates a NPE.

To add an element to a WrappedArray, you have to actually provide one, so instead of var classes:WrappedArray [String]= null, use var classes:WrappedArray [String]= new WrappedArray() (I'm not actually sure what are the constructors for WrappedArray, you may have to change this !)

Upvotes: 0

Related Questions