File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Content Type issue

 
Rick Shroff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to do the foloowing with the HTML and servlet code given below.
Downlaod a jar file if the user checks the download button; else show him an error page.

The 'if' part works fine. The 'else' part throw NullPointerException. It does not even print the "in else" statement to the console. Please help.
Have been trying for a long time on this.

Thanks in advance,
Rick

 
Edvins Reisons
Ranch Hand
Posts: 364
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line is throwing the exception?
What does request.getParameter("download") return when the checkbox is left unchecked?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Browsers send nothing if a checkbox is unchecked.
This means that getParameter will return null in that case.
Null pointers don't have an .equals(..) method.

You'll need to either check for null before trying to call equals or just check for null. Another alternative is to turn the test around.


Since the string literal "on" will always be a valid string, you can call its equals method instead of trying to call it on the results from getParameter.
 
Rick Shroff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick Shroff:
Hi,

I'm trying to do the foloowing with the HTML and servlet code given below.
Downlaod a jar file if the user checks the download button; else show him an error page.

The 'if' part works fine. The 'else' part throw NullPointerException. It does not even print the "in else" statement to the console. Please help.
Have been trying for a long time on this.

Thanks in advance,
Rick

 
Rick Shroff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Edvins and Ben!

The problem was indeed due to the 'null' returned when the download checkbox was not checked.

I used the following as suggested by Ben-



And could correctly display the Error page. Thanks!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic