How do I execute (Vector) Storage.getInstance().readObject(filePath); in Java 8 Swing?
15:21 29 Nov 2021

Latest Simulator, Java Swing project.

I want to execute Vector a1 = (Vector) Storage.getInstance().readObject(filePath); in a Java Swing application on Windows 10. I tried to import CodenameOne.jar in Swing package however, I get a null pointer exception in Storage.getInstance(). Is there a way to execute this in Swing?

I did not init the display, however Display.init(Object m) requires an object argument and the Init method is deprecated. Can you please provide me the codenameone Display dependencies and Java Swing code to initialize Display in order to execute Storage.getInstance().readObject(filePath)?

Passing init(working directory) solved the exception. Here is the code to allow me to execute Storage.getInstance().readObject(filePath):

String filePath = incSrv.Pwd();// gets working directory
try {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            Display.init(filePath);
            String fileName = "A1-MMA.properties";
            Vector a1 = (Vector) Storage.getInstance().readObject(filePath);
        }
    });
} catch (Exception e) {
}

However, I am left with a blacked out form that appears modal. How can I avoid or dispose it?

I am creating a workaround for serializing Vector in Codenameone. I save Vector to file using Storage.getInstance().writeObject(Path, Vector). I convert the file to bytes and write it to the Swing server VIA socket. Using Storage.getInstance().readObject(file) on the Swing server I have deserialised the object into the vector from my app. This is more efficient than the method I use to deliver complex vectors from the app to the Swing server.

Do you see a red flag with this workaround? Like the ability to Storage.getInstance().readObject(file) on the Swing server may go away?

Storage.getInstance().readObject(file) // (A1ServiceSrv.java:571)
Caused this Exception:
java.lang.NullPointerException
at com.codename1.io.Storage.init(Storage.java:89)
at com.codename1.io.Storage.getInstance(Storage.java:112)
at Main.A1ServiceSrv.loadVectorFromFile(A1ServiceSrv.java:571)

I am including in my class path CodenameOne.jar with the update of 12/11/2021 after CN1 refresh and get the same null pointer exception passing "C:\Src1\A1-Arms\A1-Server\A1-MMA.properties" (absolute path). I also tried "A1-MMA.properties" however, I don't think Codenameone knows where my home path is since it is not initializing it as with Display.init("Current Working Directory where files reside");. Stack trace without calling Display.init:

java.lang.NullPointerException  at

com.codename1.ui.Display.getResourceAsStream(Display.java:3086)
        at com.codename1.io.Log.print(Log.java:327)
        at com.codename1.io.Log.logThrowable(Log.java:299)
        at com.codename1.io.Log.e(Log.java:285)
        at com.codename1.io.Storage.readObject(Storage.java:271)
        at Main.A1ServiceSrv.loadVectorFromFile(A1ServiceSrv.java:596)

            vector = (Vector) Storage.getInstance().readObject(filePath); // (A1ServiceSrv.java:596)
java swing codenameone