container.createIfNotExists() 上的空对象引用错误;
Null object reference error on container.createIfNotExists();
我正在尝试通过以下 link 将 Azure Blob Storage 用于 Android 应用程序:
http://azure.microsoft.com/en-in/documentation/articles/storage-java-how-to-use-blob-storage/
我正在使用 Android Studio
在行中出现异常之前,我一切正常:
container.createIfNotExists();
这一行报错:
Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
以下是我的导入:
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
java代码:
try{
System.out.print("Code Progess 2 ");
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
System.out.print("Code Progess 3 ");
CloudBlobClient serviceClient = account.createCloudBlobClient();
System.out.print("Code Progess 4 ");
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("myimages");
System.out.print("Code Progess 5 ");
container.createIfNotExists();
System.out.print("Code Progess 6 ");
// Upload an image file.
CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
System.out.print("Code Progess 7 ");
File sourceFile = new File("<Path to local image storage>");
System.out.print("Code Progess 8 ");
blob.upload(new FileInputStream(sourceFile), sourceFile.length());
System.out.print("Code Progess 9 ");
// Download the image file.
File destinationFile = new File(sourceFile.getParentFile(), "image1Download.tmp");
System.out.print("Code Progess 10 ");
blob.downloadToFile(destinationFile.getAbsolutePath());
System.out.print("Code Progess 11 ");
}
catch (Exception e) {
System.out.print("Exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
logcat如下:
01-09 20:54:02.072 2271-2287/com.rollcall.blob W/art﹕ Verification of void com.microsoft.azure.storage.blob.CloudBlobContainer.create(com.microsoft.azure.storage.blob.BlobRequestOptions, com.microsoft.azure.storage.OperationContext) took 334.218ms
01-09 20:54:02.140 2271-2287/com.rollcall.blob W/art﹕ Unresolved exception class when finding catch block: javax.xml.stream.XMLStreamException
01-09 20:54:02.141 2271-2287/com.rollcall.blob I/System.out﹕ Code Progess 1 Code Progess 2 Code Progess 3 Code Progess 4 Code Progess 5 Exception encountered: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
谁能告诉我哪里错了?
谢谢
您参考的入门指南专门针对 Java。您使用 Java 或 Android Azure 存储客户端库作为示例吗?有一个单独的 android Azure 存储客户端库,你应该使用它来构建 Android 应用程序。
你可以看看readme document on Github repository to get started. Sample code on working with blob can also be found in the repository here
我正在尝试通过以下 link 将 Azure Blob Storage 用于 Android 应用程序: http://azure.microsoft.com/en-in/documentation/articles/storage-java-how-to-use-blob-storage/
我正在使用 Android Studio
在行中出现异常之前,我一切正常:
container.createIfNotExists();
这一行报错:
Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
以下是我的导入:
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
java代码:
try{
System.out.print("Code Progess 2 ");
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
System.out.print("Code Progess 3 ");
CloudBlobClient serviceClient = account.createCloudBlobClient();
System.out.print("Code Progess 4 ");
// Container name must be lower case.
CloudBlobContainer container = serviceClient.getContainerReference("myimages");
System.out.print("Code Progess 5 ");
container.createIfNotExists();
System.out.print("Code Progess 6 ");
// Upload an image file.
CloudBlockBlob blob = container.getBlockBlobReference("image1.jpg");
System.out.print("Code Progess 7 ");
File sourceFile = new File("<Path to local image storage>");
System.out.print("Code Progess 8 ");
blob.upload(new FileInputStream(sourceFile), sourceFile.length());
System.out.print("Code Progess 9 ");
// Download the image file.
File destinationFile = new File(sourceFile.getParentFile(), "image1Download.tmp");
System.out.print("Code Progess 10 ");
blob.downloadToFile(destinationFile.getAbsolutePath());
System.out.print("Code Progess 11 ");
}
catch (Exception e) {
System.out.print("Exception encountered: ");
System.out.println(e.getMessage());
System.exit(-1);
}
logcat如下:
01-09 20:54:02.072 2271-2287/com.rollcall.blob W/art﹕ Verification of void com.microsoft.azure.storage.blob.CloudBlobContainer.create(com.microsoft.azure.storage.blob.BlobRequestOptions, com.microsoft.azure.storage.OperationContext) took 334.218ms
01-09 20:54:02.140 2271-2287/com.rollcall.blob W/art﹕ Unresolved exception class when finding catch block: javax.xml.stream.XMLStreamException
01-09 20:54:02.141 2271-2287/com.rollcall.blob I/System.out﹕ Code Progess 1 Code Progess 2 Code Progess 3 Code Progess 4 Code Progess 5 Exception encountered: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
谁能告诉我哪里错了?
谢谢
您参考的入门指南专门针对 Java。您使用 Java 或 Android Azure 存储客户端库作为示例吗?有一个单独的 android Azure 存储客户端库,你应该使用它来构建 Android 应用程序。
你可以看看readme document on Github repository to get started. Sample code on working with blob can also be found in the repository here