0

I get this error whenever i run my program. Also when pressing some button do not respond according to there listeners(i don't have any idea why!)

  1. I registered all buttons together
  2. All buttons use the same action listener class

but

Only 3rd and 4th buttons respond to listeners but 1st and 2nd doesnt

    Exception in thread "main" java.lang.NullPointerException
at IntergrationPoint.<init>(IntergrationPoint.java:73)
at MainLibrary.main(MainLibrary.java:23)

Below is what brings all these problems . . .(and i think is SSCCE also)

    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import static java.awt.image.ImageObserver.WIDTH;
    import java.sql.SQLException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    import javax.swing.UIManager;

    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */

    /**
     *
     * @author Yunus
     */
    public class IntergrationPoint extends JFrame{
        JPanel tabOneContent;
        JPanel tabOTwoContent;
        JPanel tabThreeContent;
        JPanel tabFourContent;

JTabbedPane intergrationTabs;

public JButton createAccount;
public JButton borrowBook;
public JButton returnBook;
public JButton addBook;
public JButton deleteBook;
public JButton deleteMember;
public JButton displayMembers;
public JButton displayAvailableBooks;
public IntergrationPoint()
{
    setLookAndFeel();
    intergrationTabs = new JTabbedPane();
    setSize(761, 430);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    tabOneContent = bookOperations();
    tabOTwoContent = memberOperations();

    intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Book Operations<body><html>", null,tabOneContent,"");
    intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Member Operations<body><html>", null,tabOTwoContent,"");
    intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Display Members<body><html>", null,memberOperations(),"Display all available member");
    intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Display Books<body><html>", null,memberOperations(),"Display all available book");
    intergrationTabs.addTab("<html><body marginheight=30 marginwidth=20>Login - Logout<body><html>", null,memberOperations(),"Login - Logout");

    add(intergrationTabs);
    setVisible(true);
    setResizable(false);

    ButtonListener listener = new ButtonListener();
    // Register listener with buttons 
    borrowBook.addActionListener(listener);
    returnBook.addActionListener(listener);
    addBook.addActionListener(listener);
    deleteBook.addActionListener(listener);


}

class ButtonListener implements ActionListener {
    @Override /** Implement actionPerformed */
    public void actionPerformed(ActionEvent ev){
        if(ev.getSource() == borrowBook)
            new BorrowBook();
        else if(ev.getSource() == returnBook)
            new ReturnBook();
        else if(ev.getSource() == addBook)
            try {
            new AddBook();
        } catch (SQLException ex) {
            Logger.getLogger(IntergrationPoint.class.getName()).log(Level.SEVERE, null, ex);
        }
        else if(ev.getSource() == deleteBook)
            new DeleteBook();

     }
}


public JPanel bookOperations()
{
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    JPanel insidePanel = new JPanel(new GridBagLayout());

    GridBagConstraints constraints = new  GridBagConstraints();
    constraints.fill =  GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.gridwidth = 1;
    constraints.gridheight = 2;
    constraints.ipadx = 5;
constraints.ipady = 45;

    borrowBook = new JButton("Borrow Book");
    returnBook = new JButton("Return Book");
    addBook = new JButton("Add Book");
    deleteBook = new JButton("Delete Book");

    constraints.gridx = 0;
    constraints.gridy = 0;
    insidePanel.add(borrowBook,constraints);

    constraints.gridx = 0;
    constraints.gridy = 2;
    insidePanel.add(returnBook,constraints);

    constraints.gridx = 0;
    constraints.gridy = 4;
    insidePanel.add(addBook,constraints);

    constraints.gridx = 0;
    constraints.gridy = 6;
    insidePanel.add(deleteBook,constraints);

    panel.add(insidePanel);

    return panel;
}


public JPanel memberOperations()
{
    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));

    JPanel insidePanel = new JPanel(new GridBagLayout());

    GridBagConstraints constraints = new  GridBagConstraints();
    constraints.fill =  GridBagConstraints.BOTH;
constraints.anchor = GridBagConstraints.NORTHWEST;
    constraints.gridwidth = 1;
    constraints.gridheight = 2;
    constraints.ipadx = 0;
constraints.ipady = 45;

    borrowBook = new JButton("Add Member");
    returnBook = new JButton("Delete Member");

    constraints.gridx = 0;
    constraints.gridy = 0;
    insidePanel.add(borrowBook,constraints);

    constraints.gridx = 0;
    constraints.gridy = 2;
    insidePanel.add(returnBook,constraints);

    panel.add(insidePanel);

    return panel;
}


private void setLookAndFeel()
{
    try
    {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

    }catch(Exception ex)
    {
            ex.getMessage();
    }
}
}


    import java.sql.SQLException;
    import ui.BorrowBookUI;
    import ui.ReturnBookUI;

    public class MainLibrary {
        public static void main(String[] args) throws SQLException
        {
            IntergrationPoint intergrationPoint = new IntergrationPoint();
        }

    }
3
  • while trying to form an SSCCE, that error dissappeared! Though the problem i mentioned earlier that two of my four buttons do not respond to listener is still a problem . . . Commented Nov 5, 2013 at 16:29
  • where do you initialize BorrowBook? Commented Nov 5, 2013 at 16:55
  • @MadConan did you mean borrowBook, if yes is in bookOperations() methods Commented Nov 5, 2013 at 17:13

1 Answer 1

1

Buttons are initialized in memberOperations() and bookOperations(). For example you're using the same member borrowBook. But it gets reallocated in every call to memberOperations(). So once you get to the setup of ButtonListener, you are actually setting the listener on the buttons that sit in Login - Logout tab. So if you switch to that tab and hit Add Member then you will trigger BorrowBook() action.

As for the NPE it is hard to say as the posted code is only a fragment, it does not include all the code and unfortunately, does not compile. Please post SSCCE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.