Reputation: 53
I am getting Null Pointer Exception in my code in on click method.Please suggest me how can i remove it.
package co.sds.iitr.bullsandcows;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener
{
EditText Num;
Button done;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Num = (EditText) findViewById(R.id.etNum);
done = (Button) findViewById(R.id.btDone);
done.setOnClickListener(this);
}
@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 void onClick(View v)
{
// TODO Auto-generated method stub
if(v.getId()== R.id.btDone)
{
String num = Num.getText().toString();
int n = Integer.getInteger(num);
Toast.makeText(getApplicationContext(),"Your number is saved",
Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(),GuessActivtiy.class);
i.putExtra("num", n);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Not Found",
Toast.LENGTH_LONG).show();
}
}
GussActivity is my another activity class in which i am trying to pass my values through intent. My log cat is looking like this.
03-02 02:01:22.080: D/gralloc_goldfish(901): Emulator without GPU emulation detected.
03-02 02:01:27.570: D/AndroidRuntime(901): Shutting down VM
03-02 02:01:27.570: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception (group=0xb3aaeba8)
03-02 02:01:27.650: E/AndroidRuntime(901): FATAL EXCEPTION: main
03-02 02:01:27.650: E/AndroidRuntime(901): Process: co.sds.iitr.bullsandcows, PID: 901
03-02 02:01:27.650: E/AndroidRuntime(901): java.lang.NullPointerException
03-02 02:01:27.650: E/AndroidRuntime(901): at co.sds.iitr.bullsandcows.MainActivity.onClick(MainActivity.java:45)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.view.View.performClick(View.java:4438)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.view.View$PerformClick.run(View.java:18422)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Handler.handleCallback(Handler.java:733)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Handler.dispatchMessage(Handler.java:95)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.os.Looper.loop(Looper.java:136)
03-02 02:01:27.650: E/AndroidRuntime(901): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 02:01:27.650: E/AndroidRuntime(901): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 02:01:27.650: E/AndroidRuntime(901): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 02:01:27.650: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 02:01:27.650: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 02:01:27.650: E/AndroidRuntime(901): at dalvik.system.NativeStart.main(Native Method)
Please help me to resolve this.
Upvotes: 0
Views: 144
Reputation: 1442
You just try like this inside the MainActivity....
done.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent (MainActivity.this , GuessActivtiy.class);
intent.putExtra("num", n);
startActivity(intent);
}
});
Upvotes: 0
Reputation: 4923
Replace getInteger
method with parseInt
, to cast the String to Integer use parseInt method
As per the java doc
getInteger(String nm)
Determines the integer value of the system property with the specified name.If there is no property with the specified name, if the specified name is empty or null, or if the property does not have the correct numeric format, then null
is returned.
Upvotes: 2