0

LOG FILE :

08-18 20:27:09.762: E/AndroidRuntime(11726): FATAL EXCEPTION: AsyncTask #2
08-18 20:27:09.762: E/AndroidRuntime(11726): Process: com.example.pr, PID: 11726
08-18 20:27:09.762: E/AndroidRuntime(11726): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 20:27:09.762: E/AndroidRuntime(11726):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.lang.Thread.run(Thread.java:841)
08-18 20:27:09.762: E/AndroidRuntime(11726): Caused by: java.lang.NullPointerException
08-18 20:27:09.762: E/AndroidRuntime(11726):    at com.example.pr.MainActivity$UploadImage.doInBackground(MainActivity.java:137)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at com.example.pr.MainActivity$UploadImage.doInBackground(MainActivity.java:1)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-18 20:27:09.762: E/AndroidRuntime(11726):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-18 20:27:09.762: E/AndroidRuntime(11726):    ... 4 more

When I'm trying to hit the Upload button the app crashes. Dont know whats happening.Below is my MainAcyivity.java

There is also an error in the php file that shows the warning of file_put_contents. Is that related to this?

In the onClick(VIew v) method I've created a constructor of UploadImage as -- new UploadImage(image,text1.getText().toString()).execute()

public class MainActivity extends Activity implements View.OnClickListener{

    private static final int RESULT_IMAGE = 1;
private static final String SERVER_ADDRESS = "http://swapnil1.host22.com/";
ImageView image1,image2;
EditText text1,text2;
Button b1,b2;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        image1 = (ImageView)findViewById(R.id.image1);
        image2 = (ImageView)findViewById(R.id.image2);
        text1 = (EditText)findViewById(R.id.text1);
        text2 = (EditText)findViewById(R.id.text2);
        b1 = (Button)findViewById(R.id.button1);
        b2 = (Button)findViewById(R.id.button2);

        image1.setOnClickListener(this);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
        case R.id.image1:

            Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i,RESULT_IMAGE);

            Toast.makeText(getApplicationContext(),"Selecting Image",Toast.LENGTH_LONG).show();
            break;

        case R.id.button1:
            Bitmap image = ((BitmapDrawable)image1.getDrawable()).getBitmap();

            new UploadImage(image,text1.getText().toString());

            UploadImage UI = new UploadImage();
            UI.execute();

            break;

        case R.id.button2:
            break;




        }

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_IMAGE && resultCode == RESULT_OK && data != null)
        {
            Uri selectedImage = data.getData();
            image1.setImageURI(selectedImage);

            Toast.makeText(getApplicationContext(),"Image Selected",Toast.LENGTH_LONG).show();
        }



    }
    private class  UploadImage extends AsyncTask<Void, Void, Void>
    {

        Bitmap image;
        String name;

        public UploadImage(Bitmap image, String name)
        {

            this.image = image;
            this.name = name;

        }

        public UploadImage() {
            // TODO Auto-generated constructor stub
        }

        @Override
            protected void onPreExecute() {
            super.onPreExecute();


        }

        @Override
            protected  Void doInBackground(Void... params)
        {

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
            String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);

            ArrayList<NameValuePair> dataToSend = new ArrayList<NameValuePair>();

            dataToSend.add(new BasicNameValuePair("name",name));
            dataToSend.add(new BasicNameValuePair("image",encodedImage));


            HttpParams httpRequestParams = getHttpRequestParams();
            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS + "upload.php");



            try
            {
                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                client.execute(post);


            }
            catch (ClientProtocolException e) {
                e.printStackTrace();
            }
            catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

    @Override
    protected void onProgressUpdate(Void... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Void result) {

    super.onPostExecute(result);
    }


}


private HttpParams  getHttpRequestParams()
{


    HttpParams httpRequestParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpRequestParams,1000 * 30);
    HttpConnectionParams.setSoTimeout(httpRequestParams,1000 * 30);
    return httpRequestParams;
}

}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
swapnil saraf
  • 73
  • 1
  • 1
  • 9
  • 1
    In the logcat you can see the error is **NullPointerException**...please check the **line no 137 inside doInBackground** – Shadow Droid Aug 18 '15 at 15:12
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Aug 18 '15 at 15:51

2 Answers2

0

You are using constructor without arguments:

UploadImage UI=new UploadImage();

Which doesn't set the

Bitmap image; 

field.

That's why you get NullPointerException on line:

image.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
user2203031
  • 402
  • 3
  • 14
0

Please change the

new UploadImage(image,text1.getText().toString());

 UploadImage UI=new UploadImage();
 UI.execute();

to

       UploadImage UI=new UploadImage(image,text1.getText().toString());
       UI.execute();
Shadow Droid
  • 1,696
  • 1
  • 12
  • 26