Click here to Skip to main content
14,773,753 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Below are my MainActivity and Layout file for Android calculator app.
Everytime I run the app , it crashes and inside the LOGCAT of android studio, the exception occurs.
LOGCAT MESSAGE BELOW :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference



MainActivity Code Below :

package com.hfad.calculatorx;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setAllListners();

    }

    //Referencing all components
    TextView Display =(TextView) findViewById(R.id.Display);
    Button button0 =(Button) findViewById(R.id.button0);
    Button button1 =(Button)findViewById(R.id.button1);
    Button button2 =(Button)findViewById(R.id.button2);
    Button button3 =(Button)findViewById(R.id.button3);
    Button button4 =(Button)findViewById(R.id.button4);
    Button button5 =(Button)findViewById(R.id.button5);
    Button button6 =(Button)findViewById(R.id.button6);
    Button button7 =(Button)findViewById(R.id.button7);
    Button button8 =(Button)findViewById(R.id.button8);
    Button button9 =(Button)findViewById(R.id.button9);
    Button buttonClear =(Button)findViewById(R.id.buttonclear);
    Button buttonResult =(Button)findViewById(R.id.buttonresult);


    //Making OnclickListners for all Buttons

    View.OnClickListener Button0Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"0");
        }
    };


    View.OnClickListener Button1Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"1");
        }
    };

    View.OnClickListener Button2Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"2");
        }
    };

    View.OnClickListener Button3Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"3");
        }
    };


    View.OnClickListener Button4Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"4");
        }
    };

    View.OnClickListener Button5Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"5");
        }
    };

    View.OnClickListener Button6Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"6");
        }
    };

    View.OnClickListener Button7Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"7");
        }
    };

    View.OnClickListener Button8Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"8");
        }
    };

    View.OnClickListener Button9Listner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.append(Display.getText()+"9");
        }
    };


    View.OnClickListener ButtonClearListner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Display.setText("");
        }
    };


    View.OnClickListener ButtonResultListner = new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Part left empty intentionally

        }
    };

    //Setting all listners at once

    public void setAllListners(){
        button0.setOnClickListener(Button0Listner);
        button1.setOnClickListener(Button1Listner);
        button2.setOnClickListener(Button2Listner);
        button3.setOnClickListener(Button3Listner);
        button4.setOnClickListener(Button4Listner);
        button5.setOnClickListener(Button5Listner);
        button6.setOnClickListener(Button6Listner);
        button7.setOnClickListener(Button7Listner);
        button8.setOnClickListener(Button8Listner);
        button9.setOnClickListener(Button9Listner);
        buttonClear.setOnClickListener(ButtonClearListner);
        buttonResult.setOnClickListener(ButtonResultListner);
    }

}



Layout File Below :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/Display"
        android:layout_width="395dp"
        android:layout_height="80dp"
        android:layout_marginTop="60dp"
        android:background="@color/display_background"
        android:gravity="center_horizontal|bottom"
        android:textColor="@color/black"
        android:textSize="25dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.529"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginBottom="48dp"
        android:text="1"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="44dp"
        android:text="2"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintEnd_toStartOf="@+id/buttonadd"
        app:layout_constraintStart_toEndOf="@+id/button1" />

    <Button
        android:id="@+id/buttonadd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="52dp"
        android:text="+"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/buttonsubtract"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginBottom="36dp"
        android:text="3"
        app:layout_constraintBottom_toTopOf="@+id/button5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:text="4"
        app:layout_constraintBottom_toTopOf="@+id/button6"
        app:layout_constraintEnd_toStartOf="@+id/buttonsubtract"
        app:layout_constraintStart_toEndOf="@+id/button3" />

    <Button
        android:textSize="25sp"
        android:id="@+id/buttonsubtract"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="40dp"
        android:text="-"
        app:layout_constraintBottom_toTopOf="@+id/buttondivide"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginBottom="40dp"
        android:text="5"
        app:layout_constraintBottom_toTopOf="@+id/button8"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:text="6"
        app:layout_constraintBottom_toTopOf="@+id/button8"
        app:layout_constraintEnd_toStartOf="@+id/buttondivide"
        app:layout_constraintStart_toEndOf="@+id/button5" />

    <Button
        android:textSize="25sp"
        android:id="@+id/buttondivide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="40dp"
        android:text="/"
        app:layout_constraintBottom_toTopOf="@+id/buttonmultiply"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="40dp"
        android:text="7"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button5" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:text="8"
        app:layout_constraintBottom_toTopOf="@+id/button0"
        app:layout_constraintEnd_toStartOf="@+id/buttonmultiply"
        app:layout_constraintStart_toEndOf="@+id/button7" />

    <Button
        android:textSize="25sp"
        android:id="@+id/buttonmultiply"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="36dp"
        android:text="*"
        app:layout_constraintBottom_toTopOf="@+id/buttonclear"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="36dp"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="40dp"
        android:text="9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button7"
        app:layout_constraintVertical_bias="0.007" />

    <Button
        android:textSize="25sp"
        android:id="@+id/button0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        android:text="0"
        app:layout_constraintBottom_toTopOf="@+id/buttonresult"
        app:layout_constraintEnd_toStartOf="@+id/buttonclear"
        app:layout_constraintHorizontal_bias="0.506"
        app:layout_constraintStart_toEndOf="@+id/button9" />

    <Button
        android:textSize="22sp"
        android:id="@+id/buttonclear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="36dp"
        android:layout_marginRight="36dp"
        android:layout_marginBottom="128dp"
        android:text="Clear"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:textSize="22sp"
        android:id="@+id/buttonresult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="48dp"
        android:text="Result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>


What I have tried:

I have tried playing around with the Methods but didnt work
Posted
Updated 1-Jul-20 4:10am
Comments
Richard MacCutchan 1-Jul-20 9:16am
   
You could get rid of nine of those button listeners and use one common one for all the buttons. That would simplify your code quite considerably.
David Crow 1-Jul-20 12:59pm
   
Comment out the call to setAllListners(). Does the exception go away?

1 solution

This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and your IDE will help you here. Run your program in the debugger and when it fails, it will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, the debugger will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900