Update JLabel text error

shakarn7

I'm having some problems updating JLabel text. I have a AddCollection JDialog that calls it's controller and then updates the main view statusbar. However, when I apply the same technique to my AddHolding JDialog and controller I am getting a Null Pointer Exception. I have been at it for hours now, but can't see where I am going wrong. Is it something to do with my chaining?

Below is my code that relates to the problem at hand.

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();
        }
    }
}
Ruchir Baronia

The problem is probably in all the methods where you are doing JLabel.setText. You probably need to first call the initialization method before setting the text. A NullPointerException exception happens when the object you are changing is null, and if you are getting a NullPointerException when you are changing the text of a JLabel, it means your JLabel is null.

I noticed you are using getters to initialize your objects...Don't... Why getters and setters are evil, but that's a topic that you should get into later. For now, you need to initialize your JLabel!

Look, there is a big difference between declaring and initializing variables. Here is the simplist way I can put it:

Object imAnObject; //Declaring

Now, because you didn't do:

Object imAnObject = new Object();  //Initializing

Then, imAnObject is still null! So obviously there will be a NullPointerException.

How to in initialize your JLabels:

So, right now, you only declare it by doing the following:

  private JLabel jlCode, jlTitle;

But you never initialize jlCode or jlTitle. So, somewhere before you try changing the text

You need to initialize it. How?

jlCode = new JLabel();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related