Solved

ServletFileUpload java.lang.NullPointerException

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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 …
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.

758 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

17 Experts available now in Live!

Get 1:1 Help Now