0

I am trying to list the files in a folder but it produces a null pointer exception this is the code

String url=req.getRequestURI();
String path=session.getServletContext().getRealPath(url);
File folder = new File(path);
      File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {//this line is producing nullpointer exception
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
      }
    }

When i print the path it gives the correct path: like D:\apache-tomcat-8.5.24\webapps\JaasLogin\JaasLogin\user-files. i am new to java Context path: /jaasLogin but when i give path directly like "D:\\apache-tomcat-8.5.24\\webapps\\JaasLogin\\JaasLogin\\user-files" it will works

Output i got

 org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [userfileserv] in context with path [/JaasLogin] threw exception
 java.lang.NullPointerException
    at myPackage.UserFileServ.doGet(UserFileServ.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
  • Please edit the `NullPointerException` into your question – Lino Jun 22 '18 at 09:06
  • 7
    Possible duplicate of [NullPointerException while reading multiple files from a directory](https://stackoverflow.com/questions/17810338/nullpointerexception-while-reading-multiple-files-from-a-directory) – morganbaz Jun 22 '18 at 09:11
  • question is exactly like that but it just skipped null file . bt i hava a file in that folder – Antony Vibin Jun 22 '18 at 09:19
  • which line is producing the exception ? – vincrichaud Jun 22 '18 at 09:29
  • Printing the path doesn't tells you if it correct or not, it may contain character not printed causing the path to be wrong. For example it's something that append [here](https://stackoverflow.com/questions/50813491/empty-file-constructor-is-neither-file-nor-directory) – vincrichaud Jun 22 '18 at 09:32
  • for loop line is producing exception – Antony Vibin Jun 22 '18 at 09:36
  • So it's the duplicate. Read the answer of the question I posted. You need to check if `listOfFiles` is null - if so, an I/O error probably happened. The folder might not exist or it might not be a folder - or you may not have rights to access it, etc. – morganbaz Jun 22 '18 at 09:45
  • folder is there . i checked it How to know if i have the right to access . the folder is inside the webapp folder. so it should be accessable by server right? – Antony Vibin Jun 22 '18 at 09:56
  • i give directly th path like D:\\apache-tomcat-8.5.24\\webapps\\JaasLogin\\user-files\\Antony by escaping the single \ with \\ . it worked but i it give error when it is a variable – Antony Vibin Jun 22 '18 at 10:01
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – rustyx Jun 22 '18 at 11:17
  • "for loop line is producing exception " - mark with a comment which line throws the exception. Also post the output you get – c0der Jun 22 '18 at 11:23

1 Answers1

0

As Javadoc for File.listFiles() states:

Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So in order to prevent NPE do something like this:

    File[] listOfFiles = folder.listFiles();

    if (listOfFiles != null) {
        for (int i = 0; i < listOfFiles.length; i++) {
          if (listOfFiles[i].isFile()) {
            System.out.println("File " + listOfFiles[i].getName());
          } else if (listOfFiles[i].isDirectory()) {
            System.out.println("Directory " + listOfFiles[i].getName());
          }
        }
    }
    else {
        System.out.println("Could not read " + folder.getName());
    }
Würgspaß
  • 4,660
  • 2
  • 25
  • 41