Unable to assign to a Int? an Int value

Dear Fellows,

i have submitted a Error Report  Submitted as 553225.
which is basically caused while compilation when i uncomment this function.

fun lastWordExtendedUptoCol( wrdLst:ArrayList<StrLocation> , lastCol:Int ):ArrayList<StrLocation> {
           var sz = wrdLst.size() ;
           if ( sz <= 0) return wrdLst;
           //println(“${sz}”);
           sz = sz - 1 ;
           wrdLst.get(sz).loc?.endCol = lastCol ; // <- this line is not even compiling why? endcol is declared Var Int?
           return wrdLst;
  }


Kotlin: [Internal Error] org.jetbrains.jet.codegen.CompilationException: Back-end (JVM) Internal error: cannot store to value org.jetbrains.jet.codegen.StackValue$OnStack@1787952
Cause: cannot store to value org.jetbrains.jet.codegen.StackValue$OnStack@1787952
File being compiled and position: (36,13) in H:/Programming_Projects/IdeaProjects/KotlinProjects/AndThen/src/Utils/strMatches.kt
PsiElement: wrdLst.get(sz).loc?.endCol = lastCol
The root cause was thrown at: StackValue.java:73
many more… and then
Caused by: java.lang.UnsupportedOperationException: cannot store to value org.jetbrains.jet.codegen.StackValue$OnStack@1787952
… 96 more

am i doing something worng

Thanks
Regards

 

            wrdLst.get(sz).loc?.endCol = lastCol ; // <- this line is not even compiling why? endcol is declared Var Int? 
Let me preface by saying I’m that I’m not an expert in Kotlin.  That said, loc?.endCol could be null when when get(sz) return null, and in that case, how would the assignment work.  I think the compiler isn’t giving a very helpful error message, but I’m not sure what semantics this line would have if it did compile.

Thanks Andrew Colombi for the reply

it does not work even when put into a if check upto that point
if ( wrdLst.get(sz).loc != null)
{
  wrdLst.get(sz).loc?.endCol = lastCol ;
}

Thanks
Regards

It shouldn't. "wrdLst.get(sz).loc" involves a function call, and nobody can be sure that the function returns the same thing every time it's called. So, although you checked it once for being not null, it is not guaranteed to be not null next time.

Assign the result to a variable and check its value:

val loc = wrdLst.get(sz).loc if ( loc != null) {   loc.endCol = lastCol ; }

Also note that you can use square brackets instead of .get():

val loc = wrdLst[sz].loc

Thanks Andrey Breslav

your solution is working correctly currently.

val loc = wrdLst.get(sz).loc
if ( loc != null)
{
     loc.endCol = lastCol ;
}

althout i would like to correct the typeo for future users that instead of item it should be loc in previous post

Thanks
Regards

Correction done

Dear Fellows,

i am sorry but i am still stuck at a similar situation.
maybe i dont understand the null semantics well.
Now this wont compile with similar messages.

fun recordCapturedWord( inStrLoc:StrLocation ):ColUpdate{            var ret = ColUpdate.NotUpdated;            if (!capturedWord.equals("")) {            word_lst = updatePreviousWordsLastColValue(word_lst, currColNum);            inStrLoc.str = capturedWord.toString();            var loc = inStrLoc.loc ;            if( loc != null )            {                    loc?.line  = atLine ;                    // loc?.startCol = null;                    loc?.endCol   = currColNum;                    // loc?.fileName = null;            }            word_lst +=   inStrLoc ; // StrLocation(capturedWord.toString(), Location(atLine, currColNum, null, null));            capturedWord = StringBuilder("") ; //.clear()            ret = ColUpdate.Updated ;            }            return ret;   }

i have tried directly

  1. if ( inStrLoc.loc != null )
  2. loc.line
  3. inStrLoc.loc.line
  4. inStrLoc.loc?.line


none works
what obvious am i missing.


Thanks
Regards

Smart casts do not work for var's. And assignments do not work if there's a ?. on the left-hand side. A workaround is always to assign to a val, check it and then use. Another workaround is to use !!:

loc!!.line = atLine

!! throws an exception if the expression to the left is null.

Andrey Breslav,

Thanks for the rescue again.
changing var to val did it. i think i will stick with !! untill i get used to with Kotlin.
they just make things easier.

And assignments do not work if there's a ?. on the left-hand side.

i think that info is a relief for brain cells now.

Thanks
Regards