0

I have a method in my android app that returns the drawable id according to it's name, this method works so well in the normal cases, but when running it from a separate thread it returns Null, even when running this thread on the UIThread.

This method is :

public int get_drawable(int status){
     int res;
     res = getResources().getIdentifier("something"+ Integer.toString(status) , "drawable", getPackageName()); // this is the line which throws the Exception
     return res;
}

And the Exception:

02-04 13:43:14.644: WARN/System.err(594): java.lang.NullPointerException
02-04 13:43:14.664: WARN/System.err(594):     at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
02-04 13:43:14.664: WARN/System.err(594):     at com.example.solaceap.HomeView.get_drawable(HomeView.java:525)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.alter_light(JSONParsing.java:689)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.parse_hmm_response(JSONParsing.java:297)
02-04 13:43:14.674: WARN/System.err(594):     at com.example.solaceap.JSONParsing.check_hmm_type(JSONParsing.java:218)
02-04 13:43:14.684: WARN/System.err(594):     at com.example.solaceap.JSONParsing.parse_hmm_response(JSONParsing.java:170)
02-04 13:43:14.684: WARN/System.err(594):     at com.example.solaceap.Login.parse_response(Login.java:143)
02-04 13:43:14.704: WARN/System.err(594):     at com.example.solaceap.Login.response_received(Login.java:129)
02-04 13:43:14.704: WARN/System.err(594):     at com.example.solaceap.Login$2$1.run(Login.java:106)
02-04 13:43:14.714: WARN/System.err(594):     at android.os.Handler.handleCallback(Handler.java:605)
02-04 13:43:14.724: WARN/System.err(594):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 13:43:14.724: WARN/System.err(594):     at android.os.Looper.loop(Looper.java:137)
02-04 13:43:14.724: WARN/System.err(594):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-04 13:43:14.724: WARN/System.err(594):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 13:43:14.724: WARN/System.err(594):     at java.lang.reflect.Method.invoke(Method.java:511)
02-04 13:43:14.724: WARN/System.err(594):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-04 13:43:14.734: WARN/System.err(594):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-04 13:43:14.734: WARN/System.err(594):     at dalvik.system.NativeStart.main(Native Method)

UPDATE:

after doing some tries i found that the getPackageName() is the one which throws that Exception, even when tried to statically provide my resources package name it gives me the same result.

HomeView is an Activity class in my android app, but the JSONParsing class is a normal java class which calls this method from the HomeView.

MRefaat
  • 515
  • 2
  • 8
  • 22

3 Answers3

1

The stacktrace says the exception happens in ContextWrapper.getResources(). It can only NPE if the base context is null.

The base context can be null if you're trying to call the method before your activity onCreate(). You cannot use an activity as a Context until onCreate() in its lifecycle.

Also, you cannot instantiate activities with new as all the setup code involving e.g. setting up context resources will not be run.

For a better answer we'd need to know what HomeView is and how you're actually instantiating it.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Possibly, possibly not - please add the complete stacktrace to the question. – laalto Feb 04 '14 at 11:41
  • The stacktrace disagrees with the `getPackageName()` theory. – laalto Feb 04 '14 at 13:48
  • i just tried to get the package name in a line before and got the same stacktrace at that line – MRefaat Feb 04 '14 at 13:49
  • Yes, the base context is not set up for `getPackageName()` either. The activity is not properly initialized as a `Context`. – laalto Feb 04 '14 at 13:50
  • HomeView is an Activity class in my android app, but the JSONParsing class is a normal java class which calls this method from the HomeView. – MRefaat Feb 05 '14 at 07:33
1

From

    res = getResources().getIdentifier("something"+ Integer.toString(status) , "drawable", getPackageName()); 

Seems most likely: check the value for status(if status=12) and make sure you have a drawable with the name "something12" .

..check package name with context.getPackageName or getApplicationContext.getPackageName. Can pass the context with status parameter to make sure its not null

"res" is null for you so either the context is null(incorrect package name - can try the passing the package name in "" like "drawable") or there is no name of drawable matching the text being passed

Pararth
  • 8,114
  • 4
  • 34
  • 51
  • i figured out that the package name is the problem and it's the something which throws that exception, but even after changing `getPackageName()` with my app resources package name inside quotes it just obey the same behavior(runs normally in the first time then throws that exception when trying to access it again ) – MRefaat Feb 04 '14 at 13:38
0

Try out as below:

getResources().getIdentifier("something"+ Integer.toString(status) , "drawable", this.getPackageName().toString());
GrIsHu
  • 29,068
  • 10
  • 64
  • 102