null pointer exception in android, what is null pointer exception in android, how to handle null pointer exception in android, how to fix null pointer exception in android, how to handle null pointer exception in android

Null pointer exception in android studio

Whenever one is working with the android studio to develop android applications, errors and exceptions are common things that every developer must be ready to deal with. The biggest challenge when it comes to errors and exceptions is not knowing how to handle them or identifying where they are occurring at.

There are different kinds of errors and exceptions that are prone to happen when developing android applications, some of these errors we have highlighted how to solve them in these previous articles.

In this article, we shall discuss in detail what null pointer exception in android is, how to handle null pointer exception in android, how to avoid null pointer exception in android, and also how to fix null pointer exception in android.

What is a null pointer exception in android?

A null pointer exception is regarded as a type of error that returns null whenever an object or instance is called. When the result is null, the exception occurs and the thread stops executing and what happens next is that the app crashes.

When does null pointer exception occur in android?

From the definition of a null pointer exception, it is clear that is known as null since the instance referenced is null or the result generated is null.

Some know ways or reasons why null pointer exception occurs in android according to official android developer’s documentation are;

  • When calling a method of a null object
  • Sending a null value when a real value is required
  • Trying to access or modifying a null value

How to avoid null pointer exception in android?

From the above listing of what causes null pointer exception in android, you can easily avoid it easily.

First of all, you will need to have a clear understanding of android data types, methods, and objects to ensure that every time you are handling each instance everything is in the correct format.

Also ensure that you are using the correct data types and you have defined and instantiated each data type as they are supposed to, for example, understand how to use global variables and local variables.

In a case where you try to use a local variable outside the defined method or class, you will get the exception.

Ensure that the value you are sending to a method or object is not null and also check the response of a particular action either using logcat or toast to check whether it’s a null result or not.

How to handle null pointer exception in android?

If a null result occurs at any time due to some reasons which may not be predictable, you can handle it and avoid your app from crashing.

The most common and used way to handle null pointer exception is the use of a try-catch block. This assists in a way that, when the exception occurs in the try block, it will jump that section and move to the catch block where the exception is handled or displayed.

The catch block syntax in java language is used as follows;

try {

// block of code to execute

}catch (Exception e){

// handling exception


The usage of a try-catch block is demonstrated in the example below

How to fix null pointer exception in android

To fix null pointer exception starts at the stage of first identifying what is causing the exception.

Next step of fixing the exception is by handling the exception using the try-catch block and displaying a toast or using logcat to view the line where the exception is occurring.

Finally is ensuring that at no stage you are submitting a null value or trying to access a null value.

An example demonstrating how null pointer occurs in android

Am creating a sample android app with a text view and a button, am going to use the button defined to update or change the text of the text view.

The XML code for design is as follows;

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""













        android:text="Hello World!"







        app:layout_constraintTop_toTopOf="parent" />












        app:layout_constraintTop_toTopOf="parent" />


The current text for the defined text view with id textView1 is Hello World, but once we click the button submit, we want to change the text to This is an updated text.

In the java code, we have initialized two text views textview1 and textview2 where textview1 is the one to reference the one to change the text.

The exception will occur since we are trying to update a text in a text view that is not defined and that will result to a null pointer exception as shown in the java code below

package com.example.null_p;




import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

TextView textview1,textview2;

Button button1;


    protected void onCreate(Bundle savedInstanceState) {







        button1.setOnClickListener(new View.OnClickListener() {


            public void onClick(View view) {

                try {

                    textview2.setText("This is an updated text");

                }catch (Exception e){

                    Toast.makeText(MainActivity.this,"An exception occured"+e,Toast.LENGTH_LONG).show();






To ensure that the application will not crash and give an error shown below


    Process: com.example.null_p, PID: 4029

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

Null pointer exception in android studio

We use the try-catch block as shown in the code above such that instead of the app crashing when you click the button, we handle the exception and display it as a toast message.


From the above discussion, we have defined what is a null pointer exception, why and when it occurs in android, how to handle and prevent the null pointer exception, and also how to fix the exception.

Given the example we have shown and the image of the error when the exception has occurred, you can now be able to identify it and easily fix it.

That's it for this discussion, thank you for following.