Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
File upload null exception
Hello, I'm having a problem with the Upload component, I have created a component based on the example I saw and Its workig except that when I click the upload file button and if I dont have selected any file, the compoenent throws a null exception, how do I fix this? , where do I check if there is a file selected?
Here is part of the code:
ImageUploader receiver = new ImageUploader();
uploadImgs = new Upload("", receiver);
uploadImgs.setButtonCaption("Subir Imagen");
uploadImgs.addSucceededListener(receiver);
uploadImgs.focus();
uploadImgs.addStartedListener(new UploadStartedLis());
uploadImgs.addFailedListener(new UploadFailedLis());
//not sure why 2 SuccedededListener, but in one you return an OutputStream
uploadImgs.addSucceededListener(new UploadSuccessLis());
....
.....
......
class ImageUploader implements Receiver, SucceededListener {
private static final long serialVersionUID = -403236584428263229L;
public File file;
FileOutputStream fos = null;
public OutputStream receiveUpload(String filename, String mimeType)
{
try{
if(filename.length()==0) //not sure if this is the problem
{
Notification.show("Error", "favor de seleccionar un archivo", Notification.Type.WARNING_MESSAGE);
return null;
}
...
...
...
private class UploadSuccessLis implements Upload.SucceededListener{
private static final long serialVersionUID = -4584873468771037333L;
@Override
public void uploadSucceeded(SucceededEvent event) {
//System.out.println( "Terminado:" + event.getFilename()+ " - "+ event.getMIMEType() );
//Notification.show("Error", "Imagen guardada", Notification.Type.TRAY_NOTIFICATION);
if( !event.getMIMEType().equalsIgnoreCase("image/jpeg") ) {
uploadImgs.interruptUpload();
deleteFile(event.getFilename());
Notification.show("Error", "El archivo no es tipo 'image/jpeg'.", Notification.Type.ERROR_MESSAGE);
Log.Warning("OrderingImgs", "uploadStarted", "El archivo no es tipo 'image/jpeg' archivo:"+event.getFilename() + " mimetype:"+ event.getMIMEType() );
lblInfo.setValue("");
return;
}
Thanks for your help.
Hi, Jose,
are you getting this error message? "favor de seleccionar un archivo"
If yes, then probably the problem is in UploadFailedLis class.
Because if recieve upload method returns null, uploadFailed method will be called.
It seems that you are already handling the situation when there is no file selected here:
if(filename.length()==0) {
....;
}