19

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!

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62
kdogr
  • 201
  • 1
  • 2
  • 8

2 Answers2

35

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]

rafal
  • 3,120
  • 1
  • 17
  • 12
  • Thanks man.. It is small but very important thing. '()' became headache for me. Just removed '()' – Yog Guru Apr 11 '18 at 12:27
  • So in Kotlin, if you need to access a member variable with type `Int`, You don't use `getter`. – Alston Nov 03 '19 at 04:22
14

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

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62