I'm having an issue with my code and after hours of time I can't figure out what I'm doing wrong. There are three classes (GUI, Judging, TestGUI). All programs compile correctly with no syntax errors. The GUI creates an interface that should compute (JButton) a total of 8 scores and subtract the min and max, and also should reset (JButton) all the JTextFields when pressed. When running the program and pressing either the reset or compute JButtons I'm getting several errors and it is not functional. Please help me with my code.
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class GUI extends JFrame implements ActionListener
{
private final int FRAME_WIDTH = 775;
private final int FRAME_HEIGHT = 300;
private final int X_ORIGIN = 100;
private final int Y_ORIGIN = 400;
private JTextField [] judges;
private JLabel [] labels;
JButton btnReset, btnCompute;
private String coName;
private JTextField contestant, scr, s1,s2,s3,s4,s5,s6,s7,s8,s9;;
private JLabel label;
double score_a, scores[], sc1;
public GUI()
{
super("Judging");
setLayout(new FlowLayout());
setSize(FRAME_WIDTH,FRAME_HEIGHT);
setLocation(X_ORIGIN,Y_ORIGIN);
label = new JLabel("Name of Contestant", JLabel.CENTER);
contestant = new JTextField(18);
contestant.setEditable(true);
contestant.setBackground(Color.white);
add(label);
add(contestant);
JTextField [] judges = new JTextField[8];
JLabel [] labels = new JLabel[8];
for(int i = 0;i < judges.length;i++)
{
labels[i] = new JLabel("judge" +(i+1), JLabel.LEFT);
judges[i] = new JTextField(3);
labels[i].setVerticalAlignment(JLabel.TOP);
judges[i].setBackground(Color.white);
judges[i].setForeground(Color.black);
judges[i].addActionListener(this);
judges[i].setEditable(true);
add(labels[i]);
add(judges[i]);
}
btnCompute = new JButton("Compute Score");
btnCompute.setBackground(Color.black);
btnCompute.setForeground(Color.white);
btnCompute.addActionListener(this);
add(btnCompute);
label = new JLabel("Contestant's Score", JLabel.LEFT);
label.setVerticalAlignment(JLabel.TOP);
scr = new JTextField(10);
scr.setEditable(false);
scr.setBackground(Color.white);
add(scr);
add(label);
label = new JLabel("Reset Scores for Next Contestant", JLabel.LEFT);
btnReset = new JButton("Reset");
btnReset.setBackground(Color.gray);
btnReset.setForeground(Color.red);
btnReset.addActionListener(this);
add(btnReset);
}
public void setScores(double [] scores)
{
this.score_a = score_a;
}
public double [] getScores()
{
return scores;
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source == btnCompute)
{
s1.setEditable(true);
s2.setEditable(true);
s3.setEditable(true);
s4.setEditable(true);
s5.setEditable(true);
s6.setEditable(true);
s7.setEditable(true);
s8.setEditable(true);
}
else if (source == btnReset)
{
s1.setText("");
s1.setEditable(true);
s2.setText("");
s3.setText("");
s4.setText("");
s5.setText("");
s6.setText("");
s7.setText("");
s8.setText("");
contestant.setText("");
s1.setEditable(true);
s2.setEditable(true);
s3.setEditable(true);
s4.setEditable(true);
s5.setEditable(true);
s6.setEditable(true);
s7.setEditable(true);
s8.setEditable(true);
contestant.setEditable(true);
}
}
}
The errors below are a few of the several lines I receive when pressing the compute JButton. I'm thinking it may be because a variable is declared wrong or I haven't called a class correctly but I can't find my error.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.actionPerformed(GUI.java:104)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3255)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)