0

can someone please tell me why in the following line the code throws a NullPointer.

public List<ApplicationInfo> getAppInfos(Context myContext) {
    PackageManager pm = myContext.getPackageManager();
    List<ApplicationInfo> appInfo = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    Collections.sort(appInfo, new ApplicationInfo.DisplayNameComparator(pm)); //throws Nullpointer
    return appInfo;
}

I try to call this method from a seperate Thread (not the UIThread). But i passed the Context from my Activity... If i call this method from my Activity(without passing the context for sure) it works fine.

I appreciate any help.

Update regarding Comment:

Thank you for your respone. That's a tricky Thing. Because the Code runs fine on Emulator with API 17 but not on my real device. And i got a cool Alcatel phone which is, for me, really tricky to get it run for usb Debugging(i think not just for me, the Support is awful). Under some circumstances i achieved to Crash the app on the Emulator as well. I only remember a nullpointerexception, but maybe i mixed something. But what i can say is that when i comment the Collections.sort... out. The app runs on the device as well. I'm sorry for being so confusing right now. The only Thing i can say is that this line crashes my app on my device, but not on my Emulator and only if i call this from an seperate Thread.

I instantiate Context like this:

final Context myContext = MyActivity.this;

And in the Thread i just call the method like this:

List<ApplicationInfo> myList = getAppInfos(myContext);

PHONE and Emulator are running with same Android Version!

MMike
  • 598
  • 3
  • 23
  • 3
    When you debug it, which object is `null`? – David Nov 19 '14 at 20:17
  • try changing `pm` identifier to `final` . – Mohammad Rahchamani Nov 19 '14 at 20:21
  • 1
    How do you pass the Context? There are several methods to do so, some of which have different results... – jesses.co.tt Nov 19 '14 at 20:23
  • Can you try using this call instead: http://developer.android.com/reference/android/content/Context.html#getApplicationContext() ... So it would be final Context myContext = getApplicationContext() – jesses.co.tt Nov 19 '14 at 20:47
  • 1
    @jesses.co.tt Tried, same Problem. Once again, Emulator and Device are running on API 17. I don't get why it works on Emulator but not on my real Device... Any other hints jesses.co.tt .i would appreciate that. – MMike Nov 19 '14 at 20:56
  • @MohammadRahchamani Tried, Problem stays. Any other hints? Would appreciate that.. – MMike Nov 19 '14 at 20:58
  • At the least, if you know what line is giving the NPE, you can try/catch it and give the user a Toast that you were unable to do what you were trying to do... – jesses.co.tt Nov 19 '14 at 20:58
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Rich Schuler Nov 19 '14 at 21:14
  • @jesses.co.tt it's not something which i can simply take out of my app. I will try the wireless debbuging... and give update... – MMike Nov 19 '14 at 21:21

1 Answers1

0

You are getting the nullPointerException because pm in:

PackageManager pm = myContext.getPackageManager();

is returning null. Without seeing how you get your context, it's hard to say why sometimes it's null and sometimes it's not.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • Thank you for your Response. On the Emulator the app always runs. Regardless if call it from the UIThread or a seperate Thread. On my Device, Alcatel One Touch X 6040 also API 17 it crashes when called from a seperate thread. But runs when called from UIThread. Updated Question how i did pass the context – MMike Nov 19 '14 at 20:30
  • @MMike This could be an issue with the device. Devices handle things differently. It's an issue developing for Android. It might work on a HTC phone, but doesn't work exactly the same on a Samsung. I would use your debugger and step through the process when you run it on the phone and see what's happening. – BlackHatSamurai Nov 19 '14 at 20:42
  • Hey, maybe i was too late, but i mentioned in the Question under Update that i can't run USB Debugging on that Alcatel Phone. The Support is awful. I spent a day to get it run, but then i gave up. So i can't step through debbung with my Phone! ;/ that's the Point. I know this problem. But it's sad that such a simple line crashes the app on a whole device. I don't know why this should be.. Can you provide any other hints? Thank you – MMike Nov 19 '14 at 20:48
  • @jesses.co.tt ok i will try! Never heard about that – MMike Nov 19 '14 at 21:20
  • @jesses.co.tt after some Research, i found out that wireless debbuging is just possible if your device is rooted or if it is successfully connected via USB Debugging(for Setup wireless Debugging)... my phone isn't rooted and i don't want to(warranty and so on).. and usb Debugging is not working.. i think you forogt that... maybe you got other hints..? – MMike Nov 20 '14 at 14:24
  • Yeah, i forgot about that... You could signup for some analytic plan, like Crashalytics or HockeyApp? Then you can at least get the stacktrace... – jesses.co.tt Nov 20 '14 at 16:30