[Java for Android App Development] nikwen:
Logcat

As I have already stated, logcats are the most important aspect of debugging on Android.

You can use them to get error messages when your app crashed or to print your own debugging information.

Understand error messages

This is one example of an error message you can get:

06-15 12:45:02.205 805-805/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nikwen.myapplication/de.nikwen.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at de.nikwen.myapplication.MainActivity.doSomething(MainActivity.java:21)
at de.nikwen.myapplication.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more

The code producing the error:

package de.nikwen.myapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;

public class MainActivity extends Activity {

Button myButton;

@[B][/B]Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

doSomething();
}

private void doSomething() {
myButton.setText("Crashing here");
}

}

Now we are going to analyse the error.

The second line is the most important one. It tells you which kind of error ocurred. In this case it is a NullPointerException (NPE).

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.nikwen.myapplication/de.nikwen.myapplication.MainActivity}: java.lang.[COLOR="Red"]NullPointerException[/COLOR]

The first thing to do now is searching for this type of error. When does it occur?

You come up with something like this:

(Source: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/NullPointerException.html)

Now you know what kind of Exception was thrown. You can get a lot of other useful information from the logcat:

Usually, you get some more information about the error. There should be an explanation why the Exception was thrown:

java.lang.RuntimeException: [COLOR="Red"]Unable to start activity[/COLOR] ComponentInfo{de.nikwen.myapplication/de.nikwen.myapplication.MainActivity}: java.lang.NullPointerException

It also prints the stacktrace. The stacktrace contains all methods which have been invoked, the last one on the top.

In this case one exception caused another one. In most cases, the one which caused the other one is the one which is interesting for you.

Caused by: java.lang.NullPointerException

In this case the one which was thrown first is a NPE as well.

Let us have a closer look at the stacktrace:

[COLOR="Red"]at de.nikwen.myapplication.MainActivity.doSomething(MainActivity.java:21)
at de.nikwen.myapplication.MainActivity.onCreate(MainActivity.java:17)[/COLOR]
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

The ones which are important for you usually are the ones with your package name (the red ones).

At the end of the line it tells you in which file and in which line the error occured.

at de.nikwen.myapplication.MainActivity.onCreate[COLOR="Red"](MainActivity.java:17)[/COLOR]

Here it is just the part of the code where the other method is called. So we look at the other one. (This most often happens when there are two of your methods. For that reason have a look at the upper one first.)

at de.nikwen.myapplication.MainActivity.doSomething[COLOR="Red"](MainActivity.java:21)[/COLOR]

It tells us MainActivity.java, line 21.

That is the line:

myButton.setText("Crashing here");

Remember, we got a NullPointerException. The only variable that can get null in that line is myButton. In fact we initialise it nowhere.

So this is our new onCreate method:

@[B][/B]Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myButton = (Button) findViewById(R.id.my_button);

doSomething();
}

We solved the error. :victory: