SAPHIRE
SAPHIRE

Reputation: 103

Error android- java Null Pointer Exception

I am getting this error when i am inflating another activity on button click of first activity.I am also Passing the data between the activities.when the data is not being passed then it works fine and am able to view the second activity.the data i am passing is the content of text box from the first activity of integer type.Kindly lemme know where am i going wrong.

code for Activity1.class

public void onClick(View v)

    {

        try
        {
    Log.i("MyActivity", "Entered OnClick()");
    //Toast.makeText(getBaseContext(), "Entered", Toast.LENGTH_SHORT).show();
    int value;
    value=Integer.parseInt(range.getText().toString());
    Bundle bundle= new Bundle();
    bundle.putInt("param1",value);
//  Toast.makeText(getBaseContext(), "Wait", Toast.LENGTH_SHORT).show();
    Intent myIntent = new Intent(v.getContext(), RoutePath.class);
    myIntent.putExtras(bundle);
     startActivity(myIntent);
        }
         catch(Exception e)
            {e.printStackTrace();}
    }

and Activity2.class

public class RoutePath extends MapActivity {

GeoPoint gp1;

GeoPoint gp2;

GeoPoint srcGeoPoint;

GeoPoint destGeoPoint;

double distance;

int value=0;

public void onCreate(Bundle savedInstanceState)

{   



    super.onCreate(savedInstanceState);

    Bundle bundle = getIntent().getExtras();

    value = bundle.getInt("param1",0);

    setContentView(R.layout.main);

    MapView mapView = (MapView) findViewById(R.id.myMapView1);

    mapView.setBuiltInZoomControls(true);

//and then the other code.......

}

and the stack trace

03-21 15:44:39.276: WARN/System.err(3337): java.lang.NullPointerException
03-21 15:44:39.286: WARN/System.err(3337):     at com.nautilus.RoutePath.Welcome.onClick(Welcome.java:50)
03-21 15:44:39.306: WARN/System.err(3337):     at android.view.View.performClick(View.java:2364)
03-21 15:44:39.331: WARN/System.err(3337):     at android.view.View.onTouchEvent(View.java:4179)
03-21 15:44:39.335: WARN/System.err(3337):     at android.widget.TextView.onTouchEvent(TextView.java:6540)
03-21 15:44:39.348: WARN/System.err(3337):     at android.view.View.dispatchTouchEvent(View.java:3709)
03-21 15:44:39.355: WARN/System.err(3337):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-21 15:44:39.355: WARN/System.err(3337):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-21 15:44:39.385: WARN/System.err(3337):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-21 15:44:39.385: WARN/System.err(3337):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
03-21 15:44:39.385: WARN/System.err(3337):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
03-21 15:44:39.385: WARN/System.err(3337):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
03-21 15:44:39.385: WARN/System.err(3337):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
03-21 15:44:39.385: WARN/System.err(3337):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
03-21 15:44:39.385: WARN/System.err(3337):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
03-21 15:44:39.385: WARN/System.err(3337):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 15:44:39.385: WARN/System.err(3337):     at android.os.Looper.loop(Looper.java:123)
03-21 15:44:39.385: WARN/System.err(3337):     at android.app.ActivityThread.main(ActivityThread.java:4363)
03-21 15:44:39.385: WARN/System.err(3337):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 15:44:39.396: WARN/System.err(3337):     at java.lang.reflect.Method.invoke(Method.java:521)

Upvotes: 0

Views: 1665

Answers (3)

tbruyelle
tbruyelle

Reputation: 13045

Some advices :

  • Read and provide a stack trace inside your question, it will be very helpfull.
  • Use this instead of v.getContext() in your Intent constructor
  • Integer.parseInt() will throw NumberFormatException if your variable range (which is an EditText I assume) returns a empty string, so maybe you should test the result of range.getText().toString().

EDIT :

  • Do you affect a value to the range variable ? Something like
    range = (EditText) findViewById(R.id.range)

Upvotes: 1

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28573

Instead of

int value;
value=Integer.parseInt(range.getText().toString());
Bundle bundle= new Bundle();
bundle.putInt("param1",value);
Intent myIntent = new Intent(v.getContext(), RoutePath.class);
myIntent.putExtras(bundle);

Try

int value;
value=Integer.parseInt(range.getText().toString());
Intent myIntent = new Intent(this, RoutePath.class);
myIntent.putExtra("param1",value);

Upvotes: 0

ernazm
ernazm

Reputation: 9268

Maybe there's no view with id R.id.myMapView1 on R.layout.main layout. But what about providing a stack trace?

Upvotes: 0

Related Questions