Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

java.lang.NullPointerException:Attempt to invoke virtual method android.content.pm.PackageManager android.content.Context.getPackageManager()

Ask Question

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();

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.

My requirement is to call activeIcon() and defaultIcon() in another java file.How to achieve? – sravani Oct 6, 2021 at 5: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 6, 2021 at 6: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 6, 2021 at 6: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 6, 2021 at 16:07

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.