In my android project java.lang.nullpointer exception..Attempt to invoke a virtual method setOnclickListner

90 Views Asked by At
Attempt to invoke virtual method void android.widget.Button.setonClickListner on a null object reference

I have checked my XML file and all Buttons have correct id's, but this error persists.

Activity_main.java

    e1=(EditText)findViewById(R.id.editText);
    e1.setCursorVisible(false);

    b1=(Button)findViewById(R.id.one);
    b2=(Button)findViewById(R.id.two);
    b3=(Button)findViewById(R.id.three);
    b4=(Button)findViewById(R.id.four);
    b5=(Button)findViewById(R.id.five);
    b6=(Button)findViewById(R.id.six);
    b7=(Button)findViewById(R.id.seven);
    b8=(Button)findViewById(R.id.eight);
    b9=(Button)findViewById(R.id.nine);
    b0=(Button)findViewById(R.id.zero);
    minus=(Button)findViewById(R.id.minus);
    plus=(Button)findViewById(R.id.plus);
    dot=(Button)findViewById(R.id.dot);
    multi=(Button)findViewById(R.id.multi);
    divide=(Button)findViewById(R.id.divide);
    equals=(Button)findViewById(R.id.equals);
    del=(Button)findViewById(R.id.del);
    ac=(Button)findViewById(R.id.ac);
    sqrt=(Button)findViewById(R.id.sqrt);
    sin=(Button)findViewById(R.id.sin);
    cos=(Button)findViewById(R.id.cos);
    tan=(Button)findViewById(R.id.tan);
    plusminus=(Button)findViewById(R.id.plusminus);
    inverse=(Button)findViewById(R.id.inverse);
    power=(Button)findViewById(R.id.power);
    exp=(Button)findViewById(R.id.exp);
    log=(Button)findViewById(R.id.log);
    pie=(Button)findViewById(R.id.pie);


    b1.setOnClickListener(this);
    b2.setOnClickListener(this);
    b3.setOnClickListener(this);
    b4.setOnClickListener(this);
    b5.setOnClickListener(this);
    b6.setOnClickListener(this);
    b7.setOnClickListener(this);
    b8.setOnClickListener(this);
    b9.setOnClickListener(this);
    b0.setOnClickListener(this);
    minus.setOnClickListener(this);
    plus.setOnClickListener(this);
    dot.setOnClickListener(this);
    multi.setOnClickListener(this);
    divide.setOnClickListener(this);
    equals.setOnClickListener(this);
    del.setOnClickListener(this);
    ac.setOnClickListener(this);
    sqrt.setOnClickListener(this);
    sin.setOnClickListener(this);
    cos.setOnClickListener(this);
    tan.setOnClickListener(this);
    plusminus.setOnClickListener(this);
    inverse.setOnClickListener(this);
    power.setOnClickListener(this);
    exp.setOnClickListener(this);
    log.setOnClickListener(this);
    pie.setOnClickListener(this);

   }
3

There are 3 best solutions below

1
On

Initialize your button before calling setOnClickListener()

myButton = (Button) findViewById(R.id.btn1);
myButton.setOnClickListener...   //add listener after initialization or else it will throw this exception
3
On

Make sure that the ID which you passed in findViewById is same as that of the one declared in xml file

0
On

Initialize your buttons after setContentView() method. Make sure you are implementing OnClickListner Method.

public class MyActivity extends Activity  implements View.OnClickListener {
    protected void onCreate(Bundle bundle) {
        super.onCreate(budle);

        setContentView(R.layout.content_layout_id);
        e1 = (EditText) findViewById(R.id.editText);
        e1.setCursorVisible(false);

        b1 = (Button) findViewById(R.id.one);
        b2 = (Button) findViewById(R.id.two);
        b3 = (Button) findViewById(R.id.three);
        b4 = (Button) findViewById(R.id.four);
        b5 = (Button) findViewById(R.id.five);
        b6 = (Button) findViewById(R.id.six);
        b7 = (Button) findViewById(R.id.seven);
        b8 = (Button) findViewById(R.id.eight);
        b9 = (Button) findViewById(R.id.nine);
        b0 = (Button) findViewById(R.id.zero);
        minus = (Button) findViewById(R.id.minus);
        plus = (Button) findViewById(R.id.plus);
        dot = (Button) findViewById(R.id.dot);
        multi = (Button) findViewById(R.id.multi);
        divide = (Button) findViewById(R.id.divide);
        equals = (Button) findViewById(R.id.equals);
        del = (Button) findViewById(R.id.del);
        ac = (Button) findViewById(R.id.ac);
        sqrt = (Button) findViewById(R.id.sqrt);
        sin = (Button) findViewById(R.id.sin);
        cos = (Button) findViewById(R.id.cos);
        tan = (Button) findViewById(R.id.tan);
        plusminus = (Button) findViewById(R.id.plusminus);
        inverse = (Button) findViewById(R.id.inverse);
        power = (Button) findViewById(R.id.power);
        exp = (Button) findViewById(R.id.exp);
        log = (Button) findViewById(R.id.log);
        pie = (Button) findViewById(R.id.pie);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
        b5.setOnClickListener(this);
        b6.setOnClickListener(this);
        b7.setOnClickListener(this);
        b8.setOnClickListener(this);
        b9.setOnClickListener(this);
        b0.setOnClickListener(this);
        minus.setOnClickListener(this);
        plus.setOnClickListener(this);
        dot.setOnClickListener(this);
        multi.setOnClickListener(this);
        divide.setOnClickListener(this);
        equals.setOnClickListener(this);
        del.setOnClickListener(this);
        ac.setOnClickListener(this);
        sqrt.setOnClickListener(this);
        sin.setOnClickListener(this);
        cos.setOnClickListener(this);
        tan.setOnClickListener(this);
        plusminus.setOnClickListener(this);
        inverse.setOnClickListener(this);
        power.setOnClickListener(this);
        exp.setOnClickListener(this);
        log.setOnClickListener(this);
        pie.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {

    }

    }