I'm new to Java EE, and I want to display in a webpage a list of PDF thumbnails. These PDF are stored in a folder in src/main/webapp/pdf
, and I want to read this folder to get all the filenames. Here is my code :
@Override
public void doGet(HttpServletRequest req, HttpServletResponse res) {
try {
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
PrintWriter out = res.getWriter();
File pdfFolder = new File("/pdf");
for (File pdf : pdfFolder.listFiles()) { // Line 27
out.println(pdf.getName());
}
} catch (IOException e) {
log.log(Level.SEVERE, e.getMessage());
}
}
When I run this code, I get a NullPointerException
:
java.lang.NullPointerException
com.multi.services.ListFiles.doGet(ListFiles.java:27)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
Here is my structure :
What I want to have is a web service that reads the PDF folder and returns a JSON containing the PDF filenames, and I will call this service in a JavaScript using Ajax.
Can anyone help me to make my script running well ? Or has anyone a better solution ?
Thanks :)