0

I was trying to make a simple Spreadsheet java app. For this I am using JTable in swing. My code is as follows:

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

    }

}

When I run this code, I get the following message:

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.NullPointerException
at Main.initialize(Main.java:76) (this line is: m.newTable();)
at Main.(Main.java:34)
at Main.(Main.java:13)

Why does this happen?

Additionally, is there something wrong in the way in which I'm setting the bounds for the Table?

Matt
  • 74,352
  • 26
  • 153
  • 180
John Smith
  • 37
  • 1
  • 12
  • why are you implementing the class in a singleton-way and afterwards using it as normal class? –  May 30 '16 at 18:18
  • @Paul what exactly do you mean by that? – John Smith May 30 '16 at 18:19
  • i and j not incrementing – Farrukh Faizy May 30 '16 at 18:20
  • @MuhammadFarrukhFaizy fixed it. What about this exception? – John Smith May 30 '16 at 18:31
  • @JohnSmith a singleton is a class that provides access to a single instance of itself and holds that instance as a `static` variable ([Singleton Pattern](https://en.wikipedia.org/wiki/Singleton_pattern)). This pattern is used to restrict access to a single instance and includes preventing the creation of new instances. On the other hand your code allows direct access to the constructor. This doesn't exactly make sense. –  May 30 '16 at 18:35
  • Please do not edit your question to include a new problem. Instead, create a new question. – Matt Jun 08 '16 at 12:09

1 Answers1

2

You call initialize inside of Main constructor, when m is not yet assigned:

public Main() {
    initialize();
}

you should be able to fix it with static initialization block:

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

but remove initialize(); from Main class constructor. Another aproach would be to move :

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

just before:

  m.Frame.setVisible(true);
marcinj
  • 48,511
  • 9
  • 79
  • 100