6

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)
matiash
  • 54,791
  • 16
  • 125
  • 154
Rafael Nascimento
  • 715
  • 2
  • 8
  • 19
  • Do you know what a NullPointerException is? You stacktrace tells you that is the problem. It even tells you it is on line 89. What is the problem? – takendarkk Jul 06 '14 at 00:21
  • @Takendarkk line 89 where? I would say he should look at what happens in MainActivity on line 31, that seems to be the cause. Either way where the NullPointerException is thrown it would be nice with some code that supports why it happens. So if OP could supply some it would be awesome. – Cheesebaron Jul 06 '14 at 00:25
  • @codeMagic This is not actually a "standard" NPE. – matiash Jul 06 '14 at 00:39
  • It's definitely not line 89. He didn't write ContextWrapper. Without looking at the code, my guess is that it is the calling of uninitialized resource in the onCreate of MainActivity at line 31. – Stephan Branczyk Jul 06 '14 at 00:42
  • @StephanBranczyk From the call stack, notably `MainActivity.`, it looks like he's trying to access getResouces() from a field initializer (see answer below). Definitely closed way too quickly :( – matiash Jul 06 '14 at 00:43
  • How do you know it is being called from a field initializer? I'm not saying you're wrong, you're probably right, but I'd like to know how you arrived at that diagnosis so I can learn how to read the call stack more accurately the next time around. – Stephan Branczyk Jul 06 '14 at 00:46
  • @matiash how is it not a "standard" NPE (if there is one)? It is a `NPE` and that post explains it. It should help the OP to figure out what is `null` and why these happen. With that information, the OP can then find where it is and if still unable to figure out why, post more information and explain that there is something `null` but can't figure out why – codeMagic Jul 06 '14 at 00:48
  • When I see MainActivity. I think of the initialization of MainActivity, therefore I think of onCreate(). I didn't realize that . was about the initialization of its fields only. – Stephan Branczyk Jul 06 '14 at 00:49
  • @codeMagic What I meant is that it's an NPE thrown inside the framework classes. He doesn't have any null reference in his own code, it's purely a matter of a call in an inappropriate time. (By a "standard NPE" I meant one that is caused by dereferencing a pointer in your own code - very poor language choice, sorry) :/ – matiash Jul 06 '14 at 00:50
  • 2
    @StephanBranczyk `MainActivity.` refers specifically to the constructor (including field initializers). If the error occurred in `onCreate()`, the message would say so. – Code-Apprentice Jul 06 '14 at 00:52
  • When encountering issues like this, you should start by scanning down the stack trace to find a class which you wrote yourself. The cause of the problem is most likely on that line of code, even in this case where it is somewhat hidden. – Code-Apprentice Jul 06 '14 at 00:55
  • @matiash I wouldn't say it was closed too quickly as there are many reasons for it to be closed. However, I will reopen as this is slightly different. Good job pointing it out. rafanasil, [please see this post to help with preliminary debugging](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – codeMagic Jul 06 '14 at 00:58

2 Answers2

14

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.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • Just for people who are passing by and in trouble: I was trying to use `getResources()` before the `onCreate` call. Trying to fill an array of strings (from string.xml). As matiash said, this is not allowed. – Rafael Nascimento Jul 06 '14 at 14:03
0

The checked exception java.lang.NullPointerException is thrown when you're trying to use an object without reference,in simple words that means: you're trying to use a object variable without value.

Remember that a variable hold bits, primitive and non-primitive variable do, but the difference is that primitive variable hold the value in bits, but non-primitive variable hold the bits (like a direction) to access the object in memory, and when it's not being used anymore GGC (Garbage Colector) kills it.

Luis Pena
  • 4,132
  • 2
  • 15
  • 23