Solved

ServletFileUpload java.lang.NullPointerException

Posted on 2008-10-16
3
718 Views
Last Modified: 2013-11-24
Hi

I have a File upload on a jsp page, if I do not upload and press submit, it throws java.lang.NullPointerException

I use

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart)
then upload file

Can ServletFileUpload == null then redirect to another jsp page?

0
Comment
Question by:techques
  • 2
3 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 22737430
you can put some javascript validation for this?

if(document.forms[0].fileupoload.value="")
alert("Please upload file")
else
redirect to ur jsp;;
0
 

Author Comment

by:techques
ID: 22737702
I use the following code to check whether the upload file is in the right format extension.

However, once I use it and it cannot pass the variable to servlet and throw nullpointerexception.

How can I fix it?

<script language="JavaScript">
    extArray = new Array(".jpg", ".png", ".gif");
    function LimitAttach(form, file) {
    allowSubmit = false;
    if (!file) return;
    while (file.indexOf("\\") != -1)
    file = file.slice(file.indexOf("\\") + 1);
    ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++) {
    if (extArray[i] == ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) return true;
    else
    alert("Please only upload files that end in types:  "
    + (extArray.join("  ")) + "\nPlease select a new "
    + "file to upload and submit again.");
    return false;
    }
</script>
 
OnSubmit="return LimitAttach(this.form, this.form.myimage.value)"
 
servlet:
boolean isMultipart = ServletFileUpload.isMultipartContent(request);

Open in new window

0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 50 total points
ID: 22737907
can't you write like this
if (allowSubmit)
{
document.forms[0].action="/ServletFileUpload?action=fileupload&file="+the file u want to upload
return true;
}
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question