无法找出为什么我得到空指针异常,任何人都可以帮助我吗?

时间:2015-04-20 13:58:34

标签: java image rest io

我的任务是上传图片&向用户显示图像的路径,我有以下片段,但它没有显示图像的路径,而是在存储路径的位置抛出空指针异常,尽管我将泛型作为默认路径

  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);

&安培;操作是 空

C:\Users\Anupd\workspace2\Devalayam\WebContent\null\17f96f04-169b-47e4-9128-a326adfd2e1a-medium.jpeg
null

提前致谢

2 个答案:

答案 0 :(得分:0)

在ide中设置一个断点来调试(或者可以在每个if块中设置print语句),并逐步执行每个if循环。它将为您提供一个特定的点,它为您提供空指针异常。

答案 1 :(得分:0)

你的代码不完整,许多变量未定义 - 那么就无法确定NPE的确切位置。可能性是:

  • fileType
  • configurationSettings
  • uploadInputStream
  • mapObject

检查堆栈跟踪,它应该为您提供错误的来源。