Hello, What is the correct way of creating a text...
# compose-desktop
j
Hello, What is the correct way of creating a textfield in Compose Desktop? This work fine in Android but crashes on Desktop:
Copy code
val textState = remember { mutableStateOf(TextFieldValue()) }
        TextField(
            value = textState.value,
            onValueChange = {
                textState.value = it
            }
        )
With the following error:
Copy code
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Parameter specified as non-null is null: method androidx.compose.ui.platform.PlatformInput$methodRequestsForInput$1.getTextLocation, parameter offset
a
What version of Compose are you using? The code looks fine, so I’m guessing it has something to do with missing or wrong dependencies.
j
@Alexander Maryanovsky I'm using the default dependencies of the IntelliJ IDEA for a Desktop Compose project. The Compose version is 1.5.10:
Copy code
IntelliJ IDEA 2023.3.2 (Community Edition)
Build #IC-233.13135.103, built on December 20, 2023
Runtime version: 17.0.9+7-b1087.9 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Linux 5.15.0-91-generic
GC: G1 Young Generation, G1 Old Generation
Memory: 3994M
Cores: 4
Registry:
  ide.experimental.ui=true
Kotlin: 233.13135.103-IJ
Current Desktop: ubuntu:GNOME
Copy code
import org.jetbrains.compose.desktop.application.dsl.TargetFormat

plugins {
    kotlin("jvm")
    id("org.jetbrains.compose")
}

group = "com.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
    maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
    google()
}

dependencies {
    // Note, if you develop a library, you should use compose.desktop.common.
    // compose.desktop.currentOs should be used in launcher-sourceSet
    // (in a separate module for demo project and in testMain).
    // With compose.desktop.common you will also lose @Preview functionality
    implementation(compose.desktop.currentOs)
}

compose.desktop {
    application {
        mainClass = "MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "debugTextField"
            packageVersion = "1.0.0"
        }
    }
}
Copy code
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
kotlin.code.style=official
kotlin.version=1.9.20
compose.version=1.5.10
Copy code
pluginManagement {
    repositories {
        gradlePluginPortal()
        maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
    }

    plugins {
        kotlin("jvm").version(extra["kotlin.version"] as String)
        id("org.jetbrains.compose").version(extra["compose.version"] as String)
    }
}

rootProject.name = "debugTextField"
a
Latest version is 1.5.11 and kotlin 1.9.21, but that shouldn’t really be the issue.
j
Well, I literally created a project with the default settings and added the code in the Main App function.
a
Weird, there does seem to be a problem there. A kotlin function implementing a Java function that takes a nullable argument and doesn’t mark the type as nullable.
But this code has been there since 2020
j
Let me try on different computer then.
a
Are you running it and it crashes immediately?
j
It crashes when I click the textfield.
a
What version of Java are you running your app in?
j
JDK 17.0.9
a
Are you sure? Can you put this in your main?
Copy code
println("${System.getProperty("java.version")}")
j
Copy code
> Task :compileJava NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar

> Task :run
17.0.9
a
Ok, what OS? Any unusual input settings?
Anyway, I’ll fix it…
j
Ubuntu 20.04.6 LTS
Any unusual input settings?
For example? Like accents/non latin letters? (no)
Anyway, I’ll fix it…
Please do let me know if you need any additional information.
a
I don’t really know what would cause it, so hard to say.
Somehow the OS/JVM is interacting with the app in an unusual way. The bug is definitely in the app, but it’s surprising that we haven’t seen it for 4 years.
You can follow the issue if you’d like.
👍 1
j
I can confirm there is no bug on MacOS.
👍 1
I guess it's going to be some time before the fix is actually implemented,would there be any workaround for the time being?
a
Not that I can think of, sorry. There’s a
-Xno-param-assertions
compiler flag, but you would need to recompile compose itself, which isn’t trivial.
Can you use our dev builds? If so then you should see the fix in a few days at most.
Maybe a different version of Java won’t trigger it.
j
Seems like Coretto 21 is working fine.
a
@John Aoussou Hi. Do you recall when exactly the crash happens? Do you need to type anything into the textfield first?
j
@Alexander Maryanovsky If I remember correctly, it would crash as soon I clicked the text field (I wasn't able to input anything).
a
Got it, thanks