更新JLabel文本错误

时间:2015-11-14 00:43:40

标签: java jlabel

我在更新JLabel文本时遇到了一些问题。我有一个AddCollection JDialog调用它的控制器,然后更新主视图状态栏。但是,当我将相同的技术应用于我的AddHolding JDialog和控制器时,我得到一个空指针异常。我现在已经好几个小时了,但不知道我哪里出错了。这与我的链接有关吗?

以下是与我们手头的问题有关的代码。

public class LMSDriver 
{
    public static void main(String[] args)
    {
        LMSModel model = new LMSFacade();
        AddCollectionPanel colPanel = new AddCollectionPanel(model);
        colPanel.setVisible(true);
    }
}


public class AddCollectionPanel extends JDialog 
{   
    private LMSModel model;
    private AddCollectionController controller;
    private LibraryView view;

    private Box mainBox, hBox1, hBox2, hBox3;
    private JLabel jlCode, jlTitle;
    private JButton submitBtn;
    private JTextField codeField;
    private JTextField titleField;

    public AddCollectionPanel(LMSModel model) 
    {       
        super();
        this.model = model;
        this.controller = new AddCollectionController(this);

        setTitle("Add Collection");
        setSize(300,160);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);

        mainBox = Box.createVerticalBox();
        hBox1 = Box.createHorizontalBox();
        hBox2 = Box.createHorizontalBox();
        hBox3 = Box.createHorizontalBox();
        jlCode = new JLabel("Collection Code: ");
        jlTitle = new JLabel("Collection Title:  ");
        codeField = new JTextField(10);
        titleField = new JTextField(10);            
        submitBtn = new JButton("Submit");  

        submitBtn.addActionListener(controller);

        hBox1.add(jlCode);
        hBox1.add(Box.createHorizontalStrut(12));
        hBox1.add(codeField);
        hBox2.add(jlTitle);
        hBox2.add(Box.createHorizontalStrut(10));
        hBox2.add(titleField);
        hBox3.add(Box.createHorizontalStrut(120));
        hBox3.add(submitBtn);

        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox1);
        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox2);
        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox3);

        add(mainBox);

        setVisible(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 

        this.view = new LibraryView(model);
    }

    public String getCodeField()
    {
        String codeText = new String(codeField.getText());
        return codeText;
    }

    public void clearCodeField()
    {
        codeField.setText("");
    }

    public String getTitleField()
    {
        String titleText = new String(titleField.getText());
        return titleText;
    }

    public void clearTitleField()
    {
        titleField.setText("");
    }

    public JButton getSubmitBtn() 
    {
        return submitBtn;
    }

    public LMSModel getModel() 
    {
        return model;
    }

    public AddCollectionController getController()
    {
        return controller;
    }

    public LibraryView getView()
    {
        return view;
    }
}


public class AddCollectionController implements ActionListener 
{
    private AddCollectionPanel colPanel;
    private LMSModel model;
    private LibraryCollection lib;

    private JLabel colCode;
    private JLabel totalBooks;
    private JLabel totalVideos;

    public AddCollectionController(AddCollectionPanel collectionPanel) 
    {
        this.colPanel = collectionPanel;
        model = this.colPanel.getModel();
    }

    @Override
    public void actionPerformed(ActionEvent event) 
    {
        if (event.getSource() == colPanel.getSubmitBtn()) 
        {       
            lib = new LibraryCollection(colPanel.getCodeField(), colPanel.getTitleField());
            model.addCollection(lib);
            System.out.println(lib);
            System.out.println(model.getCollection());
            colCode = colPanel.getView().getLibraryStatusbar().getColCode();
            colCode.setText("CollectionCode: " + colPanel.getCodeField() + " | ");
            totalBooks = colPanel.getView().getLibraryStatusbar().getTotalBooks();
            totalBooks.setText("Total Books: " + model.countBooks() + " | ");
            totalVideos = colPanel.getView().getLibraryStatusbar().getTotalVideos();
            totalVideos.setText("Total Videos: " + model.countVideos());
            colPanel.dispose();
        }
    }
}


public class LibraryView extends JFrame
{
    private LMSModel model;
    private LibraryToolbar toolbar;
    private LibraryPanel panel;
    private LibraryStatusbar statusbar;
    private LibraryMenu menu;
    private LibraryViewController controller;
    private AddCollectionPanel addCollectionPanel;
    private AddBookPanel addBookPanel;

    public LibraryView(LMSModel model) 
    {
        this.model = model;
        this.controller = new LibraryViewController(this);

        toolbar = new LibraryToolbar(this);
        panel = new LibraryPanel(this);
        statusbar = new LibraryStatusbar(this);
        menu = new LibraryMenu(this);

        JFrame library = new JFrame("Library");
        library.setSize(1024, 720);
        library.setLayout(new BorderLayout(5,5));
        library.setLocationRelativeTo(null);
        library.add(toolbar, BorderLayout.WEST);
        library.add(menu, BorderLayout.NORTH);
        library.add(panel, BorderLayout.CENTER);
        library.add(statusbar, BorderLayout.SOUTH);    

        library.setVisible(true);
        library.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public LMSModel getModel()
    {
        return model;
    }

    public void setModel(LMSModel model)
    {
        this.model = model;
    }

    public LibraryMenu getLibraryMenu() 
    {
        return menu;
    }

    public LibraryToolbar getLibraryToolbar() 
    {
        return toolbar;
    }

    public LibraryPanel getLibraryPanel() 
    {
        return panel;
    }

    public LibraryStatusbar getLibraryStatusbar() 
    {
        return statusbar;
    }

    public LibraryViewController getController() 
    {
        return controller;
    }

    public void setController(LibraryViewController controller) 
    {
        this.controller = controller;
    }

    public AddCollectionPanel getAddCollectionPanel() 
    {
        return addCollectionPanel;
    }

    public void setAddCollectionPanel(AddCollectionPanel addCollectionPanel) 
    {
        this.addCollectionPanel = addCollectionPanel;
    }

    public AddBookPanel getAddBookPanel() 
    {
        return addBookPanel;
    }

    public void setAddBookPanel(AddBookPanel addBookPanel) 
    {
        this.addBookPanel = addBookPanel;
    }
}



public class AddBookPanel extends JDialog 
{
    private LMSModel model;
    private AddBookController controller;
    private LibraryView view;

    private Box mainBox, hBox1, hBox2, hBox3;
    private JLabel jlCode, jlTitle;
    private JButton addBookBtn;
    private JTextField codeField;
    private JTextField titleField;

    public AddBookPanel(LMSModel model) 
    {   
        super();
        this.model = model;
        this.controller = new AddBookController(this);

        setTitle("Add Book");
        setSize(300,160);
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);

        mainBox = Box.createVerticalBox();
        hBox1 = Box.createHorizontalBox();
        hBox2 = Box.createHorizontalBox();
        hBox3 = Box.createHorizontalBox();
        jlCode = new JLabel("Book Code: ");
        jlTitle = new JLabel("Book Title:  ");
        codeField = new JTextField(10);
        titleField = new JTextField(10);

        addBookBtn = new JButton("Add Book");

        addBookBtn.addActionListener(controller);

        hBox1.add(jlCode);
        hBox1.add(Box.createHorizontalStrut(12));
        hBox1.add(codeField);
        hBox2.add(jlTitle);
        hBox2.add(Box.createHorizontalStrut(10));
        hBox2.add(titleField);
        hBox3.add(Box.createHorizontalStrut(120));
        hBox3.add(addBookBtn);

        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox1);
        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox2);
        mainBox.add(Box.createVerticalStrut(10));
        mainBox.add(hBox3);

        add(mainBox);

        setVisible(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);     
    }

    public String getCodeField()
    {
        String codeText = new String(codeField.getText());
        return codeText;
    }

    public void clearCodeField()
    {
        codeField.setText("");
    }

    public String getTitleField()
    {
        String titleText = new String(titleField.getText());
        return titleText;
    }

    public void clearTitleField()
    {
        titleField.setText("");
    }

    public JButton getAddBookBtn() 
    {
        return addBookBtn;
    }

    public LMSModel getModel() 
    {
        return model;
    }

    public AddBookController getController()
    {
        return controller;
    }

    public LibraryView getView()
    {
        return view;
    }
}



public class AddBookController implements ActionListener {

    private AddBookPanel bookPanel;
    private LMSModel model;
    private int code;
    private LibraryView view;

    private JLabel colCode;
    private JLabel totalBooks;
    private JLabel totalVideos;

    public AddBookController(AddBookPanel bookPanel) {

        this.bookPanel = bookPanel;
        model = this.bookPanel.getModel();
    }

    @Override
    public void actionPerformed(ActionEvent event) 
    {   
        if (event.getSource() == bookPanel.getAddBookBtn()) 
        {
            code = Integer.parseInt(bookPanel.getCodeField().trim());
            Holding h = new Book(code, bookPanel.getTitleField());
            model.addHolding(h);
            System.out.println(h);
            colCode = bookPanel.getView().getLibraryStatusbar().getColCode();
            colCode.setText("CollectionCode: " + bookPanel.getCodeField() + " | ");
            totalBooks = bookPanel.getView().getLibraryStatusbar().getTotalBooks();
            totalBooks.setText("Total Books: " + model.countBooks() + " | ");
            totalVideos = bookPanel.getView().getLibraryStatusbar().getTotalVideos();
            totalVideos.setText("Total Videos: " + model.countVideos());
            bookPanel.dispose();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

问题可能在于您正在进行JLabel.setText的所有方法。在设置文本之前,您可能需要首先调用初始化方法。当您要更改的对象为空时发生NullPointerException异常,如果在更改JLabel的文本时获得NullPointerException,则表示您的JLabel为空。

我注意到你正在使用getter初始化你的对象......不要...... Why getters and setters are evil,但这是你以后应该讨论的话题。目前,您需要初始化JLabel!

看,声明和初始化变量之间存在很大差异。这是我能说的简单方法:

Object imAnObject; //Declaring

现在,因为你没有这样做:

Object imAnObject = new Object();  //Initializing

然后,imAnObject仍为空!所以很明显会出现NullPointerException。

如何初始化JLabel:

所以,现在,您只需通过执行以下操作来声明它:

  private JLabel jlCode, jlTitle;

但您永远不会初始化jlCodejlTitle。因此,在您尝试更改文本之前某处

您需要初始化它。怎么样?

jlCode = new JLabel();