Reputation: 1143
I'm coming from the C++ world and I think i'm missing something when it comes to managed code and android development. It makes sense to separate out all the UI stuff like strings into xml files. But I would expect errors to be caught at build time. In particular, NullPointerExecption errors can often occur due to bad or missing .xml components. It seems like JVM pushes catching problems to run-time which could be caught at build time, and this seems like a very bad thing.
The current error i'm getting has a NullPointerException but gives no line number. So it could come from a typo from any of my xml resources. This is very inefficient for debugging. To make things worse, I'm having difficulty in stepping through code to debug. Some files work fine, while others do not match the line numbers correctly and others can't be loaded by the debugger (PathClassLoader, BootClassLoader).
Surely there must be a better way to approach this? How can i get the line number that caused the NullPointerException?
Here is my code:
public class FragmentsMainActivity extends FragmentActivity {
public final static int STARTUP_ACTIVITY_RESULT=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragments_main);
Intent intentStartupActivity = new Intent(this, StartupActivity.class);
if(intentStartupActivity != null)
startActivityForResult(intentStartupActivity, STARTUP_ACTIVITY_RESULT);
// get an instance of FragmentTransaction from your Activity
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
//add a fragment
StreamingActivity streamingActivity = new StreamingActivity();
if (streamingActivity != null) {
fragmentTransaction.add(R.id.streamingactivity, streamingActivity);
fragmentTransaction.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragments_main, menu);
return true;
}
And here is my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kesten.fragmentstestbed"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="15" />
<!-- Sphero permissions -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".FragmentsMainActivity"
android:label="@string/title_activity_fragments_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- StartupActivity for connecting to a Robot -->
<activity
android:name="orbotix.robot.app.StartupActivity">
</activity>
</application>
</manifest>
here is the LogCat
07-18 10:37:25.600: W/dalvikvm(4835): threadid=1: thread exiting with uncaught exception (group=0x40020560)
07-18 10:37:25.680: E/AndroidRuntime(4835): FATAL EXCEPTION: main
07-18 10:37:25.680: E/AndroidRuntime(4835): java.lang.RuntimeException: Unable to start activity ComponentInfo{kesten.fragmentstestbed/kesten.fragmentstestbed.FragmentsMainActivity}:java.lang.NullPointerException
07-18 10:37:25.680: E/AndroidRuntime(4835): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1650)
07-18 10:37:25.680: E/AndroidRuntime(4835): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1666)
and the stack trace
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1650
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1666
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 117
Thanks to the answers, i found the "caused by" down the logCat trace where the line number is. This showed me that my fragment - StreamingActivity.onCreate() was calling getView() and this was returning null. This solved my NPE, so I will start a new thread to discuss the problems I'm having with the order of creating views in Activity and Fragment in another thread and mark this as solved.
Upvotes: 2
Views: 3654
Reputation: 86958
How can i get the line number that caused the NullPointerException?
The information is there, it's just a little cryptic. Consider this LogCat from NullPointerException/Help Reading LogCat:
06-11 11:56:08.118: W/dalvikvm(593): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-11 11:56:08.128: E/AndroidRuntime(593): FATAL EXCEPTION: main
06-11 11:56:08.128: E/AndroidRuntime(593): java.lang.RuntimeException: Unable to start activity ComponentInfo{gaurav.android/gaurav.android.CalcActivity}: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.os.Looper.loop(Looper.java:137)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-11 11:56:08.128: E/AndroidRuntime(593): at java.lang.reflect.Method.invokeNative(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593): at java.lang.reflect.Method.invoke(Method.java:511)
06-11 11:56:08.128: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-11 11:56:08.128: E/AndroidRuntime(593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-11 11:56:08.128: E/AndroidRuntime(593): at dalvik.system.NativeStart.main(Native Method)
06-11 11:56:08.128: E/AndroidRuntime(593): Caused by: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.onCreate(CalcActivity.java:58)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.Activity.performCreate(Activity.java:4465)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-11 11:56:08.128: E/AndroidRuntime(593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
You need to find the "Caused by: " farthest down your LogCat, in this case:
06-11 11:56:08.128: E/AndroidRuntime(593): Caused by: java.lang.NullPointerException
06-11 11:56:08.128: E/AndroidRuntime(593): at gaurav.android.CalcActivity.reset(CalcActivity.java:341)
Here we can see that CalcActivity throws a NullPointerException in reset(), specifically (CalcActivity.java:341)
tells us that it is on line 341. You can follow the stack trace down further if it will help, but this is how to read an Android LogCat for NPEs.
Hope that helps! Otherwise please post your full LogCat.
Upvotes: 2
Reputation: 10550
Eclipse will flag errors at compile time if it has to do with your XML. It won't let you build your application otherwise. NullPointerExceptions
can only be found at runtime because it entirely depends on whether a variable was assigned something or not. Of course there are easy cases such as setting a variable to null
and then never reassigning it later, but it is difficult for the compiler to detect all of your logic. You do not need to be checking if each object you create is null
like you are doing in your code.
As for solving your NullPointerException
problem you need to post the full stack trace given. It will usually have a Caused by
line in it with the line number where the error occurred.
Upvotes: 0