Java 不知道为什么我';我遇到空指针异常,有人能帮我吗?
我的任务是将图像的图像和显示路径上传给用户,我有以下代码段,但它没有显示图像的路径,而是在存储路径的位置抛出空指针异常,尽管我将generic作为默认路径Java 不知道为什么我';我遇到空指针异常,有人能帮我吗?,java,image,rest,io,Java,Image,Rest,Io,我的任务是将图像的图像和显示路径上传给用户,我有以下代码段,但它没有显示图像的路径,而是在存储路径的位置抛出空指针异常,尽管我将generic作为默认路径 try { String storagePath ="generic"; //String storagePath =""; if(storagePath!= null) { if (fileType.equalsIgn
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();
// http://stackoverflow.com/questions/991489/i-cant-delete-a-file-in-java
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);
&行动是
空的
提前感谢在ide中设置一个断点进行调试(或者可以在每个if块中设置打印语句),并逐步完成每个if循环。它将为您提供一个特定的点,在该点上,它将为您提供空指针异常。您的代码不完整,并且有许多未定义的变量-因此无法确定NPE的确切来源。可能性是:
fileType
configurationSettings
uploadInputStream
mapObject
检查stacktrace,它会告诉您错误的来源。这么多IFs。只需调试一个,看看配置设置中缺少什么属性。您需要调试代码,看看它在哪里变为
null
。谢谢大家的宝贵建议,实际上问题是我尝试上载图像的文件夹路径配置不在数据库中,其他路径工作正常,这意味着您需要在程序中检查null
,以便捕获NPE并记录显式错误。
C:\Users\Anupd\workspace2\Devalayam\WebContent\null\17f96f04-169b-47e4-9128-a326adfd2e1a-medium.jpeg
null