简体   繁体   English

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'char[] java.lang.String.toCharArray()'

[英]java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference

I am trying to read an image from the Android device then upload it to Microsoft Azure Mobile Service.我正在尝试从 Android 设备读取图像,然后将其上传到 Microsoft Azure 移动服务。

The pickImage() and onActivityResult() methods are working fine as image can be successfully display at the Image View by using the code pickImage() 和 onActivityResult() 方法工作正常,因为图像可以通过使用代码成功显示在图像视图中

mImageView.setImageURI(currImageURI);

When I am trying to save the image by using the saveToDo() method there is an error at the line当我尝试使用 saveToDo() 方法保存图像时,该行出现错误

FileInputStream fis = new FileInputStream(absoluteFilePath);

Therefore, I try to print out what exactly contains in the cursor by using the code因此,我尝试使用代码打印出光标中确切包含的内容

System.out.println("IMAGE: " + DatabaseUtils.dumpCursorToString(cursor));
System.out.println("CURRENT: " + DatabaseUtils.dumpCurrentRowToString(cursor));

I am getting the following error.我收到以下错误。

W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
W/System.err:     at java.io.File.fixSlashes(File.java:183)
W/System.err:     at java.io.File.<init>(File.java:130)
W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:103)

W/System.err:     at com.king.recipesharinghub.AddRecipe.saveTodo(AddRecipe.java:773)
W/System.err:     at com.king.recipesharinghub.AddRecipe$3.onClick(AddRecipe.java:214)

W/System.err:     at android.view.View.performClick(View.java:5198)
W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:148)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/TodoDetailsActivity: JSON: {"complete":"false","text":"This is testing"}

These are the system outputs where the data is null.这些是数据为空的系统输出。

I/System.out: Image URL: content://com.android.providers.media.documents/document/image%3A36
I/System.out: PROJECTION: [Ljava.lang.String;@290e1f6
I/System.out: DATABASE: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@6cd59f7
I/System.out: 0 {
I/System.out:    _data=null
I/System.out: }
I/System.out: <<<<<
I/System.out: CURRENT: 0 {
I/System.out:    _data=null
I/System.out: }
I/System.out: INDEX: 0
I/System.out: ABSOLUTEFILEPATH: null

Below are the full codes以下是完整代码

protected void pickImage() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, 987);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    try {
        //handle result from gallary select
        if (requestCode == 987) {
            Uri currImageURI = data.getData();
            this.mImageUrl = currImageURI;
            //Set the image view's image by using imageUri
            mImageView.setImageURI(currImageURI);
        }
    } catch (Exception ex) {
        Log.e("TodoDetailsActivity", "Error in onActivityResult: " + ex.getMessage());
    }
}

protected void saveTodo() {
    String imageString = null;
    if (mImageUrl != null && !mImageUrl.equals("")) {
        try {
            System.out.println("Image URL: " + mImageUrl.toString());

            String[] projection = { MediaStore.Images.Media.DATA };
            System.out.println("PROJECTION: " + projection);

            Cursor cursor = getContentResolver().query(mImageUrl, projection,
                    null, null, null);
            cursor.moveToFirst();

            System.out.println("DATABASE: " + DatabaseUtils.dumpCursorToString(cursor));
            System.out.println("CURRENT: " + DatabaseUtils.dumpCurrentRowToString(cursor));

            int index = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);

            System.out.println("INDEX: " + index);

            String absoluteFilePath = cursor.getString(index);

            System.out.println("ABSOLUTEFILEPATH: " + absoluteFilePath);

            FileInputStream fis = new FileInputStream(absoluteFilePath);

            int bytesRead = 0;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            while ((bytesRead = fis.read(b)) != -1) {
                bos.write(b, 0, bytesRead);
            }
            byte[] bytes = bos.toByteArray();
            imageString = Base64.encodeToString(bytes, Base64.DEFAULT);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    new SaveTodoTask(this).execute("This is testing", imageString);
}

Hope to get my problem solve as I had stuck at this issue for several days.希望能解决我的问题,因为我在这个问题上停留了好几天。 Thank you!谢谢!

you should add projection in your code while calling the query,您应该在调用查询时在代码中添加投影,

     // Attempt to fetch asset filename for image
            String[] projection = { MediaStore.Images.Media.DATA };
photoCursor = context.getContentResolver().query( photoUri, 


                                projection, null, null, null );

then only you will be able to get那么只有你能得到

int index = cursor
                    .getColumnIndex(MediaStore.Images.ImageColumns.DATA);

Hope this will help you.希望这会帮助你。

@luKing Saw, I tested your code in my simulator in Eclipse. @luKing Saw,我在 Eclipse 的模拟器中测试了您的代码。 Everything worked fine.一切正常。

03-15 21:17:47.706: I/dalvikvm-heap(621): Grow heap (frag case) to 11.867MB for 1228816-byte allocation
03-15 21:17:49.296: I/System.out(621): Image URL: content://media/external/images/media/17
03-15 21:17:49.296: I/System.out(621): PROJECTION: [Ljava.lang.String;@414b3988
03-15 21:17:49.336: I/System.out(621): DATABASE: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@414a9d50
03-15 21:17:49.336: I/System.out(621): 0 {
03-15 21:17:49.346: I/System.out(621):    _data=/mnt/sdcard/DCIM/Camera/IMG_20160315_171536.jpg
03-15 21:17:49.346: I/System.out(621): }
03-15 21:17:49.346: I/System.out(621): <<<<<
03-15 21:17:49.346: I/System.out(621): CURRENT: 0 {
03-15 21:17:49.346: I/System.out(621):    _data=/mnt/sdcard/DCIM/Camera/IMG_20160315_171536.jpg
03-15 21:17:49.356: I/System.out(621): }
03-15 21:17:49.356: I/System.out(621): INDEX: 0
03-15 21:17:49.356: I/System.out(621): ABSOLUTEFILEPATH: /mnt/sdcard/DCIM/Camera/IMG_20160315_171536.jpg
03-15 21:17:51.466: I/Choreographer(621): Skipped 562 frames!  The application may be doing too much work on its main thread.

I saw your Image Path like 'Content://com.android.providers.media.documents/document/image%3A36' ,but I am not sure your code can access into folder named 'document' in your project.我看到您的图像路径类似于'Content://com.android.providers.media.documents/document/image%3A36' ,但我不确定您的代码是否可以访问项目中名为 'document' 的文件夹。 Could you please try to use the image in SD card or src/res folder?您能否尝试使用 SD 卡或 src/res 文件夹中的图像?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 引起:java.lang.NullPointerException: Attempt to invoke virtual method &#39;boolean java.lang.String.isEmpty()&#39; on a null object reference - Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法,而它不为空 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference, while its not null java.lang.NullPointerException:尝试在 oncreate 方法中对空对象引用调用虚拟方法 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference in oncreate method java.lang.NullPointerException:尝试对空对象引用调用虚拟方法。 应用程序在运行时崩溃 - java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference. App crashes on run Android:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String java.lang.Object.toString()” - Android: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法… - java.lang.NullPointerException: Attempt to invoke virtual method … on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“RecyclerView$ViewHolder.shouldIgnore()” - java.lang.NullPointerException: Attempt to invoke virtual method 'RecyclerView$ViewHolder.shouldIgnore()' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法&#39;ActionBar.setNavigationMode(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'ActionBar.setNavigationMode(int)' on a null object reference 致命异常:java.lang.NullPointerException:尝试在空对象引用onClickItem上调用虚方法void - FATAL EXCEPTION: java.lang.NullPointerException: Attempt to invoke virtual method void on a null object reference onClickItem Android Studio:java.lang.NullPointerException:尝试在空对象引用[TextView]上调用虚拟方法 - Android Studio: java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference [TextView]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM