为什么我在初始化程序错误中出现异常?

时间:2016-05-30 18:15:58

标签: java swing nullpointerexception

我试图制作一个简单的电子表格java应用程序。为此,我正在使用JTable。我的代码如下:

import javax.swing.*;
import java.awt.*;

public class Main {

    private JFrame Frame;
    public Font f = new Font("Candara", Font.PLAIN, 14);
    public JFileChooser choicer = new JFileChooser();
    public String dir;
    private JTable Table;
    public String[] columns;
    public String[][] data = new String[10][2];
    public static Main m = new Main();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    m.Frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        Frame = new JFrame();
        Frame.setTitle("Spreadsheet");
        Frame.setBounds(100, 100, 1000, 800);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JMenuBar menuBar = new JMenuBar();
        Frame.setJMenuBar(menuBar);

        JMenu File_Menu = new JMenu("File");
        File_Menu.setFont(f);
        menuBar.add(File_Menu);

        JMenuItem New_Doc = new JMenuItem("New Document");
        New_Doc.setFont(f);
        New_Doc.addActionListener(e -> {
            choicer.setCurrentDirectory(new java.io.File("."));
            choicer.setDialogTitle("New Document");
            choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            choicer.setAcceptAllFileFilterUsed(false);

            if (choicer.showOpenDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
                dir = String.valueOf(choicer.getCurrentDirectory());
            }
        });
        File_Menu.add(New_Doc);

        JMenuItem Save_Doc = new JMenuItem("Save Document");
        Save_Doc.setFont(f);
        File_Menu.add(Save_Doc);

        JMenuItem Open_Doc = new JMenuItem("Open Document");
        Open_Doc.setFont(f);
        File_Menu.add(Open_Doc);
        Frame.getContentPane().setLayout(null);

        m.newTable();
        Table = new JTable(data, columns);
        Table.setBounds(10, 11, 964, 718);
        Table.setFillsViewportHeight(false);
        Frame.getContentPane().add(Table);
        Table.setDragEnabled(false);

    }

    public void newTable() {
        int i = 1;
        columns = new String[10];
        while (i <= 10) {
            columns[i - 1] = String.valueOf(i);
        }

        i = 0;
        int j = 0;
        while (i < 10) {
            while (j < 2) {
                data[i][j] = String.valueOf(i + j);
            }
        }

    }

}

当我运行此代码时,收到以下消息:

  

线程中的异常&#34; main&#34; java.lang.ExceptionInInitializerError   引起:java.lang.NullPointerException
    在Main.initialize(Main.java:76)(这一行是:m.newTable();
    在Main。(Main.java:34)
    在Main。(Main.java:13)

为什么会这样?

此外,我设置表格边界的方式有问题吗?

1 个答案:

答案 0 :(得分:2)

如果尚未分配initialize,则在Main构造函数内部调用m

public Main() {
    initialize();
}

您应该能够使用静态初始化块修复它:

public static Main m;
static{
  m = new Main();
  m.initialize();
}

但从initialize();类构造函数中删除Main。另一个方法是搬家:

  m = new Main();
  m.initialize();

就在之前:

  m.Frame.setVisible(true);