I'm currently working on a program that displays text for a book. However I first need to add buttons to a panel inside of a scroll panel via an action listener to call upon these book objects. However, my ActionListener
that I'm using to add these buttons is giving me a large list of errors and I've been unable to troubleshoot it.
I've noticed it says that their is a null pointer exception, however, I thought I made everything used within the listener to be instantiated and assigned a value of some sort.
book is an object that stores all the text information of a 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");
}
}
}
}
Code for 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);
}
I have the actionListener called, however it results in a huge list of errors.
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)