我在这里遗漏了什么吗?我的代码正在根据此代码踢出NullPointerException以上传到Dropbox。
File file = new File(mFileMag.getCurrentDir() +"/"+ mSelectedListItem);
System.out.println(file);
inputStream = new FileInputStream(file);
System.out.println(inputStream);
Entry newEntry = mDBApi.putFile("/" + mSelectedListItem, inputStream,
file.length(), null, null);
System.out.println(newEntry);
system.out命令的结果看起来不错。 inputStream变量输出“java.io.FileInputStream@44ecd668”,我有点困惑,但文件变量的systemout显示正确的路径和文件名。 nullPointerException似乎来自newEntry变量的设置,这对我来说毫无意义。 Eclipse将代码显示为语法正确。这个垃圾箱废话让我感到害怕。
@BenHolland - 如果我将路径硬编码如下 -
case D_MENU_SEND:
// Uploading content.
FileInputStream inputStream = null;
try {
//File file = new File(mFileMag.getCurrentDir() +"/"+ mSelectedListItem);
File file = new File("/sdcard0/DCIM/100ANDRO/deploy.json");
System.out.println(file);
inputStream = new FileInputStream(file);
System.out.println(inputStream);
Entry newEntry = mDBApi.putFile("/Dropbox/", inputStream,
file.length(), null, null);
System.out.println(newEntry);
Log.i("DbExampleLog", "The uploaded file's rev is: " + newEntry.rev);
} catch (DropboxUnlinkedException e) {
// User has unlinked, ask them to link again here.
Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
} catch (FileNotFoundException e) {
Log.e("DbExampleLog", "File not found.");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
这只会在logcat中生成“找不到文件”,当然这是来自catch(FileNotFoundException)。但是我没有收到NullPointerException。如果您看到代码的顶部 - case语句 - 在上下文菜单单击后运行此代码。
_____________________________________更新后的代码__________________________________________
// Uploading content.
FileInputStream inputStream = null;
File upfile = new File(mFileMag.getCurrentDir() + "/" + mSelectedListItem);
try {
inputStream = new FileInputStream(upfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(upfile);
System.out.println(inputStream);
try {
Entry upEntry = mDBApi.putFile("/test.rar", inputStream,upfile.length(), null, null);
System.out.println(upEntry);
} catch (DropboxException e) {
e.printStackTrace();
}
所以我已将代码更新为此。我是否对路径进行硬编码似乎无关紧要。即使我这样做,Entry upEntry行也会导致NullPointerException。我不知所措。我什么都看不到会造成这种情况。
________________________________来自nullPointerException的堆栈跟踪_______________________
02-14 13:28:07.370: W/dalvikvm(311): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-14 13:28:07.380: E/AndroidRuntime(311): FATAL EXCEPTION: main
02-14 13:28:07.380: E/AndroidRuntime(311): java.lang.NullPointerException
02-14 13:28:07.380: E/AndroidRuntime(311): at com.stavox.zircon.Main.onContextItemSelected(Main.java:722)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.app.Activity.onMenuItemSelected(Activity.java:2199)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.policy.impl.PhoneWindow$ContextMenuCallback.onMenuItemSelected(PhoneWindow.java:2744)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:137)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:874)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.widget.ListView.performItemClick(ListView.java:3382)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.os.Handler.handleCallback(Handler.java:587)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123)
02-14 13:28:07.380: E/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 13:28:07.380: E/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 13:28:07.380: E/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 13:28:07.380: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 13:28:07.380: E/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method)
单击“发送到Dropbox”时会出现堆栈跟踪。我想如果我打破整个过程,可能会让人更容易提供帮助。
上下文菜单包含“发送至Dropbox”项。单击时,它指向包含我们在此处查看的代码的case语句。那是什么产生NullPointerException。如您所见,第722行是违规行。
是 Entry upEntry = mDBApi.putFile("/test.rar", inputStream,upfile.length(), null, null);
_____________________________________________解_______________________________________
所以这就是我最终要解决的问题:
以前我在两个文件之间工作。我在Main.java中有Dropbox authcode,在Dropboxfileuploadactivity.java中有实际的上传代码。无论我尝试什么,我都无法通过这种方式上传。我每次都收到一个NullPointerException。我最初将它归咎于变量而不能使用Dropbox方法。经过几天试图找出它,最后,将auth代码和上传代码一起移动到Main.java是解决了我的问题的原因。不再有NullPointerExceptions。奇怪,但它奏效了。变量工作正常。
答案 0 :(得分:2)
所以我最后要做的就是解决这个问题:
以前我在两个文件之间工作。我在Main.java中有Dropbox authcode,在Dropboxfileuploadactivity.java中有实际的上传代码。无论我尝试什么,我都无法通过这种方式上传。我每次都收到一个NullPointerException。我最初将它归咎于变量而不能使用Dropbox方法。经过几天试图找出它,最后,将auth代码和上传代码一起移动到Main.java是解决了我的问题的原因。不再有NullPointerExceptions。奇怪,但它奏效了。变量工作正常。