Runtime error on Android 2.2 in a for iterator loop

 

for(var f : FeedSite? in river.updatedFeeds?.updatedFeed?.iterator()){
           if (f != null){
           f!!.item?.forEach {
                   if (it != null) {
                   newsItems.add(FeedItemMeta(it, f!!.feedTitle, “”))//f?.feedTitle, f?.feedUrl))
                   }
           }
           }
  }

Above code throws at run time the following error:

java.lang.NoSuchFieldError: jet.runtime.SharedVar$Object.ref

This is because I use f!!.feedTitle at the last statement. If I don’t use it, then there’s no problem.

It works when I rewrite above code to both use for loop

``

for(var f : FeedSite? in river.updatedFeeds?.updatedFeed?.iterator()){
           if (f != null){
           for(var fi in f!!.item?.iterator()){
                   if (fi != null) {
                   newsItems.add(FeedItemMeta(fi!!, f!!.feedTitle, “”))//f?.feedTitle, f?.feedUrl))
                   }
           }
           }
  }

Message was edited by: Dody Gunawinata

This looks very strange. Are you sure you have the up-to-date kotlin-runtime.jar in you classpath?

Have you tried using val f instead of var f ?

Andrey this simple example is also throwing an exception @runtime : #IC - 123.72 , #KT - 0.4.280

  val l = arrayList(arrayList(1, 2, 3), arrayList(1, 2, 3), arrayList(1, 2, 3))   for(var list in l){   list.forEach {            println("${list.size}")   }   }

Exception in thread "main" java.lang.VerifyError: (class: kotlin/bugs/namespace, method: main signature: ([Ljava/lang/String;)V) Incompatible type for getting or setting field   at java.lang.Class.forName0(Native Method)   at java.lang.Class.forName(Class.java:169)   at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)

next loops are working fine

  for(val list in l){   list.forEach {            println("${list.size}")   }   }

  for(var list in l){
  for(el in list){
           println(“${list.size}”)
  }
  }

for(val f : FeedSite? in river.updatedFeeds?.updatedFeed?.iterator()){                    if (f != null){                    f.item?.forEach{                            if (it != null) {                            newsItems.add(FeedItemMeta(it, f.feedTitle, f.feedUrl))                            }                    }                    }            }

Yup, changing var to val works

Here's another funny behaviour with var

           for(var f : FeedSite? in river.updatedFeeds?.updatedFeed?.iterator()){                    if (f != null){                    f.item?.forEach{                            if (it != null) {                            newsItems.add(FeedItemMeta(it, f.feedTitle, f.feedUrl))                            }                    }                    }            }

This will not compile because it’s missing safe ? or non-null assertion for the f even though there’s a  null guard check.

Thank you for short example. I've created an issue for it (http://youtrack.jetbrains.com/issue/KT-3134).

Check for null only works with immutable variables, because otherwise it might happen that it changes to null after the check.

ah thanks. I missed the val requirement.