gpt4 book ai didi

java - Button ActionListener 导致大量错误,我不知道为什么

转载 作者:行者123 更新时间:2023-12-02 06:16:06 25 4
gpt4 key购买 nike

我目前正在开发一个显示书籍文本的程序。然而,我首先需要通过 Action 监听器向滚动面板内部的面板添加按钮以调用这些书籍对象。然而,我的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)

最佳答案

伙计,线

at LibraryPanel$MyjButtonActionListener.actionPerformed(LibraryPanel.java:86)

表示第 86 行包含一个未实例化的对象。检查这一行,您就会知道哪个对象“未实例化”或得到了 NULL 返回。

关于java - Button ActionListener 导致大量错误,我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55871175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com