-1

My task is to upload image & display path of the image to the user, i have the following snippet but its not showing the path of the image, instead it is throwing null pointer exception in at the location of storage path, though i put generic as default path

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

& the op is null

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

thanks in advance

Robert Moskal
  • 21,737
  • 8
  • 62
  • 86

2 Answers2

0

Setup a break point to debug in your ide (or can setup print statements in each if block), and step through each if loop. It will give you a specific point in which it gives you the Null Pointer Exception.

Singledigit
  • 124
  • 1
  • 6
0

Your code is not complete with lots of variables undefined - then there is no way to determine where the NPE comes from exactly. Possibilities are:

  • fileType
  • configurationSettings
  • uploadInputStream
  • mapObject

Check the stacktrace, it should give you where the error comes from.

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • Thank you all for your valuable suggestions, actually the problem was that the folder path configuration with which i was trying to upload image was not in the db, with other path its working fine, – Anup Deshpande Apr 21 '15 at 07:20
  • It means you need to check for `null` in your program in order to catch NPE and log explicit errors. – T.Gounelle Apr 21 '15 at 07:34