无法找出为什么我得到空指针异常,任何人都可以帮助我吗?
Cant find out why i'm getting null pointer exception, can anybody help me?
我的任务是上传图片并向用户显示图片路径,我有以下代码片段但它没有显示图片的路径,而是在存储路径的位置抛出空指针异常,虽然我把通用作为默认路径
try {
String storagePath ="generic";
//String storagePath ="";
if(storagePath!= null)
{
if (fileType.equalsIgnoreCase("emailTemplateImagePathUrl")
|| fileType.equalsIgnoreCase("images"))
storagePath = configurationSettings
.getEmailTemplateImagePathUrl();
else if (fileType.equalsIgnoreCase("profileImagePathUrl"))
storagePath = configurationSettings.getProfileImagePathUrl();
else if (fileType.equalsIgnoreCase("imagePathUrl"))
storagePath = configurationSettings.getImagePathUrl();
else if (fileType.equalsIgnoreCase("postsImagePathUrl"))
storagePath = configurationSettings.getPostsImagePathUrl();
else if (fileType.equalsIgnoreCase("mantrasImagePathUrl"))
storagePath = configurationSettings.getMantrasImagePathUrl();
else if (fileType.equalsIgnoreCase("zodiacSignImagePathUrl"))
storagePath = configurationSettings.getZodiacSignImagePathUrl();
else if (fileType.equalsIgnoreCase("assetsBaseURL"))
storagePath = configurationSettings.getAssetsBaseURL();
else if (fileType.equalsIgnoreCase("assetsFolderName"))
storagePath = configurationSettings.getAssetsFolderName();
else if (fileType.equalsIgnoreCase("imagesFolderName"))
storagePath = configurationSettings.getImagesFolderName();
else if (fileType.equalsIgnoreCase("templeAssetsPrimaryFolderName"))
storagePath = configurationSettings
.getTempleAssetsPrimaryFolderName();
else if (fileType.equalsIgnoreCase("templeGalleryFolderName"))
storagePath = configurationSettings
.getTempleGalleryFolderName();
else if (fileType.equalsIgnoreCase("servicesFolderName"))
storagePath = configurationSettings.getServicesFolderName();
else if (fileType.equalsIgnoreCase("nearByFolderName"))
storagePath = configurationSettings.getNearByFolderName();
else if (fileType.equalsIgnoreCase("thumbnailsFolderName"))
storagePath = configurationSettings.getThumbnailsFolderName();
//String folderName = "C:\Users\Anupd\workspace2\Devalayam\WebContent\" + storagePath + "\" + fileName;
String folderName = "C:\Users\Anupd\workspace2\Devalayam\WebContent\" + storagePath + "\" + fileName;
System.out.println(storagePath);
System.out.println(folderName);
}
//String uploadFileLocation = folderName + storagePath + fileName;
String uploadFileLocation = storagePath ;
System.out.println(uploadFileLocation);
OutputStream out = new FileOutputStream(
new File(uploadFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = uploadInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
uploadInputStream.close();
out.flush();
out.close();
//
out = null;
System.gc();
File file = new File(uploadFileLocation);
mapObject.put("fileName", fileName);
mapObject.put("fileType", fileType);
String strLong = Long.toString(file.getTotalSpace());
mapObject.put("fileSize",strLong);
} catch (IOException e) {
e.printStackTrace();
}
return g.toJson(mapObject);
&操作是
空
C:\Users\Anupd\workspace2\Devalayam\WebContent\nullf96f04-169b-47e4-9128-a326adfd2e1a-medium.jpeg
null
提前致谢
在您的 ide 中设置调试断点(或者可以在每个 if 块中设置打印语句),并单步执行每个 if 循环。它会给你一个特定的点,它给你空指针异常。
您的代码不完整,有很多未定义的变量 - 那么就无法确定 NPE 的确切来源。可能性是:
fileType
configurationSettings
uploadInputStream
mapObject
检查堆栈跟踪,它应该会告诉您错误的来源。
我的任务是上传图片并向用户显示图片路径,我有以下代码片段但它没有显示图片的路径,而是在存储路径的位置抛出空指针异常,虽然我把通用作为默认路径
try {
String storagePath ="generic";
//String storagePath ="";
if(storagePath!= null)
{
if (fileType.equalsIgnoreCase("emailTemplateImagePathUrl")
|| fileType.equalsIgnoreCase("images"))
storagePath = configurationSettings
.getEmailTemplateImagePathUrl();
else if (fileType.equalsIgnoreCase("profileImagePathUrl"))
storagePath = configurationSettings.getProfileImagePathUrl();
else if (fileType.equalsIgnoreCase("imagePathUrl"))
storagePath = configurationSettings.getImagePathUrl();
else if (fileType.equalsIgnoreCase("postsImagePathUrl"))
storagePath = configurationSettings.getPostsImagePathUrl();
else if (fileType.equalsIgnoreCase("mantrasImagePathUrl"))
storagePath = configurationSettings.getMantrasImagePathUrl();
else if (fileType.equalsIgnoreCase("zodiacSignImagePathUrl"))
storagePath = configurationSettings.getZodiacSignImagePathUrl();
else if (fileType.equalsIgnoreCase("assetsBaseURL"))
storagePath = configurationSettings.getAssetsBaseURL();
else if (fileType.equalsIgnoreCase("assetsFolderName"))
storagePath = configurationSettings.getAssetsFolderName();
else if (fileType.equalsIgnoreCase("imagesFolderName"))
storagePath = configurationSettings.getImagesFolderName();
else if (fileType.equalsIgnoreCase("templeAssetsPrimaryFolderName"))
storagePath = configurationSettings
.getTempleAssetsPrimaryFolderName();
else if (fileType.equalsIgnoreCase("templeGalleryFolderName"))
storagePath = configurationSettings
.getTempleGalleryFolderName();
else if (fileType.equalsIgnoreCase("servicesFolderName"))
storagePath = configurationSettings.getServicesFolderName();
else if (fileType.equalsIgnoreCase("nearByFolderName"))
storagePath = configurationSettings.getNearByFolderName();
else if (fileType.equalsIgnoreCase("thumbnailsFolderName"))
storagePath = configurationSettings.getThumbnailsFolderName();
//String folderName = "C:\Users\Anupd\workspace2\Devalayam\WebContent\" + storagePath + "\" + fileName;
String folderName = "C:\Users\Anupd\workspace2\Devalayam\WebContent\" + storagePath + "\" + fileName;
System.out.println(storagePath);
System.out.println(folderName);
}
//String uploadFileLocation = folderName + storagePath + fileName;
String uploadFileLocation = storagePath ;
System.out.println(uploadFileLocation);
OutputStream out = new FileOutputStream(
new File(uploadFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = uploadInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
uploadInputStream.close();
out.flush();
out.close();
//
out = null;
System.gc();
File file = new File(uploadFileLocation);
mapObject.put("fileName", fileName);
mapObject.put("fileType", fileType);
String strLong = Long.toString(file.getTotalSpace());
mapObject.put("fileSize",strLong);
} catch (IOException e) {
e.printStackTrace();
}
return g.toJson(mapObject);
&操作是 空
C:\Users\Anupd\workspace2\Devalayam\WebContent\nullf96f04-169b-47e4-9128-a326adfd2e1a-medium.jpeg
null
提前致谢
在您的 ide 中设置调试断点(或者可以在每个 if 块中设置打印语句),并单步执行每个 if 循环。它会给你一个特定的点,它给你空指针异常。
您的代码不完整,有很多未定义的变量 - 那么就无法确定 NPE 的确切来源。可能性是:
fileType
configurationSettings
uploadInputStream
mapObject
检查堆栈跟踪,它应该会告诉您错误的来源。