Hi
I am trying to upload file but getting the following error message:
java.lang.NullPointerException
at com.company.demofileupload.gui.screens.Myfileupload.init(Myfileupload.java:31)
at com.haulmont.cuba.gui.WindowManager.init(WindowManager.java:731)
at com.haulmont.cuba.gui.WindowManager.initWrapperFrame(WindowManager.java:722)
at com.haulmont.cuba.gui.WindowManager.createWindow(WindowManager.java:176)
at com.haulmont.cuba.gui.WindowManager.openWindow(WindowManager.java:345)
at com.haulmont.cuba.gui.config.MenuCommand.execute(MenuCommand.java:87)
at com.haulmont.cuba.web.sys.MenuBuilder$1.menuSelected(MenuBuilder.java:153)
at com.vaadin.ui.MenuBar.changeVariables(MenuBar.java:207)
at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:508)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:327)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:202)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:95)
at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.lambda$synchronizedHandleRequest$67(CubaVaadinServletService.java:290)
at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler$$Lambda$11/103374517.call(Unknown Source)
at com.haulmont.cuba.web.sys.CubaVaadinServletService.withUserSession(CubaVaadinServletService.java:171)
at com.haulmont.cuba.web.sys.CubaVaadinServletService$CubaUidlRequestHandler.synchronizedHandleRequest(CubaVaadinServletService.java:290)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1408)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:356)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:226)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:149)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.haulmont.cuba.web.sys.CubaHttpFilter.handleNotFiltered(CubaHttpFilter.java:100)
at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:87)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1526)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1482)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
I have created a screen file and copied the data from documentation to controller as follows:
I just fixed the error and file is now uploaded. But i don’t see any file name that has been uploaded. How can I display the file name and an icon? Can I save it to database?
Here’s some code I use to upload a file and save it to storage:
uploadReferenceField.addFileUploadSucceedListener(event -> {
FileDescriptor fd = uploadReferenceField.getFileDescriptor();
try {
// save file to FileStorage
fileUploadingAPI.putFileIntoStorage(uploadReferenceField.getFileId(), fd);
} catch (FileStorageException e) {
throw new RuntimeException(“Error saving file to FileStorage”, e);
}
// save file descriptor to database and assign it to “content” property
getItem().setReferenceFileContents(dataManager.commit(fd));
txtReferenceFile.setValue(uploadReferenceField.getFileName());
showNotification("Uploaded file: " + uploadReferenceField.getFileName(), NotificationType.HUMANIZED);
});
Not sure I understand, but once the files are uploaded, they are stored in the external storage. You can see them by going to Administration -> External Files.
Also, when you upload them, you should be getting a file descriptor. It is that file descriptor that you save to your entity if your entity expects a file descriptor. But you can always get the files from storage by doing something like this:
fileStorageService.loadFile(fileDescriptor);
very easy. When you create an entity, let’s say “MyFiles”.
MyFiles entity will have File1 and File2 attributes to store the files. You would need to set them as File Descriptor.
To do that, when you create File1 atrribute, choose the Attribute Type to be ASSOCIATION, type File Descriptor, Cardinality MANY-TO-ONE.
The following lines are red, can not resolve…
getItem().setReferenceFileContents(dataManager.commit(fd));
txtReferenceFile.setValue(uploadReferenceField.getFileName()