NullPointerException,有时会运行,有时会抛出异常

时间:2016-05-07 16:15:07

标签: java

当我添加JFileChooser并初始化它时,它会抛出NullPointerException。没有JFileChooser,每次编译和运行时,相同的代码都运行良好。但是当我添加JFileChooser时,它会抛出异常。有时它会成功运行,有时则不会运行。例外:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.text.PlainView.getPreferredSpan(PlainView.java:233)
at javax.swing.plaf.basic.BasicTextUI$RootView.getPreferredSpan(BasicTextUI.java:1353)
at javax.swing.plaf.basic.BasicTextUI.getPreferredSize(BasicTextUI.java:921)
at     javax.swing.plaf.basic.BasicTextAreaUI.getPreferredSize(BasicTextAreaUI.java:120)
at javax.swing.JComponent.getPreferredSize(JComponent.java:1659)
at javax.swing.JTextArea.getPreferredSize(JTextArea.java:619)
at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:791)
at java.awt.Container.layout(Container.java:1508)
at java.awt.Container.doLayout(Container.java:1497)
at java.awt.Container.validateTree(Container.java:1693)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validateTree(Container.java:1702)
at java.awt.Container.validate(Container.java:1628)
at java.awt.Container.validateUnconditionally(Container.java:1665)
at java.awt.Window.show(Window.java:1033)
at java.awt.Component.show(Component.java:1654)
at java.awt.Component.setVisible(Component.java:1606)
at java.awt.Window.setVisible(Window.java:1014)
at notepad.Notepad.<init>(Notepad.java:66)
at notepad.Notepad.main(Notepad.java:144)

我的代码是:

package notepad;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;


public  class Notepad extends JFrame implements ActionListener{
private JTextArea area;
private ImageIcon frameicon;
private JMenu filemenu;
private JMenu editmenu;
private JMenu formatmenu;
private JMenu helpmenu;
private JScrollPane scroll;
private Font font;
private JMenuBar menubar;
private JMenuItem newmenuitem;
private JMenuItem openmenuitem;
private JMenuItem savemenuitem;
private JMenuItem exitmenuitem;
private int msg;
private int returnVal;
private JFileChooser choose;


public Notepad(){
    initComponents();
    setComponents();

    setTitle("Simple Notepad");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLocation(500, 100);
    setResizable(true);
    setSize(600,600);
    setJMenuBar(menubar);



    menubar.add(filemenu);
    menubar.add(editmenu);
    menubar.add(formatmenu);
    menubar.add(helpmenu);
    filemenu.add(newmenuitem);
    filemenu.add(openmenuitem);
    filemenu.add(savemenuitem);
    filemenu.add(exitmenuitem);


    add(scroll);
    setIconImage(frameicon.getImage());
    setVisible(true);
}


public final void initComponents(){
    area = new JTextArea();
    scroll = new JScrollPane (area, //no need of add textArea when added in        JScrollPane
   JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    menubar = new JMenuBar();
    filemenu = new JMenu("  File");
    editmenu = new JMenu("    Edit");
    formatmenu = new JMenu("    Format");
    helpmenu = new JMenu("    Help");
    newmenuitem = new JMenuItem("    New");
    openmenuitem = new JMenuItem("    Open");
    savemenuitem = new JMenuItem("    Save");
    exitmenuitem = new JMenuItem("    Exit");
    choose = new JFileChooser("E:");
    font = new Font("Calibri",Font.PLAIN,26);
    frameicon = new ImageIcon(getClass().getResource("/res/setting.png"));
}

public final void setComponents(){

    area.setSize(600,600);
    area.setBackground(Color.WHITE);
    area.setFont(font);
    //adding ActionListener
    newmenuitem.addActionListener(this);
    exitmenuitem.addActionListener(this);
    openmenuitem.addActionListener(this);

}
@Override
public void actionPerformed(ActionEvent e){

    //if newmenuitemclicked
   if(e.getSource()==newmenuitem) { 
       if(area.getText()!=""){
          msg = JOptionPane.showConfirmDialog(menubar, "DO you want to save changes?");
           if(msg == JOptionPane.YES_OPTION){
              try {

                  FileOutputStream file = new FileOutputStream("E:\\newdocument.txt");
                  String s = area.getText();
                  byte c[] = s.getBytes();
                  file.write(c);
                  area.setText("");
                  JOptionPane.showMessageDialog(menubar, "File saved as E:\\newdocument.txt");
                  file.close();

              } catch (FileNotFoundException ex) {

              } catch (IOException ex) {

              }
           }
           if(msg == JOptionPane.NO_OPTION){

           }

       }

   }

   if(e.getSource() == exitmenuitem){
       msg = JOptionPane.showConfirmDialog(menubar, "Are you sure you want to exit?");
       if(msg == JOptionPane.YES_OPTION)
       System.exit(0); 

}
   if(e.getSource() == openmenuitem){

   }

}

public static void main(String[] args) {
    Notepad n = new Notepad();
}
}

1 个答案:

答案 0 :(得分:0)

我认为与JFileChooser无关。似乎您的某个组件未正确实例化。

at java.awt.Window.setVisible(Window.java:1014)

您无法调用其setVisible(true)方法 无论如何要实现真正的原因,你应该包括你的代码。

更新:由于您的代码已包含在内,因此您无法在构造函数中使用setVisible方法。在init()方法中使用它 这是因为在构造函数执行结束之前,您的对象无法正确获取实例。