Validator for file upload component is not working !!

This validator method for a file upload component is throwing a java.lang.NullPointerException.

public void fileUpload1_validate(FacesContext context, UIComponent component, Object value) {

long size=fileUpload1.getUploadedFile().getSize();

if (size>65535){

throw new ValidatorException(new FacesMessage("Your image is too big!"));

}

}

Any help to solve this problem is very much appreciated.

Thanks.

[467 byte] By [MISS_DUKE] at [2007-11-26 8:39:33]
# 1

Hi,

Are you sure that correct file name is specified in Upload component?

Also, are you sure that you check is applied to fileUpload1, not another component?

Also please note that better practice is to use component parameter of validate method and do not specify this component directly:

long size = ((Upload)component).getUploadedFile().getSize();

Thanks, Misha

(Creator team)

Mikhail_Matveev at 2007-7-6 22:14:58 > top of Java-index,Development Tools,Java Tools...
# 2
Hi,I have checked everything. java.lang.NullPointerException is thrown when tested.Thanks.
MISS_DUKE at 2007-7-6 22:14:58 > top of Java-index,Development Tools,Java Tools...
# 3

Hi,

I like mention something about the Technical Tips for File Upload component given by Sakthivel Gopal here:

http://developers.sun.com/prodtech/javatools/jscreator/reference/tips/2/retriev e_binary_data.html

His validator method is this:

public void fileUpload1_validate(FacesContext context, UIComponent component, Object value) {

// TODO: Check the value parameter here, and if not valid, do something like this:

// throw new ValidatorException(new FacesMessage("Not a valid value!"));

try {

//Context initContext = new InitialContext();

//com.sun.rave.web.ui.util.UploadFilter

long l_filesize = ((Upload) component).getUploadedFile().getSize();

if ( l_filesize > 1000000)

throw new ValidatorException(new FacesMessage("File Size should be less than 1000000 bytes! It is" + l_filesize));

} catch (Exception ex) {

log("Error Description", ex);

error(ex.getMessage());

}

}

He puts his validator codes inside a "try" block, but in this case always try block is bypassed and the "catch" block is executed, therefore the validator method is not doing the validation at any time.

Thanks.

MISS_DUKE at 2007-7-6 22:14:58 > top of Java-index,Development Tools,Java Tools...
# 4

Hi i have a fileUpload in a pop-up and the methods are :

public void fileUpload1_validate(FacesContext context, UIComponent component, Object value) {

try {

com.sun.rave.web.ui.model.UploadedFile uploadedFile = ((Upload)component).getUploadedFile();

long l_filesize = uploadedFile.getSize();

if ( l_filesize > 1000000)

throw new ValidatorException(new FacesMessage("File Size should be less than 1000000 bytes! It is" + l_filesize));

else {

String tipo = uploadedFile.getContentType();

if ( tipo.compareTo("application/word") == -1 )

if ( tipo.compareTo("application/rtf") == -1 )

if ( tipo.compareTo("application/pdf") == -1 )

throw new ValidatorException(new FacesMessage("Error : los tipos de archivo aceptados son PDF, WORD y RTF"));

}

} catch (Exception ex) {

this.mauvaisFichier = true;

error(ex.getMessage());

}

}

public String botonValidar_action() {

if ( !this.mauvaisFichier )

if ( this.fileUpload1.getUploadedFile() != null )

if ( this.fileUpload1.getUploadedFile().getSize() > 0 )

try {

this.getSessionBean1().getComunicacion().setArchivo( new javax.sql.rowset.serial.SerialBlob(this.fileUpload1.getUploadedFile().getBytes( ) ) );

this.getSessionBean1().getComunicacion().setTipoArchivo( this.fileUpload1.getUploadedFile().getOriginalName().substring(this.fileUpload1 .getUploadedFile().getOriginalName().length() - 3 ) );

} catch(Exception e){

e.getMessage();

}

return null;

}

I have to put a boolean value because the first time, validation code doesn't function ( the method botonValidar_action() is called ) correctly. The error message appears but empty.

If I try next time then it's ok.

Thx for any issue.

Baal at 2007-7-6 22:14:58 > top of Java-index,Development Tools,Java Tools...