java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“char[] java.lang.String.toCharArray()”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35933333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.NullPointerException: Attempt to invoke virtual method 'char[] java.lang.String.toCharArray()' on a null object reference
提问by IuKing Saw
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.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!
希望能解决我的问题,因为我已经在这个问题上坚持了好几天。谢谢!
回答by Silvans Solanki
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.
希望这会帮助你。
回答by Will Shao - MSFT
@luKing Saw, I tested your code in my simulator in Eclipse. Everything worked fine.
@luKing Saw,我在 Eclipse 的模拟器中测试了您的代码。一切正常。
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.
Could you please try to use the image in SD card or src/res folder?
我看到了你的图像路径'Content://com.android.providers.media.documents/document/image%3A36'
,但我不确定你的代码可以访问你项目中名为“document”的文件夹。你可以尝试使用 SD 卡或 src/res 文件夹中的图像吗?