Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException in ContextWrapper.getResources()

Tags:

android

I really don't know what's going on with the Android app I'm building. At some point on its development, I started getting it breaking as soon as it starts in the emulator, with the message "Foo stopped". Here is the error log, if anyone could help me:

07-05 21:13:30.063    7647-7647/root.fazerumsom E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: root.fazerumsom, PID: 7647
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{root.fazerumsom/root.fazerumsom.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
        at root.fazerumsom.MainActivity.<init>(MainActivity.java:31)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
like image 815
Rafael Nascimento Avatar asked Jul 06 '14 00:07

Rafael Nascimento


People also ask

Why NullPointerException occur in the code?

Why NullPointerException occur in the code NullPointerException is a situation in code where you try to access/ modify an object which has not been initialized yet. It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘ null ‘. A example java program which throws null pointer exception. 2.

What is the use of getResource in Java?

Java Class getResource () Method The getResource () method of java Class class is used to return the resources of the module in which this class exists. The value returned from this function exists in the form of the object of the URL class.

Why do I get a null pointer exception when calling a method?

If you are dealing with static variables or static method than you won’t get null pointer exception even if you have your reference variable pointing to null because static variables and method call are bonded during compile time based on class name and not associated with object


1 Answers

You're calling getResources() from a field initializer. Something like

private String mStr = getResources().getString(...);

(or with drawables or other kind of resources).

This is not valid, since the Context isn't set up yet. Move this assignment inside the onCreate() method.

like image 140
matiash Avatar answered Oct 28 '22 04:10

matiash