-4

I am new to coding and enthusiastically learning Android Studio, following on videos to do coding. I have already gone through that article - What is NullPointerException and understood the concept in few more related articles but I am unable to fix though I understand the error is on which code but if not that, then what is my question. I am facing this NullPointerException after pressing any button on calculator app:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at com.blogeomics.calcee.MainActivity.numberPressed(MainActivity.java:167)
                                                                           at com.blogeomics.calcee.MainActivity$7.onClick(MainActivity.java:97)

Code is below:

package com.blogeomics.calcee;

import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import static android.R.string.no;
import static android.os.Build.VERSION_CODES.O;

public class MainActivity extends AppCompatActivity {

    String runningNumber = "0";
    TextView resultsView;



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

        Button oneBtn = (Button) findViewById(R.id.button1);
        Button twoBtn = (Button) findViewById(R.id.button2);
        Button threeBtn = (Button) findViewById(R.id.button3);
        Button fourBtn = (Button) findViewById(R.id.button4);
        Button fiveBtn = (Button) findViewById(R.id.button5);
        Button sixBtn = (Button) findViewById(R.id.button6);
        Button sevenBtn = (Button) findViewById(R.id.button7);
        Button eightBtn = (Button) findViewById(R.id.button8);
        final Button nineBtn = (Button) findViewById(R.id.button9);
        Button zeroBtn = (Button) findViewById(R.id.button0);

        ImageButton calcBtn = (ImageButton) findViewById(R.id.buttonEqual);
        ImageButton divideBtn = (ImageButton) findViewById(R.id.buttonDivide);
        ImageButton multiplyBtn = (ImageButton) findViewById(R.id.buttonMultiply);
        ImageButton addBtn = (ImageButton) findViewById(R.id.buttonAdd);
        ImageButton subtractBtn = (ImageButton) findViewById(R.id.buttonSubtract);

        Button clearBtn = (Button) findViewById(R.id.buttonClear);
        TextView resultsView = (TextView) findViewById(R.id.result_view);

        resultsView.setText("");

        oneBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(1);

            }
        });

        twoBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(2);

            }
        });

        threeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(3);
            }
        });

        fourBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(4);
            }
        });

        fiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(5);
            }
        });

        sixBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(6);
            }
        });

        sevenBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(7);
            }
        });

        eightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(8);
            }
        });

        nineBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(9);
            }
        });

        zeroBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                numberPressed(0);
            }
        });

        calcBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        subtractBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        multiplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        divideBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

        clearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    void numberPressed(int number) {
        runningNumber = runningNumber + String.valueOf(number);
        resultsView.setText(runningNumber);


    }


}

I have gone through lot of posts about this error but still unable to figure out how to SOLVE my problem, I know it should be a minor thing yet I can't comprehend, please help.

  • Post layout xml – Romadro Oct 04 '17 at 07:18
  • 1
    change `TextView resultsView = (TextView) findViewById(R.id.result_view);` to `resultsView = (TextView) findViewById(R.id.result_view);` in `onCreate` – Aswin P Ashok Oct 04 '17 at 07:19
  • What is is on line 167? – meeeee Oct 04 '17 at 07:19
  • resultsView.setText(runningNumber); – Pankaj Dhawan Oct 04 '17 at 07:23
  • yeah @AswinPAshok is right – meeeee Oct 04 '17 at 07:26
  • You are bunch of geniuses, the problem is solved but please tell me why it was giving this nullpointer. How did it change by removing the TextView word? – Pankaj Dhawan Oct 04 '17 at 07:30
  • 2
    @PankajDhawan it is a scoping problem. look here: http://www.geeksforgeeks.org/variable-scope-in-java/ – meeeee Oct 04 '17 at 07:34
  • You declared `TextView resultsView;` on top of class as a Global object. But when you did this `TextView resultsView = (TextView) findViewById(R.id.result_view);` in onCreate, you were creating another textView object with same name (which means, eventhough those textViews have same name they are not the same). You can use that in onCreate only. So android does not know what that global `resultsView` is, since you are not calling it from onCreate. So it gave you a NullPointer exception. – Aswin P Ashok Oct 04 '17 at 07:37
  • Thank you for the detailed answer. I knew it was a minor bug but your support was really needed. Thanks again! – Pankaj Dhawan Oct 04 '17 at 13:47

1 Answers1

2

Change this line:

TextView resultsView = (TextView) findViewById(R.id.result_view);

to

resultsView = (TextView) findViewById(R.id.result_view);

now your resultsView will be global

sumit
  • 1,047
  • 1
  • 10
  • 15
  • Thank you Sumit, the problem is solved but could you please explain why I was getting error and why this change solved it, I apologize for asking this but I am still learning. – Pankaj Dhawan Oct 04 '17 at 07:31
  • 1
    you had defined your variable globally TextView resultsView but during initializing when you again wrote TextView resultsView = (TextView) findViewById(R.id.result_view); you created another variable with same name but this was a local variable whose scope was just inside onCreate function. So when you were using it inside your numberPressed method android was referring to the global variable which wasn't initialized. that is why you were getting Null pointer error – sumit Oct 04 '17 at 07:38
  • Wow! I didn't know I defined the same thing twice, thank you Sumit. – Pankaj Dhawan Oct 04 '17 at 13:46