0

I'm getting a java.lang.RuntimeException Error in logcat for my app. It then goes on to show the error is coming from:

protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        gps = new GPSTracker(EditProductActivity.this);
        double inputlat = gps.getLatitude();
        double inputlong = gps.getLongitude();

        String inputLat=Double.toString(inputlat);
        String inputLong=Double.toString(inputlong);

        // getting updated data from EditTexts
        String name = txtName.getText().toString();
        String longitude = inputLong;
        String latitude = inputLat;

        Log.d("Get this far?", pid);//something wrong with these when I want to save
        String pavement = spinpavement.getSelectedItem().toString();
        String traffic = spintraffic.getSelectedItem().toString();
        String environment = spinenviro.getSelectedItem().toString();
        //Oh no something is wrong here
        // Building Parameters

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_PID, pid));
        params.add(new BasicNameValuePair(TAG_NAME, name));
        params.add(new BasicNameValuePair(TAG_LONG, longitude));
        params.add(new BasicNameValuePair(TAG_LAT, latitude));
        params.add(new BasicNameValuePair(TAG_PAVEMENT, pavement));
        params.add(new BasicNameValuePair(TAG_TRAFFIC, traffic));
        params.add(new BasicNameValuePair(TAG_ENVIRONMENT, environment));

        // sending modified data through http request
        // Notice that update product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_update_product,
                "POST", params);

        // check json success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully updated
                //Intent i = getIntent();
                // send result code 100 to notify about product update
                //setResult(100, i);
                finish();
            } else {
                // failed to update product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

As you can see the error comes at:

String pavement = spinpavement.getSelectedItem().toString();
        String traffic = spintraffic.getSelectedItem().toString();
        String environment = spinenviro.getSelectedItem().toString();

But I can not figure out what is wrong with it.

Here is the whole logcat from where the error starts

01-16 19:20:29.446: E/AndroidRuntime(24403): FATAL EXCEPTION: AsyncTask #2
01-16 19:20:29.446: E/AndroidRuntime(24403): Process: com.example.androidhive, PID: 24403
01-16 19:20:29.446: E/AndroidRuntime(24403): java.lang.RuntimeException: An error occured while executing doInBackground()
01-16 19:20:29.446: E/AndroidRuntime(24403):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.lang.Thread.run(Thread.java:841)
01-16 19:20:29.446: E/AndroidRuntime(24403): Caused by: java.lang.NullPointerException
01-16 19:20:29.446: E/AndroidRuntime(24403):    at com.example.androidhive.EditProductActivity$SaveProductDetails.doInBackground(EditProductActivity.java:225)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at com.example.androidhive.EditProductActivity$SaveProductDetails.doInBackground(EditProductActivity.java:1)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-16 19:20:29.446: E/AndroidRuntime(24403):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-16 19:20:29.446: E/AndroidRuntime(24403):    ... 3 more
01-16 19:20:30.019: E/WindowManager(24403): android.view.WindowLeaked: Activity com.example.androidhive.EditProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{421bbd78 V.E..... R.....ID 0,0-684,192} that was originally added here
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
01-16 19:20:30.019: E/WindowManager(24403):     at  android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
01-16 19:20:30.019: E/WindowManager(24403):     at android.app.Dialog.show(Dialog.java:286)
01-16 19:20:30.019: E/WindowManager(24403):     at com.example.androidhive.EditProductActivity$SaveProductDetails.onPreExecute(EditProductActivity.java:202)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.AsyncTask.execute(AsyncTask.java:535)
01-16 19:20:30.019: E/WindowManager(24403):     at com.example.androidhive.EditProductActivity.onClick(EditProductActivity.java:87)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invoke(Method.java:515)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View$1.onClick(View.java:3837)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View.performClick(View.java:4452)
01-16 19:20:30.019: E/WindowManager(24403):     at android.view.View$PerformClick.run(View.java:18498)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Handler.handleCallback(Handler.java:733)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-16 19:20:30.019: E/WindowManager(24403):     at android.os.Looper.loop(Looper.java:137)
01-16 19:20:30.019: E/WindowManager(24403):     at android.app.ActivityThread.main(ActivityThread.java:5083)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 19:20:30.019: E/WindowManager(24403):     at java.lang.reflect.Method.invoke(Method.java:515)
01-16 19:20:30.019: E/WindowManager(24403):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-16 19:20:30.019: E/WindowManager(24403):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-16 19:20:30.019: E/WindowManager(24403):     at dalvik.system.NativeStart.main(Native Method)
4
  • Exactly which line is 225? If its one of the lines that you posted then show how/where you initialized spinpavement and those other variables which I'm guessing are Spinners Commented Jan 17, 2014 at 0:47
  • Yup that is the line and yes they are Spinners Commented Jan 17, 2014 at 0:56
  • Ok, but where/how are you initializing them? Can you post that because they must be null. Either that or getSelectedItem() returns null Commented Jan 17, 2014 at 0:57
  • Yup that would be the problem haha thanks I guess I must have erased the initializing lines. Thanks again Commented Jan 17, 2014 at 1:01

2 Answers 2

1

If the first line corresponds to line 225, then the NullPointerException is maybe because there is nothing selected int the spinner. Try:

String pavement = (spinpavement!=null)?String.valueOf(spinpavement.getSelectedItem()):"NULL Pavement";
String traffic = (spintraffic!=null)?String.valueOf(spintraffic.getSelectedItem()):"NULL Traffic";
String environment = (spinenviro!=null)?String.valueOf(spinenviro.getSelectedItem()):"NULL Environment";

and you'll be able to realize if there's some issue or lost reference to any of your member variables.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

check it..before getting selected string..

if(spinner.getselectedpostion!=0)
{`
`
String selected_string= spinner.getSelectedItem().toString();

}`enter code here`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.