sanedroid
sanedroid

Reputation: 1036

nullpointerexception while uploading images to azure blob storage in android

The user selects images from gallery and those selected images I am uploading on windows azure blob storage. But while uploading I am getting a null-pointer exception.

I couldn't find any solution on internet. The ArrayList<String> 'selected' consists of paths of the selected images. The paths are displayed in this format in the logcat: mnt/sdcard/Pictures/image1.jpg

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    selected = new ArrayList<String>();

    try {
        // Retrieve storage account from connection-string.
        storageAccount = CloudStorageAccount.parse(storageConnectionString);

        // Create the blob client.
        blobClient = storageAccount.createCloudBlobClient();

        // Get a reference to a container.
        // The container name must be lower case
        blobContainer = blobClient.getContainerReference("mycontainer");

        // Create the container if it does not exist.
        // Create a blob container using the createIfNotExist method that
        // checks whether a container exists with the same name. The method
        // creates the blob container only if a container with the same name
        // does not exist. Otherwise, no operation is performed. 
        blobContainer.createIfNotExists();

        // Create a permissions object.
        containerPermissions = new BlobContainerPermissions();

        // Include public access in the permissions object.
        containerPermissions.setPublicAccess(BlobContainerPublicAccessType.CONTAINER);

        // Set the permissions on the container.
        blobContainer.uploadPermissions(containerPermissions);

    } catch (InvalidKeyException e1) {
        e1.printStackTrace();
    } catch (Exception e1) {
        e1.printStackTrace();
    }             
}

public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);

    // Inflate the menu items for use in the action bar

    inflater.inflate(R.menu.mymenu, menu);

    // Here we get the action view we defined
    myActionMenuItem = menu.findItem(R.id.my_action);
    View actionView = myActionMenuItem.getActionView();

    // We then get the button view that is part of the action view
    if(actionView != null) {
        myActionButton = (Button) actionView.findViewById(R.id.action_btn);
        myActionButton.setText(R.string.txt_submit);
        if(myActionButton != null) {
            // We set a listener that will be called when the return/enter key is pressed
            myActionButton.setOnClickListener(new OnClickListener() {                       

                @Override
                public void onClick(View v) {

                    myActionButton.setEnabled(false);

                    myActionButton.setText("Submitting..");                         

                    try {

                        for(int i = 0; i <selected.size();i++){
                            String filePath = selected.get(i).sdcardPath;

                            File source = new File(filePath);
                            String absoluteFilePath = source.getAbsolutePath();
                            Log.d("personal", absoluteFilePath);
                            CloudBlockBlob blob = blobContainer.getBlockBlobReference(source.getName());
                            Log.d("personal", source.getName());
                            //Log.d("personal", imageName.get(i));
                            blob.upload(new FileInputStream(absoluteFilePath), source.length());
                            //blob.uploadFromFile(filePath);
                            Log.d("personal", "Image Uploaded");
                        }

                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                });

            }
        }
    }

StackTrace:

10-09 15:50:27.168: W/System.err(1451): java.lang.NullPointerException
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:784)
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:274)
10-09 15:50:27.168: W/System.err(1451):     at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:479)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:145)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:252)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.commitBlockList(CloudBlockBlob.java:242)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.BlobOutputStream.commit(BlobOutputStream.java:321)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.BlobOutputStream.close(BlobOutputStream.java:285)
10-09 15:50:27.178: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:582)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.azure.storage.blob.CloudBlockBlob.upload(CloudBlockBlob.java:499)
10-09 15:50:27.199: W/System.err(1451):     at com.jbandroid.fragment.PersonalInfoFragment$2.onCompleted(PersonalInfoFragment.java:273)
10-09 15:50:27.199: W/System.err(1451):     at com.jbandroid.fragment.PersonalInfoFragment$2.onCompleted(PersonalInfoFragment.java:1)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceTable$ParseResultOperationCallback.onCompleted(MobileServiceTable.java:103)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$2.onCompleted(MobileServiceJsonTable.java:249)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$4.onPostExecute(MobileServiceJsonTable.java:389)
10-09 15:50:27.199: W/System.err(1451):     at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable$4.onPostExecute(MobileServiceJsonTable.java:1)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask.finish(AsyncTask.java:602)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
10-09 15:50:27.199: W/System.err(1451):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
10-09 15:50:27.199: W/System.err(1451):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 15:50:27.208: W/System.err(1451):     at android.os.Looper.loop(Looper.java:137)
10-09 15:50:27.208: W/System.err(1451):     at android.app.ActivityThread.main(ActivityThread.java:4340)
10-09 15:50:27.208: W/System.err(1451):     at java.lang.reflect.Method.invokeNative(Native Method)
10-09 15:50:27.208: W/System.err(1451):     at java.lang.reflect.Method.invoke(Method.java:511)
10-09 15:50:27.208: W/System.err(1451):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-09 15:50:27.208: W/System.err(1451):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-09 15:50:27.208: W/System.err(1451):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 709

Answers (1)

Emily Gerner
Emily Gerner

Reputation: 2457

This may be because Android does not allow network connections on the main thread. The storage library currently provides the incorrect error message in this case - we're working on fixing this. Take a look at this other Stack Overflow post for more info.

If this is not the issue, if you could provide the version of the Android library you're working with and the version of Android you're running on that would be helpful in reproducing the problem.

Upvotes: 2

Related Questions