Android使用Azure移动服务删除空例外

时间:2014-01-02 05:08:03

标签: android azure azure-storage-blobs azure-mobile-services azure-storage

我正在尝试从我的azure移动服务存储中删除blob和容器,并且我不断从调用中获取异常。我的上传,geturi,下载,getcontainers等都工作,但既不删除blob或删除容器工作。我一直有例外。对于删除容器,我得到:

    Process: com.w9jds.myglassshare, PID: 504
java.lang.NullPointerException
        at com.w9jds.myglassshare.Classes.StorageService$2.onCompleted(StorageService.java:117)
        at com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.delete(MobileServiceTableBase.java:218)
        at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable.delete(MobileServiceJsonTable.java:1)
        at com.w9jds.myglassshare.Classes.StorageService.deleteContainer(StorageService.java:110)
        at com.w9jds.myglassshare.MainActivity$2.onClick(MainActivity.java:116)
        at android.view.View.performClick(View.java:4442)
        at android.view.View$PerformClick.run(View.java:18423)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5083)
        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:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
        at dalvik.system.NativeStart.main(Native Method)

我得到的删除blob:

01-01 23:00:47.561    1457-1457/com.w9jds.myglassshare E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.w9jds.myglassshare, PID: 1457
java.lang.RuntimeException: Error receiving broadcast Intent { act=blob.delete flg=0x10 } in com.w9jds.myglassshare.MainActivity$3@41f34b50
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5083)
        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:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.w9jds.myglassshare.Classes.StorageService$4.onCompleted(StorageService.java:198)
        at com.microsoft.windowsazure.mobileservices.MobileServiceTableBase.delete(MobileServiceTableBase.java:218)
        at com.microsoft.windowsazure.mobileservices.MobileServiceJsonTable.delete(MobileServiceJsonTable.java:1)
        at com.w9jds.myglassshare.Classes.StorageService.deleteBlob(StorageService.java:191)
        at com.w9jds.myglassshare.MainActivity$3.onReceive(MainActivity.java:187)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:768)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5083)
        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:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
        at dalvik.system.NativeStart.main(Native Method)

这两个电话的代码如下:

DeleteContainer:

    public void deleteContainer(String containerName)
    {
        //Create the json Object we'll send over and fill it with the required
        //id property - otherwise we'll get kicked back
        JsonObject container = new JsonObject();
        container.addProperty("id", 0);
        //Create parameters to pass in the container details.  We do this with params
        //because it would be stripped out if we put it on the container object
        List<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>();
        parameters.add(new Pair<String, String>("containerName", containerName));
        mTableContainers.delete(container, parameters, new TableDeleteCallback()
        {
            @Override
            public void onCompleted(Exception exception, ServiceFilterResponse response)
            {
                if (exception != null)
                {
                    Log.e(TAG, exception.getCause().getMessage());
                    return;
                }

                //Refetch containers from the server
                //Intent broadcast = new Intent();
                //broadcast.setAction("blob.deleted");
                //mContext.sendBroadcast(broadcast);

                getContainers();
            }
        });
    }

DeleteBlob:

    public void deleteBlob(final String containerName, String blobName)
    {
        //Create the json Object we'll send over and fill it with the required
        //id property - otherwise we'll get kicked back
        JsonObject blob = new JsonObject();
        blob.addProperty("id", 0);
        //Create parameters to pass in the blob details.  We do this with params
        //because it would be stripped out if we put it on the blob object
        List<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>();
        parameters.add(new Pair<String, String>("containerName", containerName));
        parameters.add(new Pair<String, String>("blobName", blobName));

        mTableBlobs.delete(blob, parameters, new TableDeleteCallback()
        {
            @Override
            public void onCompleted(Exception exception, ServiceFilterResponse response)
            {
                if (exception != null)
                {
                    Log.e(TAG, exception.getCause().getMessage());
                    return;
                }
                //Refetch the blobs from the server
                //getBlobsForContainer(containerName);
            }
        });
    }

对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

异常发生在onCompleted方法中,我发现空指针异常发生的唯一方法是exception.getCause()调用返回null。尝试按如下方式重写您的方法:

public void onCompleted(Exception exception, ServiceFilterResponse response)
{
    if (exception != null)
    {
        Log.e(TAG, exception.getMessage();
        Throwable t = e.getCause();
        while (t != null)
        {
            Log.e(TAG, "  Cause: " + t.getMessage());
            t = t.getCause();
        }
        return;
    }