Upload component

I am trying to do something simple, and I am stuck at a point. Any suggestions really appreciate. I have 2 files.

  1. MyVaadinUI.java
  2. UploadReceiver.java

I am creating a 9 x 9 integer array in uploadfinished method in the UploadReceiver. How do I access this varaible matrix in the MyVaadin file ?


package foo;

import com.vaadin.annotations.Theme;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.Upload;
import com.vaadin.ui.VerticalLayout;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
@Theme("sudotheme")
public class MyVaadinUI extends UI {

	private Container container = new IndexedContainer();
	private final VerticalLayout layout = new VerticalLayout();
	private Table table = new Table();
	private Label title = new Label("Sh Sudoku Solver");
	private UploadReceiver uploadReceiver = new UploadReceiver();
	private Upload upload = new Upload("Upload the file here", uploadReceiver);
	private Button solveButton = new Button("Solve");
	private Label statusLabel = new Label("none");

	@Override
	protected void init(VaadinRequest request) {
		layout.setMargin(true);
		layout.setSpacing(true);
		setContent(layout);
		upload.setButtonCaption("Upload Sudoko file");
		upload.setReceiver(uploadReceiver);
		upload.addFinishedListener(uploadReceiver);
		System.out.println("inside init " + uploadReceiver.getFileName());

		title.addStyleName("mystyle");
		table.addStyleName("checkerboard");

		for (int i = 1; i <= 9; i++) {
			container.addContainerProperty(i, Integer.class, 0);
		}

		for (int i = 1; i <= 9; i++) {
			container.addItem(i);
		}

		table.setPageLength(table.size());
		Item item = container.getItem(9);
		Property<Integer> nameProperty = item.getItemProperty(9);
		nameProperty.setValue(99);

		table.setContainerDataSource(container);
		layout.addComponent(title);
		layout.addComponent(upload);
		layout.addComponent(statusLabel);
		layout.addComponent(table);
		layout.addComponent(solveButton);
	}

}

UploadReceiver



package foo;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.vaadin.ui.Upload.FinishedEvent;
import com.vaadin.ui.Upload.FinishedListener;
import com.vaadin.ui.Upload.Receiver;

public class UploadReceiver implements Receiver, FinishedListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private FileOutputStream fos = null;
	private FileInputStream fis = null;
	private InputStreamReader isr = null;
	private Integer[][]
 matrix;

	private String fileName;

	public UploadReceiver() {

	}

	// The receiveUpload() method is called when the user clicks the submit
	// button.
	@Override
	public OutputStream receiveUpload(String filename, String mimeType) {

		fileName = filename;

		System.out.println("File Name inside receiveUpload = " + fileName + "MIME =" + mimeType);

		try {
			fos = new FileOutputStream("sudoinput.txt");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new OutputStream() {

			@Override
			public void write(int b) throws IOException {
				fos.write(b);
			}

		};
	}

	@Override
	public void uploadFinished(FinishedEvent event) {

		fileName = event.getFilename();
		System.out.println("File name inside upload finished = " + fileName + ":: " + event.getFilename());
		// TODO Auto-generated method stub
		matrix = new Integer[9]
[9]
;

		try {
			fis = new FileInputStream("sudoinput.txt");
			isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			String sCurrentLine;

			int i = 0;
			while ((sCurrentLine = br.readLine()) != null) {
				// System.out.println("sCurrentLine: " + sCurrentLine);
				String[] row = sCurrentLine.split(" ");
				int j = 0;
				for (String s : row) {
					// System.out.println("s=" + Integer.parseInt(s));
					matrix[i]
[j]
 = Integer.parseInt(s);
					// System.out.println("matrix=" + matrix[i]
[j]
);
					j++;
				}
				i++;
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public String getFileName() {
		System.out.println("File Name inside getFileName  = " + fileName);
		return fileName;
	}

	public Integer[][]
 getMatrix() {
		return matrix;
	}

	public void setMatrix(Integer[][]
 matrix) {
		this.matrix = matrix;
	}

}

I don’t understand the question. What do you mean by “how do I access it”? You have already created a getter method for it. Isn’t that suitable for it?

getter method is not giving me anything but null. which is what i dont understand

basically, System.out.println("inside init " + uploadReceiver.getFileName()); is coming to be null and also the matrix is also null.

I’m sure you figured this out already, but just answering this way delayed answer for completeness sake. Inside init before the receiver is actually received the file getFileName will return null. You have to call the getFileName after finish has been called. How do you know when it’s called? Well that’s why you have the listener in the first place. So give a way for your finished listener to use the result either through another interface or by directly. Depends really on how clean implementation you want.