Solved

ServletFileUpload java.lang.NullPointerException

Posted on 2008-10-16
3
732 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

729 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