我目前正在开发一个显示书籍文字的程序。但是,我首先需要通过动作侦听器将按钮添加到滚动面板内部的面板上,以调用这些书本对象。但是,我用来添加这些按钮的ActionListener
给了我一大堆错误,但我无法对其进行故障排除。
我注意到它说它们是一个空指针异常,但是,我认为我已使侦听器中使用的所有内容实例化并分配了某种值。
book是一个对象,用于存储一本书的所有文本信息。
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
public class LibraryPanel extends JPanel {
private Library library;
private BookButton bookButton;
private JScrollPane jScrollPane;
private JButton jButton;
private JTextField fileName;
private JPanel bookList;
public LibraryPanel ()
{
library = new Library();
JPanel libraryPanel = new JPanel();
libraryPanel.setLayout(new BoxLayout(libraryPanel, BoxLayout.Y_AXIS));
JPanel bookBox = new JPanel();
bookBox.setLayout(new BorderLayout());
JPanel importBooks = new JPanel();
fileName = new JTextField(4);
jButton = new JButton("Load");
jButton.addActionListener(new MyjButtonActionListener());
JPanel bookList = new JPanel();
bookList.setLayout(new BoxLayout(bookList, BoxLayout.Y_AXIS));
importBooks.add(fileName);
importBooks.add(jButton);
setLayout(new BorderLayout());
jScrollPane = new JScrollPane(bookList);
jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
bookBox.add(jScrollPane);
libraryPanel.add(bookBox, BorderLayout.CENTER);
libraryPanel.add(importBooks);
add(libraryPanel);
}
private class MyjButtonActionListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
String something = fileName.getText();
File file = new File(something);
if(file.exists() && file.isFile())
{
library.loadLibraryFromCSV(something);
for(int i = 0;i<library.getBooks().size(); i++)
{
Book book = library.getBook(i);
bookButton = new BookButton(book);
bookList.add(bookButton);
}
}
else
{
fileName.setText("Invalid File");
}
}
}
}
BookButton的代码
public BookButton (Book book)
{
this.book = book;
BookButtonListener clicked = new BookButtonListener();
addActionListener(clicked);
String buttonText;
if (book.getTitle().length() >= 20)
{
buttonText = book.toString().substring(0,19);
}
else
buttonText = book.getTitle();
setToolTipText(buttonText);
}
我调用了actionListener,但是它会导致大量错误。
Results:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at LibraryPanel$MyjButtonActionListener.actionPerformed(LibraryPanel.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
答案 0 :(得分:0)
Dude, 线
at LibraryPanel$MyjButtonActionListener.actionPerformed(LibraryPanel.java:86)
表示第86行包含一个未实例化的对象。检查此行,您将知道哪个对象“未被实例化”或返回NULL。