I'm attempting to use a Spinner in Android to select a file from a directory, and then upload that file to an Amazon S3 bucket when a button is clicked. However, I'm getting a NullPointerException from the TransferUtility.Upload constructor.
Here's the full error:
AndroidRuntime: FATAL EXCEPTION: main
Process: [appname], PID: 12322
java.lang.IllegalStateException: Could not execute method for
android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19455)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19455)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5653)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at [appname].uploadFileToS3(Analyze.java:193)
And the methods that I'm using:
public void uploadFileToS3(View view){
// selectedFile is chosen from the spinner's selected position
File selectedFile = getFile(files);
//transfer utility
TransferObserver transferObserver = transferUtility.upload(
bucket, /* The bucket to upload to */
"TEST_FILE.txt", /* The key for the uploaded object */
selectedFile /* The file where the data to upload exists */
);
transferObserverListener(transferObserver);
}
private File getFile(ArrayList<File> files) {
// This is to grab just the file that's located at the spinner's
selected position
int position = fileSpinner.getSelectedItemPosition();
File selectedfile = files.get(position);
return selectedfile;
}
"bucket" is a global variable that's set to the name of our S3 bucket. I've passed the file to the TransferUtility constructor manually as well (without using the spinner) and it still threw a NullPointerException. Does anyone have any suggestions?