0
votes
     bitmap1 = Bitmap.createScaledBitmap(

        bitmap1, // <---- error is  here 

        (width.toInt()),

        (height.toInt()),

        false)

    numberOfInvaders ++

I also used bitmap2 and bitmap 1 in another class :

                 if (uhOrOh) {
                    canvas.drawBitmap(Invader.bitmap1, // <--- error is here 
                        invader.position.left,
                        invader.position.top,
                        paint)
                } else {
                    canvas.drawBitmap(Invader.bitmap2, // <---- and here
                        invader.position.left,
                        invader.position.top,
                        paint)
                }

here its says : Type mismatch, Required:Bitmap Found: Bitmap?

2
It expects a non-null bitmap (so a Bitmap), but bitmap1 is nullable (so a Bitmap?)gpunto
so what i have to do to fix it ? im kinda newShadowBroker 2077
Figured it out just needed to add !! next to itShadowBroker 2077

2 Answers

1
votes

Yup, that's true :) You cannot use value like this, because it can be null at some point.

createScaledBitmap requires nonnullable Bitmap, but there is no guarantee that bitmap you use won't be null at the moment of calling given function.

So, what you can do? Before the call check if bitmap is not null:

if (bitmap != null) { /* code here, still requires !! operator */ }

In multithreaded environment there is a risk that during execution of code block a value will change anyway, so you can use let function with ?. operator (basically the same operator like ., but executes only if value is not null). The block code will be invoked with an effectively final argument which is an instance you use to call this method, in this case "bitmap", called "context object", accessible via it keyword:

bitmap?.let { /* code here, bitmap is passed as effectively final, so for sure it's not null  */ }

There other way would be !! operator (but it can finish with NPE exception, if value is null). Use only if you are sure that this value at that moment won't be null, otherwise you can crash your application.

Also, you can use ?: operator - this will take first value if not null, otherwise the second. It's quite nice, because you can use for example default value. Also, you can throw exception there ;)

bitmap ?: throw IllegalStateException("bitmap is null") // exception
bitmap ?: DEFAULT_BITMAP // default bitmap, if any

In this case you will get exception but with very communicative message (instead of just NPE).

0
votes
    bitmap1 = Bitmap.createScaledBitmap(

    bitmap1!!, // !! <--- helps

    (width.toInt()),

    (height.toInt()),

    false)

numberOfInvaders ++



            if (uhOrOh) {
                canvas.drawBitmap(Invader.bitmap1!!, //  here 
                    invader.position.left,
                    invader.position.top,
                    paint)
            } else {
                canvas.drawBitmap(Invader.bitmap2!!, //  and here too
                    invader.position.left,
                    invader.position.top,
                    paint)
            }