0

I have 4 classes: TestGUI, MainMenuBar, MainFrame and ProjectToAdd. MainFrame and ProjectToAdd implements Serializable. When I click on New>Project in MainMenuBar I want to create a new project, new dir for it and in that dir to write a serializable object. When the object has been writen I get NullPointerException (see stacktrace at the end). If I remove Look and Feel part of code in main method I do not get error any more... I'm using NetBeans IDE 8.0.2.

TestGUI code:

package testgui;

import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

public class RiskManagerGUI {

    public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                try {
                    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                        if ("Nimbus".equals(info.getName())) {
                            UIManager.setLookAndFeel(info.getClassName());
                            break;
                        }
                    }
                } catch (Exception e) {
                    // If Nimbus is not available, fall back to cross-platform
                    try {
                        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                    } catch (Exception ex) {
                        // not worth my time
                    }
                }

                new MainFrame().setVisible(true);

            }
        });
    }

}

MainMenuBar code:

package menu_package;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import testgui.MainFrame;

public class MainMenuBar extends JMenuBar{

    private MainFrame mainFrame;

    public MainMenuBar(MainFrame mainFrame){

        this.mainFrame = mainFrame;

        JMenu fileMenu = new JMenu("File");
        JMenu newMenu = new JMenu("New");

        JMenuItem projectMenuItem = new JMenuItem("Project");  
        projectMenuItem.addActionListener(new AddNewProject());

        newMenu.add(projectMenuItem);

        fileMenu.add(newMenu);

        add(fileMenu);
    }

    class AddNewProject implements ActionListener {
    public void actionPerformed(ActionEvent event) {

            mainFrame.addNewProject();

    }
    }
}

MainFrame code:

package testgui;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.border.*;
import left_panel_package.ProjectsJTree;
import menu_package.MainMenuBar;

public class MainFrame extends JFrame implements Serializable{

    private String instalationLocation;
    private ArrayList <ProjectToAdd> listOfProjects;

    public MainFrame(){
        initComponents();
    }

    public void initComponents() {


        setInstalationLocation("C:\\Users\\Pera\\Desktop\\RiskManagerGUI");
        listOfProjects = new ArrayList<ProjectToAdd>();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("TEST");
        setName("mainFrame");

        JPanel mainPanel = new JPanel();

        setContentPane(mainPanel);
        setJMenuBar(new MainMenuBar(this));

        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        int width = gd.getDisplayMode().getWidth();
        int height = gd.getDisplayMode().getHeight();
        setSize(width, height);
        setLocationRelativeTo(null);
    }

    public String getInstalationLocation() {
        return instalationLocation;
    }

    private void setInstalationLocation(String instalationLocation) {
        this.instalationLocation = instalationLocation;
    }

    public void addNewProject(){
        String nameOfProject = JOptionPane.showInputDialog(null, "Enter the name of new project: ", "Add new project",JOptionPane.OK_CANCEL_OPTION);
        if ((nameOfProject != null) && (nameOfProject.length() > 0)) {
            try {
                ProjectToAdd newProject = new ProjectToAdd(this, nameOfProject);
                listOfProjects.add(newProject);

                File theDir = new File(newProject.getProjectPath());
                theDir.mkdirs();

                newProject.writeToFile();

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

        }   
    }
}

ProjectToAdd code:

package testgui;


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ProjectToAdd implements Serializable{

    private String nameOfProject;
    private MainFrame mainFrame;
    private String projectPath;

    public ProjectToAdd (MainFrame mainFrame, String nameOfProject) {

            this.mainFrame = mainFrame;
            setNameOfProject(nameOfProject);
            setProjectPath(mainFrame.getInstalationLocation()+"\\Projects\\"+nameOfProject);

    }

    public String getNameOfProject() {
        return nameOfProject;
    }

    public void setNameOfProject(String nameOfProject) {
        this.nameOfProject = nameOfProject;
    }

    public void writeToFile() throws IOException{
        ObjectOutputStream objectOutputStream = 
            new ObjectOutputStream(new FileOutputStream(projectPath+
                    "\\"+nameOfProject+".bin"));
        objectOutputStream.writeObject(this);
    }
    public void readFromFile(){

    }

    public String getProjectPath() {
        return projectPath;
    }

    public void setProjectPath(String projectPath) {
        this.projectPath = projectPath;
    }
}

Stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.plaf.synth.SynthLookAndFeel.paintRegion(SynthLookAndFeel.java:371)
    at javax.swing.plaf.synth.SynthLookAndFeel.update(SynthLookAndFeel.java:335)
    at javax.swing.plaf.synth.SynthRootPaneUI.update(SynthRootPaneUI.java:119)
    at javax.swing.JComponent.paintComponent(JComponent.java:780)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Dusan Cankovic
  • 159
  • 1
  • 12
  • 1
    Pleas post the stacktrace. – hotzst Nov 01 '15 at 14:49
  • @Tunaki, I know what is NullPointerException but I just don't see it where have I made a mistake in my code. I've been through it for hours and I just don't see it. If you see it at first glance, than you are far more experienced than me, and you can help me in just a few minutes... Marking question as duplicate and pointing to NullPointerException general explanation is not constructive and not helpfull for me, as a java noob... – Dusan Cankovic Nov 01 '15 at 15:11
  • 1
    Why do you think that, "`I get NullPointerException on objectOutputStream.writeObject(this); in ProjectToAdd class"` this is where the NPE is thrown? There's no mention of the ProjectToAdd class anywhere in your stacktrace. I have to ask if you're starting your GUI on the EDT or not? – Hovercraft Full Of Eels Nov 01 '15 at 15:12
  • If you knew what `NullPointerException` was, you wouldn't say you have it on `objectOutputStream.writeObject(this);` because that is clearly not possible from the code you have. So I do think you need to read the duplicate question and try to minimize the relevant code. Find out what is `null`, then why. – Tunaki Nov 01 '15 at 15:15
  • @Hovercraft Full Of Eels, my bad. NPE was not on writeObject(). When I checked line by line and commented them, this was the line that was causing the NPE, so I thought that it was that line and that something funky was printed in stacktrace. I was right about one part :) Something funky is going on. I've edited the question. It looks like Nimbus look and feel is generating NPE when I use writeObject(). Could not find anything about it... Sorry for initial bad question. – Dusan Cankovic Nov 01 '15 at 16:10
  • 1
    I've already told you something that you must do to make sure that Nimbus doesn't throw the exception: **start your GUI and set the Look & Feel on the EDT**. Consider trying this. – Hovercraft Full Of Eels Nov 01 '15 at 16:12
  • @Tunaki, I've eddited the question. It looks like NPE is thrown because of Nimbus Look and Feel, when **writeObject(this)** is executed. When I comment LaF part of code everything works fine... Sorry for initial incomplete question and thanks for your help :) – Dusan Cankovic Nov 01 '15 at 16:14
  • FYI: EDT == Event Dispatch Thread, the main Swing event thread. – Hovercraft Full Of Eels Nov 01 '15 at 16:25
  • @Hovercraft Full Of Eels, changed code in TestGUI code at the top. Same error appears. – Dusan Cankovic Nov 01 '15 at 17:00
  • @Tunaki, please remove duplicate. I've edited the question and I have added relevant code. Thanks. – Dusan Cankovic Nov 01 '15 at 17:31
  • I think you should clean-up the question by removing all the non relevant code and creating a simple MCVE. – Tunaki Nov 01 '15 at 17:33
  • @Tunaki, I have removed all non relevant code. Code is now minimal and easy to navigate through. – Dusan Cankovic Nov 01 '15 at 17:54
  • 1+ for improvements to the code and the question. – Hovercraft Full Of Eels Nov 01 '15 at 18:28
  • @Tunaki: FYI, the question actually is unrelated to the dup used, and so I've re-opened the question and have answered it. – Hovercraft Full Of Eels Nov 01 '15 at 18:30
  • 1
    @HovercraftFullOfEels Yes, it seems the root cause was found. – Tunaki Nov 01 '15 at 18:31

1 Answers1

2

The NPE is being thrown because you're trying to serialize MainFrame, an object which contains much GUI detritus, including the Nimbus L&F information, stuff that shouldn't be serialized and looks to be non-serializable. How do I know? Mark that field transient and see what happens:

class ProjectToAdd implements Serializable {
    private static final long serialVersionUID = 1L;
    private String nameOfProject;
    private transient MainFrame mainFrame;  // **********
    private String projectPath;

If you need to serialize information that's held within the MainFrame, then create a non-GUI model class that contains the key business data from MainFrame and serialize that, not the view -- not the GUI itself. You should in general almost never serialize the view regardless, and it's always a good idea to refactor your program logic out of your view code.


and you were right, this:

objectOutputStream.writeObject(this);

was related to the NPE. Apologies to you.

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346