Kishan Shah Kishan Shah - 4 months ago 26
Android Question

Android null pointer exception on view.getId()

This code gives a null pointer exception in the onClick() method. I checked other posts but didn't get proper solutions. Please help. This is my first android project and so I can't solve it. Where is the error in this? This seems to be the way I have seen in other solutions

public class MainActivity extends Activity implements OnClickListener{

Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnAdd,btnSub,btnDiv,btnMul,btnDot,btnSign,btnAns,btnCE,btnC,btnDel;
EditText et;
char last;
double num,num1,num2;
boolean signFlag,numFlag;

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

//Initializing variables
btn0=(Button) findViewById(R.id.btn0);
btn1=(Button) findViewById(R.id.btn1);
btn2=(Button) findViewById(R.id.btn2);
btn3=(Button) findViewById(R.id.btn3);
btn4=(Button) findViewById(R.id.btn4);
btn5=(Button) findViewById(R.id.btn5);
btn6=(Button) findViewById(R.id.btn6);
btn7=(Button) findViewById(R.id.btn7);
btn8=(Button) findViewById(R.id.btn8);
btn9=(Button) findViewById(R.id.btn9);
btnAdd=(Button) findViewById(R.id.btnAdd);
btnSub=(Button) findViewById(R.id.btnSub);
btnMul=(Button) findViewById(R.id.btnMul);
btnDiv=(Button) findViewById(R.id.btnDiv);
btnDot=(Button) findViewById(R.id.btnDot);
btnSign=(Button) findViewById(R.id.btnSign);
btnAns=(Button) findViewById(R.id.btnAns);
btnCE=(Button) findViewById(R.id.btnCE);
btnC=(Button) findViewById(R.id.btnC);
btnDel=(Button) findViewById(R.id.btnDel);
num1=0; num2=0; num=0;
signFlag=false; numFlag=false;
//Initializing variables ends here

//Setting onClickListner
btn0.setOnClickListener(this);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnDiv.setOnClickListener(this);
btnSub.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnSign.setOnClickListener(this);
btnAns.setOnClickListener(this);
btnDot.setOnClickListener(this);
btnCE.setOnClickListener(this);
btnC.setOnClickListener(this);
btnDel.setOnClickListener(this);
//Setting onClickListner Ends here
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

int id =v.getId(); //NullPointerException somewhere around here
switch (id) {
case R.id.btn0:
printNum('0');
break;
case R.id.btn1:
printNum('1');
break;
case R.id.btn2:
printNum('2');
break;
case R.id.btn3:
printNum('3');
break;
case R.id.btn4:
printNum('4');
break;
case R.id.btn5:
printNum('5');
break;
case R.id.btn6:
printNum('6');
break;
case R.id.btn7:
printNum('7');
break;
case R.id.btn8:
printNum('8');
break;
case R.id.btn9:
printNum('9');
break;
case R.id.btnAdd:
num=Integer.parseInt(et.getText().toString())+num;
et.setText("");
signFlag=true;
last='+';
break;
case R.id.btnSub:
num=Integer.parseInt(et.getText().toString())-num;
et.setText("");
signFlag=true;
last='-';
break;
case R.id.btnMul:
if(signFlag==false)
num=Integer.parseInt(et.getText().toString());
else
num=Integer.parseInt(et.getText().toString())*num;
et.setText("");
signFlag=true;
last='*';
break;
case R.id.btnDiv:
if(signFlag==false)
num=Integer.parseInt(et.getText().toString());
else
num=Integer.parseInt(et.getText().toString())/num;
et.setText("");
signFlag=true;
last='/';
break;
case R.id.btnDot:
et.setText(et.getText().toString()+".");
break;
case R.id.btnSign:
double temp=Integer.parseInt(et.getText().toString());
temp=temp*(-1);
et.setText(temp+"");
break;
case R.id.btnAns:

break;
default:
break;
}
}

public void printNum(char c) {
String temp="";
if(numFlag){
temp=et.getText().toString();
}
temp+=c;
Dialog dialog = new Dialog(this);
dialog.show();

et.setText(temp);
}

}


The layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.calculator.MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<EditText
android:id="@+id/ET"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnCE"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="CE" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnC"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="C" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnDel"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="DEL" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnDiv"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="/" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn7"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn8"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn9"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnMul"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="*" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn4"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn5"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn6"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnSub"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="-" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnAdd"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnSign"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+-" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btn0"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="0" />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnDot"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="." />

<Button style="?android:attr/buttonBarButtonStyle"
android:id="@+id/btnAns"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="=" />

</LinearLayout>

</LinearLayout>


The logcat:

07-18 08:12:03.890: D/gralloc_goldfish(962): Emulator without GPU emulation detected.
07-18 08:12:06.580: D/AndroidRuntime(962): Shutting down VM
07-18 08:12:06.580: W/dalvikvm(962): threadid=1: thread exiting with uncaught exception (group=0xb4ab0ba8)
07-18 08:12:06.580: E/AndroidRuntime(962): FATAL EXCEPTION: main
07-18 08:12:06.580: E/AndroidRuntime(962): Process: com.example.calculator, PID: 962
07-18 08:12:06.580: E/AndroidRuntime(962): java.lang.NullPointerException
07-18 08:12:06.580: E/AndroidRuntime(962): at com.example.calculator.MainActivity.printNum(MainActivity.java:187)
07-18 08:12:06.580: E/AndroidRuntime(962): at com.example.calculator.MainActivity.onClick(MainActivity.java:103)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.view.View.performClick(View.java:4438)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.view.View$PerformClick.run(View.java:18422)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.os.Handler.handleCallback(Handler.java:733)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.os.Looper.loop(Looper.java:136)
07-18 08:12:06.580: E/AndroidRuntime(962): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-18 08:12:06.580: E/AndroidRuntime(962): at java.lang.reflect.Method.invokeNative(Native Method)
07-18 08:12:06.580: E/AndroidRuntime(962): at java.lang.reflect.Method.invoke(Method.java:515)
07-18 08:12:06.580: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-18 08:12:06.580: E/AndroidRuntime(962): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-18 08:12:06.580: E/AndroidRuntime(962): at dalvik.system.NativeStart.main(Native Method)
07-18 08:12:09.940: I/Process(962): Sending signal. PID: 962 SIG: 9

Answer

You might be missing these two:

Instead of switch(id) use this

 public void clicker(View v) {
      switch (v.getId()) {
          case R.id.btnmhome:
               cf.gohome();
               break;
            }
        }

And write

android:onClick="clicker"

it means your method name where you are writing onclick funtion

Comments