question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

See original GitHub issue

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 3.5.1
  @capacitor/core: 3.5.1
  @capacitor/android: 3.5.1
  @capacitor/ios: 3.5.1

Installed Dependencies:

  @capacitor/cli: 3.3.4
  @capacitor/core: 3.3.4
  @capacitor/android: 3.3.4
  @capacitor/ios: 3.3.4

Platform(s)

Android

Current Behavior

When trying to run getUserMedia and show camera preview the app crashes and the only log we get in Android Studio is bug: java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.graphics.Bitmap.getWidth()’

Expected Behavior

App not crashing but working properly

Code Reproduction

I couldn’t reproduce the issue on a mini app with the same code

Other Technical Details

I found the issue and a fix for it, so just writing it down for the capacitor team or anyone else experiencing this problem. The fix I did was add

@Override
public Bitmap getDefaultVideoPoster() {
    if (super.getDefaultVideoPoster() == null) {
        return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    } else {
        return super.getDefaultVideoPoster();
    }
}

in BridgeWebCustomClient.java Line 84 (capacitor/src/main/java/com/getcapacitor/BridgeWebCustomClient.java)

This adds a black bitmap if there is no default video poster. I also tried this return BitmapFactory.decodeResource(bridge.getContext().getResources(), R.drawable.ic_transparent); while importing R as import com.getcapacitor.android.R; and this resulted in the same error which showed me that your ic_transparent (that you probably use if there is no default video poster) is crashing the app with the error.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
DanaStefanoskacommented, Aug 3, 2022

Thank you @PrabhatSuprdaily, we are indeed using the plugin diagonstic with version ^6.0.2 (which probably installed a newer version and we started to have problems). I’ll implement the version changes now.

1reaction
PrabhatSuprdailycommented, Jul 29, 2022

@DanaStefanoska We also faced this issue in our project. We are having a dependency of ‘cordova.plugins.diagnostic’. With this library if we use version 6.0.0 - 6.0.3, it uses internally androidx.appcompat:appcompat with auto upgrade. And androidx.appcompat:appcompat has some compatibility issue with cordova sharing resources like camera in the later versions. In the earlier version or later ones ‘cordova.plugins.diagnostic’ have specified the exact version of androidx.appcompat:appcompat. PS: Please check your package-lock.json as well that the ‘cordova.plugins.diagnostic’ version is not between 6.0.0 - 6.0.3. @DanaStefanoska thanks for the temporary fix earlier.

Bug Report

Capacitor Version

Latest Dependencies:

  @capacitor/cli: 3.5.1
  @capacitor/core: 3.5.1
  @capacitor/android: 3.5.1
  @capacitor/ios: 3.5.1

Installed Dependencies:

  @capacitor/cli: 3.3.4
  @capacitor/core: 3.3.4
  @capacitor/android: 3.3.4
  @capacitor/ios: 3.3.4

Platform(s)

Android

Current Behavior

When trying to run getUserMedia and show camera preview the app crashes and the only log we get in Android Studio is bug: java.lang.NullPointerException: Attempt to invoke virtual method ‘int android.graphics.Bitmap.getWidth()’

Expected Behavior

App not crashing but working properly

Code Reproduction

I couldn’t reproduce the issue on a mini app with the same code

Other Technical Details

I found the issue and a fix for it, so just writing it down for the capacitor team or anyone else experiencing this problem. The fix I did was add

@Override
public Bitmap getDefaultVideoPoster() {
    if (super.getDefaultVideoPoster() == null) {
        return Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
    } else {
        return super.getDefaultVideoPoster();
    }
}

in BridgeWebCustomClient.java Line 84 (capacitor/src/main/java/com/getcapacitor/BridgeWebCustomClient.java)

This adds a black bitmap if there is no default video poster. I also tried this return BitmapFactory.decodeResource(bridge.getContext().getResources(), R.drawable.ic_transparent); while importing R as import com.getcapacitor.android.R; and this resulted in the same error which showed me that your ic_transparent (that you probably use if there is no default video poster) is crashing the app with the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attempt to invoke virtual method 'int android.graphics.Bitmap ...
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference.
Read more >
int android.graphics.Bitmap.getWidth() - Issue Tracker
Error : java.lang.NullPointerException:Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
Read more >
Attempt to invoke virtual method ''int android.graphics.Bitmap ...
NullPointerException : Attempt to invoke virtual method ''int android.graphics.Bitmap.getWidth()'' on a null object reference at android.graphics.Bitmap.
Read more >
Attempt to invoke virtual method 'int android.graphics.Bitmap ...
Android – Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference.
Read more >
Runtime Error "android.graphics.Bitmap.getWidth()" - Discuss
Source: Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found