0

I am trying to do a program in which a user will be able to create folios and buy/sell stocks etc. I want to create a save button so the user will be able to save its current folio. The IPortofolioDb is an interface of the PortofolioDb where all the methods for the user data are implemented. I tried to do the follow but it doesnt work and i literally run out of ides. Can anyone help me ?

public class saveFolioListener implements ActionListener {
    private Gui g;
    private IPortfolioDb folios;

    public saveFolioListener(Gui g, IPortfolioDb folios) {
        this.g = g;
        this.folios = folios;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {

        String fileName= "data.txt";
        try {
            ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileName));
            os.writeObject(folios);
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("saved");

    }

}

updated :

    java.io.NotSerializableException: java.util.concurrent.ScheduledThreadPoolExecutor
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
    at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
saved
    at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
    at java.io.ObjectOutputStream.writeObject0(Unknown Source)
    at java.io.ObjectOutputStream.writeObject(Unknown Source)
    at saveFolioListener.actionPerformed(saveFolioListener.java:28)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Andreas
  • 7
  • 1
  • 7
  • "Doesn't work" is a much too imprecise error description. What is the error or output (edit the question to show it as properly formatted text)? – Michael Butscher Nov 22 '18 at 23:09
  • @MichaelButscher sorry i updated with the errors i have, there are a lot :( – Andreas Nov 22 '18 at 23:16
  • Classes to serialize must implement [`Serializable`](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) – Michael Butscher Nov 22 '18 at 23:18
  • @MichaelButscher did it, got new bunch of errors – Andreas Nov 22 '18 at 23:22
  • The object contains a `ScheduledThreadPoolExecutor` which can't be serialized. A quick solution would be [`transient`](https://stackoverflow.com/questions/910374/why-does-java-have-transient-fields) but probably your database class (or the program as a whole) has design problems. – Michael Butscher Nov 22 '18 at 23:29
  • Could you post the IPortfolioDb class? and/or a implementation of this one? – bsaverino Nov 23 '18 at 01:49

0 Answers0