File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Jakarta Commons Fileupload

 
sabbir kazi
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am getting a NullPointerException when trying to execute:

List fileList = fileUpload.parseRequest(request); in my Servlet.

Please help me find out why I am getting this exception.

Thanks.

Sabbir

Here is the full code:

public void doPost( HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// JDBC Connection
if (conn == null){
prepareConnection();
try {
stmt = conn.prepareStatement(
"INSERT into Template (customer_id, template_name, template_size,"+
"template) VALUES"+
"(123456,?,?,?)");

// parse the incoming request
ServletFileUpload fileUpload = new ServletFileUpload();


try {
System.out.println("Calling ServletFileUpload's parseRequest method...");
List fileList = fileUpload.parseRequest(request);
System.out.println("Successful");
Iterator fileItemItr = fileList.iterator();
while (fileItemItr.hasNext()){
if (!fileItem.isFormField()){
fileItem = (FileItem) fileItemItr.next();
fileName = fileItem.getName();
fileInputStream = fileItem.getInputStream();
fileSize = fileItem.getSize();
} else {
// must be other fields
}
// writing to database
stmt.setString(1, fileName);
stmt.setLong(2, fileSize);
stmt.setBinaryStream(3, fileInputStream, 10);
stmt.execute();
}
} catch (FileUploadException e1) {
// TODO Auto-generated catch block
System.out.println("Un-Successful "+ e1.getMessage());
e1.printStackTrace();
}

releaseConnection();

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
 
Paul Clapham
Sheriff
Pie
Posts: 20161
24
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would help to see the stack trace instead of just the class of the exception that's being thrown.
 
sabbir kazi
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Thanks for your reply. Here is the exception stack trace:

 
Angel J Gama
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think you need to create a FileItemFactory and use it to create the ServletFileUpload, and you have to read all elements even when the element is/isn't a form field... Here's you code modified:

// parse the incoming request
FileItemFactory factory = new DiskFileItemFactory(); //MODIFIED
ServletFileUpload fileUpload = new ServletFileUpload(factory); //MODIFIED
try {
System.out.println("Calling ServletFileUpload's parseRequest method...");
List fileList = fileUpload.parseRequest(request);
System.out.println("Successful");
Iterator fileItemItr = fileList.iterator();
while (fileItemItr.hasNext()){
fileItem = (FileItem) fileItemItr.next(); //MODIFIED (used to be within if, you have to read the element even when it's not a form field element, then you ask if it is a formfield element)
if (!fileItem.isFormField()){
fileName = fileItem.getName();
fileInputStream = fileItem.getInputStream();
fileSize = fileItem.getSize();
} else {
// must be other fields
}
}

Hope it helps.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic