2016-05-07 6 views
0

JFileChooserを追加して初期化すると、NullPointerExceptionがスローされます。 JFileChooserがなければ、コンパイルして実行するたびに同じコードがうまく実行されます。しかし、JFileChooserを追加すると例外がスローされます。時には、それが正常に実行し、時にはそれがdoesn't.Exceptionis:NullPointerException、時々実行され、例外をスローする場合があります

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

コードバディが必要です。 – ziLk

+0

@zilk編集済み –

答えて

0

私は思うのJFileChooserにはありません関連性を。あなたのコンポーネントの1つが正しくインスタンス化されていないようです。

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

そして、setVisible(true)メソッドを呼び出すことはできません。
とにかく、あなたのコードを含めるべき真の原因を理解する。


更新:あなたのコードが含まれているので、あなたは、コンストラクタでsetVisible方法を使用することはできません。それをinit()メソッドで使用してください。
コンストラクタの実行が終了する前にオブジェクトが正しくインスタンス化されないためです。

+0

オクラホマ ありがとう –

関連する問題