gpt4 book ai didi

java - 使用 Nimbus 外观时 writeObject() 期间出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 04:14:30 26 4
gpt4 key购买 nike

我有 4 个类:TestGUIMainMenuBarMainFrameProjectToAdd。MainFrame 和 ProjectToAdd 实现了可序列化。当我在 MainMenuBar 中单击“新建”>“项目”时,我想创建一个新项目、新的目录,并在该目录中写入可序列化的对象。当对象被写入时,我得到 NullPointerException (请参阅最后的堆栈跟踪)。如果我删除主方法中代码的外观部分,我就不会再收到错误...我正在使用 NetBeans IDE 8.0.2。

测试GUI代码:

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);

}
});
}

}

主菜单栏代码:

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();

}
}
}

主机代码:

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());
}

}
}
}

项目添加代码:

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;
}
}

堆栈跟踪:

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)

最佳答案

抛出 NPE 是因为您尝试序列化 MainFrame,该对象包含大量 GUI 碎片,包括 Nimbus L&F 信息、不应序列化且看起来不可序列化的内容。我怎么知道?将该字段标记为 transient ,看看会发生什么:

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

如果您需要序列化 ​​MainFrame 中保存的信息,则创建一个包含 MainFrame 中关键业务数据的非 GUI 模型类,并序列化该数据,而不是 View (而不是 GUI 本身)。一般来说,无论如何,您几乎不应该序列化 View ,并且从 View 代码中重构程序逻辑始终是一个好主意。

<小时/>

你是对的,这个:

objectOutputStream.writeObject(this);

与 NPE 有关。向您道歉。

关于java - 使用 Nimbus 外观时 writeObject() 期间出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com