• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Problem with Jakarta Commons Fileupload

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
}
 
Sheriff
Posts: 26791
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic