Solved

ServletFileUpload java.lang.NullPointerException

Posted on 2008-10-16
3
710 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Fix45 challenge 15 104
how to hide hide popup page refresh second time? 10 112
word0 challenge 3 79
print map entry 34 56
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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.

862 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now