-2

My Android application crashes in the emulator with, with the following being output to the logs:

09-26 14:04:02.123: D/dalvikvm(716): GC_FOR_ALLOC freed 66K, 6% free 2675K/2844K, paused 29ms, total 30ms
09-26 14:04:02.123: I/dalvikvm-heap(716): Grow heap (frag case) to 3.455MB for 782224-byte allocation
09-26 14:04:02.173: D/dalvikvm(716): GC_FOR_ALLOC freed 2K, 5% free 3437K/3608K, paused 42ms, total 42ms
09-26 14:04:02.303: D/dalvikvm(716): GC_FOR_ALLOC freed <1K, 5% free 3436K/3608K, paused 38ms, total 39ms
09-26 14:04:02.314: I/dalvikvm-heap(716): Grow heap (frag case) to 4.778MB for 1389712-byte allocation
09-26 14:04:02.363: D/dalvikvm(716): GC_FOR_ALLOC freed <1K, 4% free 4793K/4968K, paused 52ms, total 52ms
09-26 14:04:02.423: D/AndroidRuntime(716): Shutting down VM
09-26 14:04:02.423: W/dalvikvm(716): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
09-26 14:04:02.433: E/AndroidRuntime(716): FATAL EXCEPTION: main
09-26 14:04:02.433: E/AndroidRuntime(716): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sample_proj/com.example.sample_proj.MainActivity}: java.lang.NullPointerException
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.os.Looper.loop(Looper.java:137)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread.main(ActivityThread.java:5103)
09-26 14:04:02.433: E/AndroidRuntime(716):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 14:04:02.433: E/AndroidRuntime(716):  at java.lang.reflect.Method.invoke(Method.java:525)
09-26 14:04:02.433: E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-26 14:04:02.433: E/AndroidRuntime(716):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 14:04:02.433: E/AndroidRuntime(716):  at dalvik.system.NativeStart.main(Native Method)
09-26 14:04:02.433: E/AndroidRuntime(716): Caused by: java.lang.NullPointerException
09-26 14:04:02.433: E/AndroidRuntime(716):  at com.example.sample_proj.MainActivity.onCreate(MainActivity.java:21)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.Activity.performCreate(Activity.java:5133)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-26 14:04:02.433: E/AndroidRuntime(716):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-26 14:04:02.433: E/AndroidRuntime(716):  ... 11 more

Would someone please help me in understanding why NullPointerException is being thrown and how I can resolve it?

romi
  • 17
  • 2
  • Please post some code. Logcat alone will not help. – S.A.Norton Stanley Sep 26 '13 at 18:11
  • Where is the code of your `MainActivity`? – SimonSays Sep 26 '13 at 18:12
  • Post your MainActivity.java code here. – S.A.Norton Stanley Sep 26 '13 at 18:19
  • [See this answer](http://stackoverflow.com/questions/16782558/what-is-the-best-way-to-debug-the-android-code-in-eclipse/16782621#16782621) or [this answer](http://stackoverflow.com/questions/18964329/eclipse-logcat-debugging/18964524#18964524) about how to read your logcat. Most likely you aren't initializing a `View` correctly like maybe trying to initialize before calling `setContentView()` – codeMagic Sep 26 '13 at 18:20

2 Answers2

0

Check this line in your MainActivity

MainActivity.java:21
S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
0

You have probably used findviewbyid and the following happened

  1. you forgot to put the line "setContentView" or put it after findviewbyid
  2. or you put the contentView to the wrong layout
  3. 1 and 2 are not it, and you did findviewbyid to non existing id in the given layout- and tried to use that object aterwards, because the id is incorrect, the object returns null, and you are trying to use it
Lena Bru
  • 13,521
  • 11
  • 61
  • 126
  • Note that `NullPointerException`s can have a lot of causes, but this is one of the most common ones in Android development. – Robin Kanters Sep 26 '13 at 18:34
  • because he provided no code - and his project is called "sample" i guessed that he is a beginner, and these were the most often mistakes i made on which i got NPE when was i was a beginner – Lena Bru Sep 26 '13 at 22:28
  • 1
    I know, I just added it for the sake of completeness – Robin Kanters Sep 28 '13 at 20:13