我在更新JLabel文本時遇到了一些問題。我有一個AddCollection JDialog,它調用它的控制器,然後更新主視圖狀態欄。但是,當我將相同的技術應用到我的AddHolding JDialog和控制器時,我得到一個空指針異常。我現在已經呆了好幾個小時了,但是不知道我哪裏出了問題。這與我的鏈接有關嗎?更新JLabel文本錯誤
下面是我的代碼,涉及到手頭的問題。
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();
}
}
}
請將您的代碼降低到最小(即[Short但可運行的示例](http://stackoverflow.com/help/mcve)),我們仍然可以看到該錯誤。併發布堆棧跟蹤。 – Frakcool
*但是,當我將相同的技術應用到我的AddHolding JDialog和控制器時,我得到一個空指針異常。*您忘記在此處放置該代碼(即'AddHolding'代碼)。 – Frakcool
我的答案是否有效?如果沒有,請隨時問我任何問題。如果確實如此,請確保將其標記爲最佳答案! :) –