0

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)
Trocity
  • 11
  • 3
  • 1
    The error shows you the line number with the problem, and the type of problem.. – Steve Smith Apr 26 '19 at 16:01
  • I know that, however I don't know why it's giving me a nullpointerexception, the line its referring to is the bookList.add(bookButton) – Trocity Apr 26 '19 at 16:04
  • can you post LibraryPanel.java? Basically you have an object that is not instantiated properly, thus the NPE. – thefern Apr 26 '19 at 16:07
  • 1
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 3) Always copy/paste error and exception output! – Andrew Thompson Apr 26 '19 at 16:10
  • I just updated it – Trocity Apr 26 '19 at 16:13
  • *"I just updated it"* There's still no MCVE, so we can't match the line numbers in the stack trace to the source code. Go through both links in the 2nd point above. If you cannot solve it from that, post an MCVE. BTW - Tip: Add @SteveSmith (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson Apr 26 '19 at 16:16
  • `JPanel bookList = new JPanel();` is declaring and initializing a **local vairable** so when accessing the field, despite having the same name as the variable, it is still not initialized, that is `null` (a good IDE should have the option to warn when a variable is hiding a field) – user85421 Apr 26 '19 at 16:41
  • @CarlosHeuberger in that case how would I go about using it within the action listener? – Trocity Apr 26 '19 at 17:11
  • just `bookList = new JPanel();` that is, do not declare a local variable, initialize the field instead (just in the constructor) like you are already doing with `library`, `fileName`, ... – user85421 Apr 26 '19 at 17:18
  • @CarlosHeuberger, works, thanks I appreciate the help – Trocity Apr 26 '19 at 17:25

1 Answers1

0

Dude, the line

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

says that line 86 contains an UNinstantiated object. Check this line and you'd know which Object was "unstantiated" or got a NULL return.