Reputation: 350
Am I missing something here? My code is kicking a NullPointerException based on this code to upload to 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);
The results of the system.out commands look ok. The inputStream variable outputs "java.io.FileInputStream@44ecd668" which I'm a little confused about but the systemout for the file variable displays the right path and filename. The nullPointerException seems to be coming from the setting of the newEntry variable which makes zero sense to me. Eclipse shows the code as being syntactically correct. This dropbox crap is killing me.
@BenHolland -- If I hardcode the paths as below --
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) {}
}
}
This simply generates a "File not found" in logcat which of course is coming from catch (FileNotFoundException). I don't however receive the NullPointerException. If you see at the top of the code -- the case statement -- this code is being run after a context menu click.
_____________________________________Updated code__________________________________________
// 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();
}
So I've updated the code to this. Whether I hardcode the paths or not seems to be irrelevant. Even If I do, the Entry upEntry line is causing a NullPointerException. I'm at a loss. I cant see anything at all that would be causing this.
________________________________Stack trace from 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)
There is the stack trace which occurs when I click "Send to Dropbox" . I think if I break down the whole process it might make it easier for someone to help.
The context menu contains a "Send To Dropbox" item. When clicked, it points to the case statement containing the code we have been looking at here. Thats what is generating the NullPointerException. As you can see above line 722 is the offending line. That line is the
Entry upEntry = mDBApi.putFile("/test.rar", inputStream,upfile.length(), null, null);
_____________________________________________Solution_______________________________________
So here's what I ended up doing to solve this:
Previously I was working between two files. I had the Dropbox authcode in Main.java, and the actual upload code in Dropboxfileuploadactivity.java. No matter what I tried, I could not get the upload to work this way. I received a NullPointerException every time. I initially blamed it on the variables and not being able to use then with the Dropbox methods. After days of trying to figure it out, in the end, moving the auth code and the upload code together in to Main.java is what solved my issue. No more NullPointerExceptions. Odd, but it worked. The variables work fine.
Upvotes: 2
Views: 1260
Reputation: 350
So heres what I ended up doing to solve this:
Previously I was working between two files. I had the Dropbox authcode in Main.java, and the actual upload code in Dropboxfileuploadactivity.java. No matter what I tried, I could not get the upload to work this way. I received a NullPointerException every time. I initially blamed it on the variables and not being able to use then with the Dropbox methods. After days of trying to figure it out, in the end, moving the auth code and the upload code together in to Main.java is what solved my issue. No more NullPointerExceptions. Odd, but it worked. The variables work fine.
Upvotes: 2