1

Below is my function to get the names of all installed apps into a List.

Below the function is the failing stacktrace - what am I doing wrong?

@SuppressWarnings("null")
private List<String> getInstalledAppsList() {
    List<String> l_installedApps = null;

    List<PackageInfo> allPackagesList = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);

    PackageManager pm = getPackageManager();

    for(int i = 0 ; i < allPackagesList.size() ; i++) {
        PackageInfo p = allPackagesList.get(i);

        l_installedApps.add(p.applicationInfo.loadLabel(pm).toString());  //THIS IS WHERE EXCEPTION IS OCCURRING
    }

    return l_installedApps; 
}

Failing Stacktrace:

05-26 21:59:22.128: E/AndroidRuntime(3270): FATAL EXCEPTION: main
05-26 21:59:22.128: E/AndroidRuntime(3270): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mavdev.focusoutfacebook/com.mavdev.focusoutfacebook.MainActivityCircularSeekbar}: java.lang.NullPointerException
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.os.Looper.loop(Looper.java:137)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread.main(ActivityThread.java:5103)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at java.lang.reflect.Method.invoke(Method.java:525)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at dalvik.system.NativeStart.main(Native Method)
05-26 21:59:22.128: E/AndroidRuntime(3270): Caused by: java.lang.NullPointerException
05-26 21:59:22.128: E/AndroidRuntime(3270):     at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar.getInstalledAppsList(MainActivityCircularSeekbar.java:2045)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at com.mavdev.focusoutfacebook.MainActivityCircularSeekbar.onCreate(MainActivityCircularSeekbar.java:798)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at  android.app.Activity.performCreate(Activity.java:5133)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-26 21:59:22.128: E/AndroidRuntime(3270):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-26 21:59:22.128: E/AndroidRuntime(3270):     ... 11 more
user1406716
  • 9,565
  • 22
  • 96
  • 151

2 Answers2

2

l_installedApps = null

Where do you initialize it?

you shoud do this before adding items:

l_installedApps = new ArrayList<String>();
Oleg Gryb
  • 5,122
  • 1
  • 28
  • 40
  • i am doing *List l_installedApps = null;* in the first line of the function - doing something wrong? – user1406716 May 27 '14 at 03:11
  • you pointed me in the right direction, i am initializing it right. I need to do *List l_installedApps = new ArrayList();* (the *new List()* will not work as per: http://stackoverflow.com/questions/13395114/how-to-initialize-liststring-object-in-java, i tested too - thank you though! +1 for you) – user1406716 May 27 '14 at 03:15
  • 1
    you're right, it should be ArrayList, but the root cause is still l_installedApps, which was set to null – Oleg Gryb May 27 '14 at 03:20
1

That is throwing NullPointerException because l_installedApps is null. you should initialize it like so:

List<String> l_installedApps = new ArrayList<String>();

Be sure to import

java.util.ArrayList;
Keale
  • 3,924
  • 3
  • 29
  • 46