Thread
#compose
    Elnur Jeksenov

    Elnur Jeksenov

    1 year ago
    I want to make dashed border like this to my button, for this I am drawing rectangle with dashed stroke, but there is obscure exception: “java.lang.NullPointerException: null cannot be cast to non-null type androidx.compose.ui.graphics.AndroidPathEffect”
    Canvas(modifier = Modifier.fillMaxSize(), 
    onDraw = {
                                drawRect(
                                    size = Size(packFloats(200f, 50f)),
                                    color = Color.Blue,
                                    style = Stroke(width = 1f, pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f))
                                )
    }
    )
    Have you got any idea, why this exception is appearing?
    k

    Kirill Grouchnikov

    1 year ago
    @Nader Jawad
    jim

    jim

    1 year ago
    Probably Nader is going to need more than just the message. Can you post the whole trace in this thread or in a gist?
    BenjO

    BenjO

    1 year ago
    Jeisson Sáchica

    Jeisson Sáchica

    1 year ago
    I had a similar issue last week https://issuetracker.google.com/issues/179062252 which has already been resolved (maybe for alpha12 it will be released). The problem there though was that when making a draw call with no pathEffect after one which did have one the cast failed
    Nader Jawad

    Nader Jawad

    1 year ago
    Yes this looks similar to the issue that @Jeisson Sáchica has linked to. If you need a workaround @Elnur Jeksenov you can try the following code that allows you to draw with the canvas directly instead of leveraging the
    DrawScope
    API.
    val paint = remember {
            Paint().apply {
                color = Color.Blue
                style = PaintingStyle.Stroke
                strokeWidth = 1f
                pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
            }
        }
        Canvas(modifier = Modifier.fillMaxSize()) {
            drawIntoCanvas { canvas ->
                canvas.drawRect(0f, 0f, 200f, 50f, paint)
            }
        }
    Also FWIW in the same code provided you wouldn't need to call
    Size(packFloats(200f, 50f)
    but rather just call
    Size(200f, 50f)
    directly. This is a side effect of how inline classes in Kotlin work. We need to expose the packed long value public constructor however, we expose similarly named function constructors to do this work on your behalf