I’m having an issue with Compose `@Preview` render...
# compose
c
I’m having an issue with Compose
@Preview
rendering. This shows up as “Render problem” in the preview pane:
Copy code
java.lang.NullPointerException: Parameter specified as non-null is null
The parameter referenced in the message is just a
text: String
which gets passed to a text composable
Text(text = text)
This is the exact code
Copy code
@Composable
fun ShoutoutCard(text: String) {
  Card(
    modifier = Modifier
      .padding(16.dp),
    elevation = 2.dp,
  ) {
    Row(
      modifier = Modifier.padding(8.dp),
      verticalAlignment = Alignment.CenterVertically,
    ) {
      HeartFilledIcon(tint = Color.Red)
      Spacer(Modifier.padding(16.dp))
      Text(text = text)
    }
  }
}

@Preview
@Composable
fun ShoutoutCard() {
  ShoutoutCard(text = "Chris7!")
}
Sometimes the preview works, and renders like so:
This has been my troubleshooting method which has been successful: When I hit this exception, I do the following: • Create a test composable like this one:
Copy code
@Preview
@Composable
fun TestComposable() {
  Text(text = "Hello2345 ")
}
• Refresh the preview. Sometimes I have to refresh twice
Android Studio
Compose version is
1.1.0-rc01
I don’t know why adding a dummy composable preview and refreshing makes it work. Note that simply repeatedly refreshing doesn’t apparently fix the problem, I have to add some new dummy code as above.
I am using Kotlin v
1.6.10
for my project, but I rolled back to
1.6.0
to see if it made a difference (as 1.6.10 is not known to be compatible with this version of compose). There was no difference in behaviour. I am more or less able to trivially reproduce this problem. Starting from a “working” state (that is, with my
ShoutoutCard
composable and
TestComposable
both being previewed, I do the following: • Comment out
TestComposable
• Refresh preview • Observe NullPointerException • Uncomment
TestComposable
• Refresh preview • Observe preview works again
m
Why don’t you just update to 1.1.0-rc02 which is compatible with Kotlin 1.6.10? See: https://developer.android.com/jetpack/androidx/releases/compose-compiler#1.1.0-rc02
☝️ 1
c
Hmm does this continue to happen if you rename your Preview composable function to "ShoutoutCardPreview"? I wonder if there's issues with the fact that both your functions are the same name and the overload + Preview is causing unexpected results.
c
@Chris Sinco [G] thanks for responding and sorry for the delay getting back to you. I took another look this morning and I don’t seem to be able to reproduce the issue now. Perhaps it was a transient problem where Android Studio had gotten into a bad state (though I did attempt a few restarts before, to no effect). If I see it again I’ll try your suggestion to see if it resolves the issue.
👍 1
p
@Chris Sinco [G] Thanks. That was the resolution in my cases. However I wasn't having any overloaded compose functions whatsoever. Preview was even working just fine. The exception arose only when running the preview on the emulator/physical device. At a different occurrence, I had nearly the exact some exception thrown, but was due to misleading package name at the file header.
👍 1
531 Views