来自图库的上传图片上的空指针异常

时间:2016-02-11 12:08:11

标签: android

我在从运行时抛出的gallery.String.trim()null对象引用上传图像时收到空指针异常。

下面我指出了错误行。

logcat的:

02-11 07:00:20.932: E/selectedImageUri(24696) : content://com.android.providers.media.documents/document/image%3A388
02-11 07:00:20.935: E/selectedPath1(24696) : null
02-11 07:00:23.840: E/selectedImageUri(24696) : content://com.android.providers.media.documents/document/image%3A388
02-11 07:00:23.845: E/selectedPath2(24696) : null

02-11 07:00:26.004: E/AndroidRuntime(24696): FATAL EXCEPTION: main
02-11 07:00:26.004: E/AndroidRuntime(24696): Process: com.steve.multipleuploadimage, PID: 24696
02-11 07:00:26.004: E/AndroidRuntime(24696): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference
02-11 07:00:26.004: E/AndroidRuntime(24696):    at com.steve.multipleuploadimage.MainActivity$3.onClick(MainActivity.java:80)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.view.View.performClick(View.java:4756)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.view.View$PerformClick.run(View.java:19749)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.os.Handler.handleCallback(Handler.java:739)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.os.Looper.loop(Looper.java:135)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at android.app.ActivityThread.main(ActivityThread.java:5221)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at java.lang.reflect.Method.invoke(Native Method)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at java.lang.reflect.Method.invoke(Method.java:372)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-11 07:00:26.004: E/AndroidRuntime(24696):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

MainActivity.java:

 b1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    openGallery(SELECT_FILE1);
                }
            });
            b2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    openGallery(SELECT_FILE2);
                }
            });
            b3.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!(selectedPath1.trim().equalsIgnoreCase("NONE")) && !(selectedPath2.trim().equalsIgnoreCase("NONE"))){    ----> 80th line
                        progressDialog = ProgressDialog.show(MainActivity.this, "", "Uploading files to server.....", false);
                         Thread thread=new Thread(new Runnable(){
                                public void run(){
                                    doFileUpload();
                                    runOnUiThread(new Runnable(){
                                        public void run() {
                                            if(progressDialog.isShowing())
                                                progressDialog.dismiss();
                                        }
                                    });
                                }
                        });
                        thread.start();
                    }else{
                                Toast.makeText(getApplicationContext(),"Please select two files to upload.", Toast.LENGTH_SHORT).show();
                    }
                }
            });

        }

        public void openGallery(int req_code){

            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
       }

       public void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (resultCode == RESULT_OK) {
                Uri selectedImageUri = data.getData();

                Log.e("selectedImageUri",""+selectedImageUri);

                if (requestCode == SELECT_FILE1)
                {
                    selectedPath1 = getPath(selectedImageUri);

                    Log.e("selectedPath1", ""+selectedPath1);
                }
                if (requestCode == SELECT_FILE2)
                {
                    selectedPath2 = getPath(selectedImageUri);

                    Log.e("selectedPath2", ""+selectedPath2);

                }
                tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
            }
        }

        public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

        private void doFileUpload(){

            File file1 = new File(selectedPath1);
            File file2 = new File(selectedPath2);
            String urlString = "http://10.0.2.2/upload_test/upload_media_test.php";
            try
            {
                 HttpClient client = new DefaultHttpClient();
                 HttpPost post = new HttpPost(urlString);
                 FileBody bin1 = new FileBody(file1);
                 FileBody bin2 = new FileBody(file2);
                 MultipartEntity reqEntity = new MultipartEntity();
                 reqEntity.addPart("uploadedfile1", bin1);
                 reqEntity.addPart("uploadedfile2", bin2);
                 reqEntity.addPart("user", new StringBody("User"));
                 post.setEntity(reqEntity);
                 HttpResponse response = client.execute(post);
                 resEntity = response.getEntity();
                 final String response_str = EntityUtils.toString(resEntity);
                 if (resEntity != null) {
                     Log.i("RESPONSE",response_str);
                     runOnUiThread(new Runnable(){
                            public void run() {
                                 try {
                                    res.setTextColor(Color.GREEN);
                                    res.setText("n Response from server : n " + response_str);
                                    Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                               }
                        });
                 }
            }
            catch (Exception ex){
                 Log.e("Debug", "error: " + ex.getMessage(), ex);
            }
          }

任何人都可以帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:0)

你的字符串selectedPath2似乎是空的。

请检查:

if(selectedPath2!= null && !selectedPath2.isEmpty())
{
if(!(selectedPath1.trim().equalsIgnoreCase("NONE")) && !(selectedPath2.trim().equalsIgnoreCase("NONE"))){    ----> 80th line
  ............................               .
}