I'm having issues with ActionPerformed() functionality in a JFrame. Eclipse thinks everything looks fine, and the GUI loads fine, but I'm getting the following error if I press ANY button. I've removed code for all but basic function to ensure the problem isn't in my other code.
For the example below, I pressed exitBnt. If I take out line 75 and replace it with a call to a method in another class, which I've proven to work in a console-based implementation, I still get the same result. If I remove line 75,I get no error, but the exitBnt button still doesn't function. Ideas?
For the example below, I pressed exitBnt. If I take out line 75 and replace it with a call to a method in another class, which I've proven to work in a console-based implementation, I still get the same result. If I remove line 75,I get no error, but the exitBnt button still doesn't function. Ideas?
Code:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at CollegeTesterGUI.actionPerformed(CollegeTesterGUI.java:75)
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.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)
Code:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class CollegeTesterGUI extends JFrame implements ActionListener {
CollegeTesterGUI startGUI;
College myCollege;
private JTextArea recordMsg;
private JButton aStudentBnt;
private JButton exitBnt;
public static void main(String[] args) {
@SuppressWarnings("unused")
CollegeTesterGUI startGUI = new CollegeTesterGUI("College");
@SuppressWarnings("unused")
College myCollege = new College();
myCollege.addStudent("Zhac");
myCollege.addStudent("Colleen");
myCollege.setCurrentIndex(1);
((Student) myCollege.getStudent()).addCourse("Basketweaving", 4, 3);
((Student) myCollege.getStudent()).addCourse("Physcis", 1, 4);
}
public CollegeTesterGUI(String title) {
//setup screen
setSize(750, 500);
setTitle(title);
//setup menu and add buttons
JPanel menuPanel = new JPanel();
menuPanel.setSize(150, 500);
menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.PAGE_AXIS));
JButton aStudentBnt = new JButton(" ADD STUDENT ");
JButton exitBnt = new JButton(" EXIT PROGRAM ");
aStudentBnt.setAlignmentX(Component.CENTER_ALIGNMENT);
exitBnt.setAlignmentX(Component.CENTER_ALIGNMENT);
menuPanel.add(Box.createRigidArea(new Dimension(0, 20)));
menuPanel.add(aStudentBnt);
menuPanel.add(Box.createRigidArea(new Dimension(0, 5)));
menuPanel.add(exitBnt);
getContentPane().add(menuPanel);
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.PAGE_AXIS));
infoPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
JPanel studentPanel = new JPanel();
studentPanel.setSize(600, 500);
getContentPane().add(studentPanel);
JPanel coursePanel = new JPanel();
coursePanel.setSize(600, 500);
getContentPane().add(coursePanel);
JPanel outputPanel = new JPanel();
outputPanel.setSize(600, 500);
JTextArea recordMsg = new JTextArea(20, 40);
outputPanel.add(recordMsg);
infoPanel.add(studentPanel);
infoPanel.add(coursePanel);
infoPanel.add(outputPanel);
getContentPane().add(infoPanel);
//set screen visible
setVisible(true);
//button listeners
aStudentBnt.addActionListener(this);
exitBnt.addActionListener(this);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent event) {
Object sourceObject = event.getSource();
recordMsg.setText("Testing Add Student Button");
//exit program
if (sourceObject == exitBnt) {
dispose();
System.exit(0);
}
}
}