我有4類:TestGUI,MainMenuBar,大型機和ProjectToAdd。 MainFrame和ProjectToAdd實現了Serializable。當我點擊MainMenuBar中的New> Project時,我想創建一個新項目,爲它創建新目錄,並在該目錄中編寫一個可序列化的對象。當對象被寫入時,我得到NullPointerException(見最後的stacktrace)。如果我在主方法中刪除了外觀和感覺代碼的一部分,我不會再收到錯誤...我正在使用NetBeans IDE 8.0.2。(),而使用Nimbus外觀
TestGUI代碼:
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代碼:
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());
}
}
}
}
ProjectToAdd代碼:
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)
請發佈堆棧跟蹤。 – hotzst
added stacktrace –
@Tunaki,我知道什麼是NullPointerException,但我只是沒有看到它,我在代碼中犯了什麼錯誤。我已經度過了好幾個小時,而我卻沒有看到它。如果你第一眼看到它,比你更有經驗,而且你可以在短短几分鐘內幫助我...... 將問題標記爲重複並指向NullPointerException一般性解釋對我來說並不具有建設性,對我沒有幫助,作爲一個Java noob ... –