3

SO users, please read fully then decide to mark the Duplicate for NullPointer exception.

While app launch, from ApplicationExt(extends Application), we initialize the thread in which load the CSV file from Resource bundle(RawSource). It always works on many of the devices. But get crashed on below device for the very first time. For the second time, it gets loaded from the bundle.

OS version : Lollipop (5.1.1) Device : OnePlus A0001

Crash Log:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.content.res.StringBlock.get(int)' on a null object reference
       at android.content.res.AssetManager.getResourceValue(AssetManager.java:213)
       at android.content.res.Resources.getValue(Resources.java:1334)
       at android.content.res.Resources.getValue(Resources.java:1323)
       at android.content.res.Resources.openRawResource(Resources.java:1242)
       at android.content.res.Resources.openRawResource(Resources.java:1219)
       at com.xxxx.yyyy.network.NetworkManager.getBrandCodesFromCsvFile(NetworkManager.java:2961)
       at com.xxxx.yyyy.network.NetworkManager.getBrandCodes(NetworkManager.java:3221)
       at com.xxxx.yyyy.network.NetworkManager.checkAndUpdateMobileCodeFromBundle(NetworkManager.java:3110)
       at com.xxxx.yyyy.OurApplication$1.run(OurApplication.java:132)
       at java.lang.Thread.run(Thread.java:818)

Code Snippet:

In Application, onCreate(...),

  1. Create a thread with code of csv load
  2. Run the thread at the end of onCreate -> new Thread(ourThread).start();
  3. Exact crash on this line :

    InputStream bundleStream = OurApplicationClass.getContext().getResources().openRawResource(sourceId);
    

Note:

I've found related issue on Github. https://github.com/osmandapp/Osmand/issues/2575. But unable to confirm whether it's working for my case.

Mani
  • 17,549
  • 13
  • 79
  • 100
  • Can we stop sending everyone with an NPE to an 8 year post? I'm pretty sure a guy with 11k knows the textbook definition of NPE. Thank You! – Shaishav Aug 29 '16 at 12:26
  • @xenteros This is not NPE from our own code. This is from Stock android. Did you know what is difference between normal NPE and this? Did you see the stack trace? – Mani Aug 29 '16 at 12:30
  • @Shaishav Ya, you're right. I'm always know what is NPE and how can I fix it which happen on our code? Did you read the question fully.. Or Did you see the stack trace? – Mani Aug 29 '16 at 12:31
  • An alternative would be to load the `CSV` file from the assets folder. – K Neeraj Lal Aug 29 '16 at 12:34
  • @KNeerajLal Thanks for your suggestion. But how is it differ from loading from bundle resource? Possible to get into same problem on the asset case too. Right? The above problem is not always happening. It's random case and also couldn't find by reproduction steps. – Mani Aug 29 '16 at 12:41
  • @Yazan I've modified the package name here.. (com.xxxx.yyyy.network.NetworkManager.) There's no network activity. I think, there's no relation with bundle resource and network. There's no activity related code. So don't mess with activity. It's happen during very first time app launch. Also this is not normal case. This is extra-ordinary case. – Mani Aug 29 '16 at 12:56
  • @Yazan Crash happen on app launch. also again it wake up from launch too. So we cann't predict as you said. – Mani Aug 29 '16 at 12:59
  • @Rotwang How could this duplicate as like other NPE? Did you see my trace log? This is because of Android stock. So I'm looking for either fix or workaround. Read my question carefully then mark as what you get. – Mani Aug 29 '16 at 13:02
  • 1
    @Rotwang Your explanation is right for normal NPE. but this NPE is not because of my code. Please see the code one time then make your decision. – Mani Aug 29 '16 at 13:05
  • Wherever you are using a null resource reference or a null context reference in `at com.xxxx.yyyy.network.NetworkManager` – Phantômaxx Aug 29 '16 at 13:06
  • here. android.content.res.AssetManager.getResourceValue(AssetManager.java:213). I'm just passing the resource id as arg and invoke the resource loading by Application context. – Mani Aug 29 '16 at 13:07
  • @Rotwang It may too. See this related issue here. https://github.com/osmandapp/Osmand/issues/2575. But the guy found the workaround. I'm looking for the same. – Mani Aug 29 '16 at 13:13
  • @Yazan I'm less prone to believe in a Google Development Team bug than in a bug in OUR code. – Phantômaxx Aug 29 '16 at 13:14
  • Did you find a solution? I have a similar problem but while loading views. On hypothesis we have at work is this may be due to too much memory being used so we have been chasing all memory leaks, cannot confirm yet if the issue was fixed as we have many activities. – vallllll Sep 26 '16 at 14:06

0 Answers0