0

Getting exception "java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference"

when i execute below code. Goal: change image dynamically based on some status for one flavor in android studio.

AndroidManifest.xml

 <application
        tools:replace="android:icon,android:roundIcon"
        android:allowBackup="true"
        android:icon="${appIcon}"
        android:label="@string/app_name"
        android:roundIcon="${appIconRound}"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name="devicelist.AddDeviceActivity"/>
        <activity android:name="resourcestatesimulator.NativeActivity" />
        <activity android:name="devicelist.DeviceListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name="CarplayMain" />

        <activity-alias
            android:name=".DeviceListActivityAlias"
            android:enabled="false"
            android:icon="@mipmap/ic_launcher_default"
            android:roundIcon="@mipmap/ic_launcher_default_round"
            android:label="@string/app_name"
            android:targetActivity="devicelist.DeviceListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity-alias>
    </application>

DeviceListActivity.java

public void defaultIcon()
    {
        Log.i(TAG, "Before default Icon" );
        packageManager = getPackageManager();
        packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);

        packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivityAlias"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
        Log.i(TAG, "After default Icon" );
    }
    public void activeIcon()
    {
        Log.i(TAG, "Before active Icon" );
        packageManager = getPackageManager();
        packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivity"),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        packageManager.setComponentEnabledSetting(new ComponentName(DeviceListActivity.this,"com.bosch.spi.carplay.devicelist.DeviceListActivityAlias"),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
        Log.i(TAG, "After Active Icon" );
    }

Calling methods in DeviceListAdapter.java

DeviceListActivity deviceListActivity= new DeviceListActivity();
deviceListActivity.activeIcon();
deviceListActivity.defaultIcon();
Ryan M
  • 18,333
  • 31
  • 67
  • 74
sravani
  • 13
  • 3
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jens Oct 05 '21 at 10:42
  • No my question is totally different. Thank you! – sravani Oct 05 '21 at 11:11

1 Answers1

1

The problem happens because you are trying to manually instantiate the Activity DeviceListActivity. When you call getPackageManager() inside the methods from DeviceListActivity class it throws a NullPointerException because your activity is not managed by Android so it don't know how to provide a package manger for it since Android runtime don't even know about your deviceListActivity since you have instantiated it with new keyword. Don't do this. You have to let Android manage your Activities for you, never instantiate it with new keyword.

Read this to understand the proper way to start an activity.

Iogui
  • 1,526
  • 1
  • 17
  • 28
  • My requirement is to call activeIcon() and defaultIcon() in another java file.How to achieve? – sravani Oct 06 '21 at 05:31
  • But why do you need to call those methods? In order for me to be able to help, you need to provide more info. What are you trying to do? DeviceListActivity is your main activity, so in runtime, this is the first activity that should open. In what moment you want to call those methods and why? – Iogui Oct 06 '21 at 06:18
  • Requirement: Change app icon based on carplay connection status for one flavor.AndroidManifest.xml and code changes are given in post. My connectionstatus logic in defined in DeviceListAdapter.java and my activity in xml is DeviceListActivity.java. – sravani Oct 06 '21 at 06:30
  • Sorry, I don't understand what you intend to do. I don't know what is "carplay connetion status" for you and what its connection to Android programming. If you really want help with whatever you want to do, you have to better communicate it. I have already explained the reason you are getting a `NullPointerException` and what is wrong with your code. But I don't know what your DeviceListAdapter do, where is its entry point on the application and why you are trying to instantiate DeviceListActivity from it. Without this info, I can't help. – Iogui Oct 06 '21 at 16:07