简体   繁体   中英

Java serializable object to a file

I have a Swing Program. I am having trouble to save entire main class to a file.

public class GreenHouseMain extends JFrame implements ActionListener,
        MouseListener, Runnable, WindowListener, KeyListener, Serializable
{
//.....other components
static GreenHouseMain ghMain;
}
public static void main(String[] args)
    {
        ghMain = new GreenHouseMain();
    }

public void startEvents()
    {
        suspended = false;
        terminate = false;
        jbStart.setEnabled(false);
        worker = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    //Other Code
                } catch (ControllerException e)
                {
                    try
                    {
                        Date now = new Date();
                        String log = "";
                        PrintWriter out = new PrintWriter(new BufferedWriter(
                                new FileWriter("error.log")));
                        if (e.getMessage() == "Unknown Windows Malfuction")
                        {
                            log = "ErrorCode=1, WindowMalfunction," + now;
                        } else
                        {
                            log = "ErrorCode=2, PowerOut," + now;
                        }
                        out.println(log);
                        jTextArea.append(log + "\n");
                        out.close();
                        out.flush();

                        ObjectOutputStream output = new ObjectOutputStream(
                                new FileOutputStream("dump.out"));
                //It failed in here, says "java.lang.NullPointerException
                        output.writeObject(GreenHouseMain.ghMain);

                        output.flush();


                    } catch (IOException ex)
                    {
                        System.out.println(ex.getMessage());
                    }
                }
            }
        });
        worker.start();

    }

Few things you should know: 1. All classes have been implements Serializable interface 2. There several threads in the program (don't know if it is reason for exception 3. I have had Serialized a object to file before with about the same code but in a console app. Don't know why it fails here.

at javax.swing.plaf.basic.BasicScrollPaneUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(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)

This

                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }

should be:

                } catch (IOException ex)  {
                    ex.printStackTrace();
                }

This will give you much better information, most importantly the exact line where the NullPointerException occurs and from where that line is reached. If that doesn't make the cause of the problem obvious, start the program in a debugger and put a breakpoint on that line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM