0

I am receiving a crash report but it doesn't make sense to me. Says text may not be null. It works on any device I test with but somehow it is crashing for a few people. Latest one reported using a Galaxy Indulge (SCH-R910). Here is the report

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:200)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
at java.lang.Thread.run(Thread.java:1096)
Caused by: java.lang.IllegalArgumentException: Text may not be null
at org.apache.http.entity.mime.content.StringBody.<init>(StringBody.java:94)
at org.apache.http.entity.mime.content.StringBody.<init>(StringBody.java:113)
at    com.pff.photosfromfriends.UploadPicture$uploadthephoto.doInBackground(UploadPicture.java:313)
at com.pff.photosfromfriends.UploadPicture$uploadthephoto.doInBackground(UploadPicture.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:185)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
... 4 more

And here is my code for the upload

class uploadthephoto extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(UploadPicture.this);
        pDialog.setMessage("Uploading Picture");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    @Override
    protected String doInBackground(String... args) {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(POST_COMMNT_URL);

        try {

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            File file = new File(theimagepath);
            cbFile = new FileBody(file, "image/jpeg");
            entity.addPart("username", new StringBody(username,
                    Charset.forName("UTF-8")));
            entity.addPart("name", new StringBody(name,
                    Charset.forName("UTF-8")));
            entity.addPart("album", new StringBody(spinselected, Charset.forName("UTF-8")));
            entity.addPart("picture", cbFile);
            post.setEntity(entity);


            HttpResponse response1 = client.execute(post);
            HttpEntity resEntity = response1.getEntity();
            String Response = EntityUtils.toString(resEntity);
            Log.d("Response", Response);

            json = new JSONObject(Response);
            success_number = json.getInt("success");

            if (success_number == 1) { // picture added!
                return json.getString("message");

            } else {
                Log.d("Login Failure!", json.getString(message));
                return json.getString("message");

            }

        } catch (IOException e) {
            Log.e("asdf", e.getMessage(), e);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(UploadPicture.this, file_url, Toast.LENGTH_LONG)
                    .show();
        }

    }
}

// Username is from SharedPreferences
// Name is from SharedPreferences
// Album is from a Spinner 
// Picture is chosen from their gallery

Any idea why it would throw this error on some devices? Thank you.

JeffK
  • 247
  • 1
  • 5
  • 18
  • entity.addPart("album", new StringBody(spinselected, Charset.forName("UTF-8"))); – JeffK Nov 26 '13 at 02:08
  • It comes from @Override public void onItemSelected(AdapterView> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub spinselected = (String) upSpinner.getItemAtPosition(position); Log.d("spin selected", spinselected); } – JeffK Nov 26 '13 at 02:08
  • Is `spinselected` a static variable? – Lawrence Choy Nov 26 '13 at 02:11
  • I have it set as a private string but not as a static one – JeffK Nov 26 '13 at 02:49

1 Answers1

1

It's this line:

entity.addPart("album", new StringBody(spinselected, Charset.forName("UTF-8")));

Apparently spinselected is null and StringBody throws an error if it is constructed with a null parameter.

Caused by: java.lang.IllegalArgumentException: Text may not be null
at org.apache.http.entity.mime.content.StringBody.<init>(StringBody.java:94) 
James McCracken
  • 15,488
  • 5
  • 54
  • 62
  • Any clues why it could be okay with some devices and null with others? The spinner is populated from a database during the oncreate – JeffK Nov 26 '13 at 04:04
  • That I'm not too sure about. It's a bit of a bandaid at this point, but I would at least add a null check to make sure you don't crash. – James McCracken Nov 26 '13 at 04:08