kdogr
kdogr

Reputation: 201

Kotlin: Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

I'm trying to fix an issue in an old kotlin project. But the problem is that I can't compile the code. I tried compile and run in Android Studio and IntelliJ. I got same errors.

Here are the errors:

Error:(174, 25) Expression 'length' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

Error:(176, 60) Unresolved reference: charAt

Error:(148, 67) Expression 'size' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

Error:(107, 76) Expression 'ordinal' of type 'Int' cannot be invoked as a function. The function 'invoke()' is not found

My gradle script:

buildscript {
ext.kotlin_version = '1.0.4'

repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.5.0'
    classpath 'com.google.gms:google-services:1.5.0'
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
} 
.
.
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
.
.
sourceSets {
    main.java.srcDirs += 'src/main/kotlin'
}
}

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

For ordinal error:

//enum class
enum class Category(val n:Int, val color:Int, val id : String){
   HEADLINE(R.string.category_headline, Color.parseColor("#EC4A42"), "101"),
   .
   .
  }
//where call ordinal func
intent.putExtra(MainActivity.EXTRA_CATEGORY, Category.HEADLINE.ordinal())

For charAt error:

companion object{
    fun trim(s : CharSequence) : CharSequence{
        var start = 0
        var end = s.length()

        while (start < end && Character.isWhitespace(s.charAt(start))) {
            start++
        }

        while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
            end--
        }

        return s.subSequence(start, end)
    }
}

For length():

 companion object{
    fun trim(s : CharSequence) : CharSequence{
        var start = 0
        var end = s.length()

        while (start < end && Character.isWhitespace(s.charAt(start))) {
            start++
        }

        while (end > start && Character.isWhitespace(s.charAt(end - 1))) {
            end--
        }

        return s.subSequence(start, end)
    }
}

size() usage:

class PhotoGalleryAdapter(val ac : Activity, val result : ResponseNewsDetail) : PagerAdapter(){
   override fun getCount(): Int = result.gallery!!.size()
   .
   .
 }

Any ideas/suggestions would be appreciated. Cheers!

Upvotes: 19

Views: 35265

Answers (2)

Braian Coronel
Braian Coronel

Reputation: 22867

In Kotlin the expressions getter and setter differ from Java in the absence of parentheses.

getter: #Class.method setter: #Class.method = value

e.g.

From: competitions.value(body?.competitionsList).

To: competitions.value = body?.competitionsList

e.g. 2:

// Gets linearlayout
  val layout: LinearLayout = findViewById(R.id.myLayout)
// Gets the layout params that will allow you to resize the layout
  val params: ViewGroup.LayoutParams = layout.layoutParams
  params.width = 100
  params.height = 100
  layout.layoutParams = params

Source

Upvotes: 15

rafal
rafal

Reputation: 3260

All of those int-returning methods (String#length(),...) have some time ago became properties. Just remove parenthesis () and use it in properties manner.

    var start = 0
    var end = s.length  //without ()

btw. String already has a method trim()

charAt should be replaced with [] operator. So replace s.charAt(end-1) with s[end-1]

Upvotes: 35

Related Questions