hi all i try to do calculator,i'm write the following code and when i try to run it i have a null pointer exception i don't know what is the error ,i found that my code is good and no error the function goes in force close before the layout appear and every thing inside onclickerlistener so at least i must goes in force close when i clicked on any button if there is something wrong please help me this is my code:
package com.example.calculato;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText edt;
double num1=0,num2=0,result=0;
double fact=1;
String a="";
char op;
boolean b=false;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("inside on create ", "okzzzzzzzzzzzzzzzzz");
edt=(EditText)findViewById(R.id.editText1);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
b6=(Button)findViewById(R.id.button6);
b7=(Button)findViewById(R.id.button7);
b8=(Button)findViewById(R.id.button8);
b9=(Button)findViewById(R.id.button9);
b10=(Button)findViewById(R.id.button10);
b11=(Button)findViewById(R.id.button11);
b12=(Button)findViewById(R.id.button12);
b13=(Button)findViewById(R.id.button13);
b14=(Button)findViewById(R.id.button14);
b15=(Button)findViewById(R.id.button15);
b16=(Button)findViewById(R.id.button16);
Log.d("after def ", "okzzzzzzzzzzzzzzzzz");
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "9");
}
});
b10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
a=edt.getText().toString();
edt.setText(a+ "0");
}
});
b13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num1=Double.valueOf(edt.getText().toString());
op='+';
edt.setText("");
}
});
b14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num1=Double.valueOf(edt.getText().toString());
op='-';
edt.setText("");
}
});
b15.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num1=Double.valueOf(edt.getText().toString());
op='*';
edt.setText("");
}
});
b16.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num1=Double.valueOf(edt.getText().toString());
op='/';
edt.setText("");
}
});
b11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edt.setText(" ");
}
});
b12.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
String Value = edt.getText().toString();
if(Value.length()==1)
edt.setText("");
else
{
Value = Value.substring(0,Value.length()-1);
edt.setText(Value);
}
} catch (Exception e) {
}
}
});
b17.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
num2=Double.valueOf(edt.getText().toString());
if(op=='+')
{
result=num1+num2;
edt.setText(String.valueOf(result));
}
if(op=='-')
{
result=num1-num2;
edt.setText(String.valueOf(result));
}
if(op=='*')
{
result=num1*num2;
edt.setText(String.valueOf(result));
}
if(op=='/')
{
if(num2!=0)
{
result=num1/num2;
edt.setText(String.valueOf(result));
}
else
edt.setText("Math Error");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
And this the logcat :
10-28 14:16:22.488: E/AndroidRuntime(1049): FATAL EXCEPTION: main
10-28 14:16:22.488: E/AndroidRuntime(1049): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculato/com.example.calculato.MainActivity}: java.lang.NullPointerException
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.os.Looper.loop(Looper.java:137)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread.main(ActivityThread.java:5039)
10-28 14:16:22.488: E/AndroidRuntime(1049): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 14:16:22.488: E/AndroidRuntime(1049): at java.lang.reflect.Method.invoke(Method.java:511)
10-28 14:16:22.488: E/AndroidRuntime(1049): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-28 14:16:22.488: E/AndroidRuntime(1049): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-28 14:16:22.488: E/AndroidRuntime(1049): at dalvik.system.NativeStart.main(Native Method)
10-28 14:16:22.488: E/AndroidRuntime(1049): Caused by: java.lang.NullPointerException
10-28 14:16:22.488: E/AndroidRuntime(1049): at com.example.calculato.MainActivity.onCreate(MainActivity.java:254)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.Activity.performCreate(Activity.java:5104)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-28 14:16:22.488: E/AndroidRuntime(1049): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)