Debug and fix an application crash (uncaught exception) (Associate Android Developer Certification)

Let’s create uncaught exception

String word = null;
char[] letters = word.toCharArray();

We get such a log:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
at com.gayratrakhimov.logsandbox.MainActivity.onCreate(MainActivity.java:17)

We can see that this exception caused because something is null. In our case, String name is null. Here MainActivity.java:17 is highlighted and underlided. Click on it, and you can see where exception was happened.

char[] letters = word.toCharArray();

Let’s fix this code:

String word = "Joe";
char[] letters = word.toCharArray();

Very easy!