0

I get an error by "btnCalculate.setOnClickListener(new View.OnClickListener()" saying

"Method invocation 'btnCalculate.setOnClickListener(new View.OnClickListener() { @Override ...' may produce 'java.lang.NullPointerException"

What does this mean and how can I fix it?

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity
{
    TextView totalTextView;
    EditText edtPercentage;
    EditText edtNumber;

    float percentage;
    float number;
    float total;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        percentage = 0;
        number = 0;
        total = 0;

        Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
        btnCalculate.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                percentage = Float.parseFloat(edtPercentage.getText().toString());
                number = Float.parseFloat(edtNumber.getText().toString());
                total = number * percentage/100;
                totalTextView.setText(String.valueOf(total));
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

3 Answers3

0

btnCalculate may be null when it is not found in the layout - to get rid of the warning you might add:

 assert(btnCalculate!=null)

before the warning

ligi
  • 39,001
  • 44
  • 144
  • 244
0

You have not 'connected' the totalTextView edtPercentage and edtNumber using the findReferenceByID() method. In the onCreate method do

.....
setContentView(R.layout.activity_main());
totalTextView = (TextView)findViewByID(R.id.<IdOfTextView>);
edtPercentage = (EditText)findViewByID(R.id.<IdOfEditText>);
edtNumber    = (EditText)findViewByID(R.id.<IdOfEditText>);
.....
Aswin P J
  • 546
  • 4
  • 13
0

The msg is quite clear:

this object TextView totalTextView must be initialized (normally in the onCreate method) before you try to set the text on it, but you forgot to do it, you app is going to crash as soon as you click the button...

Fix it!

do this:

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       totalTextView = (TextView) findViewById(R.id.toolbar); //init the text view....
       ....
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97