I am working on a grade program for my class in school. I have decided to try and use the Swing designer to make it look better (have a ugly, but working, version already). Anyway, I am trying to implement the score functionality (which works on the ugly version), but when I click the button I get an extremely long error (see below code):
Here is my code from the one done in the designer - I marked the area where im being told the program fails (See the comment with !!!!!!):
package Week4;
import java.awt.BorderLayout;
public class Grade extends JFrame {
private JPanel contentPane;
private JTextField fldStuName;
private JTextField fldStuID;
private JTextField fldScores;
private boolean errorExist;
private int scores;
private ErrorChecker ec;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Grade frame = new Grade();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Grade()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 325, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblStuName = new JLabel("Enter Student Name:");
lblStuName.setBounds(10, 35, 109, 14);
contentPane.add(lblStuName);
fldStuName = new JTextField();
fldStuName.setBounds(129, 32, 139, 20);
contentPane.add(fldStuName);
fldStuName.setColumns(10);
JLabel lblStuID = new JLabel("Enter Student ID:");
lblStuID.setBounds(10, 73, 109, 14);
contentPane.add(lblStuID);
fldStuID = new JTextField();
fldStuID.setBounds(129, 70, 139, 20);
contentPane.add(fldStuID);
fldStuID.setColumns(10);
JLabel lblScores = new JLabel("Enter Scores:");
lblScores.setBounds(10, 111, 106, 14);
contentPane.add(lblScores);
fldScores = new JTextField();
fldScores.setBounds(129, 108, 139, 20);
contentPane.add(fldScores);
fldScores.setColumns(10);
JButton btnScore = new JButton("Score");
btnScore.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String scoreString;
scoreString = fldScores.getText();
//proof that scoreString is getting the text
System.out.println(scoreString);
if(!scoreString.equals(""))
{
errorExist = ec.DoubleParseChecker("2.0");
errorExist = ec.DoubleParseChecker(scoreString); // why does this crash program
if(!errorExist)
{
double tempNum = Double.parseDouble(scoreString);
errorExist = ec.RangeChecker(tempNum, 0, 100);
if(!errorExist)
{
scores += tempNum;
JOptionPane.showMessageDialog(null, scores);
}
}
}
else
{
fldScores.requestFocus();
//also try fldScores.setText("");
}
}
});
btnScore.setBounds(129, 140, 89, 23);
contentPane.add(btnScore);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
btnCalculate.setBounds(10, 203, 89, 23);
contentPane.add(btnCalculate);
JButton btnClear = new JButton("Clear");
btnClear.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
btnClear.setBounds(109, 203, 89, 23);
contentPane.add(btnClear);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
btnExit.setBounds(208, 203, 89, 23);
contentPane.add(btnExit);
}
}
Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Week4.Grade$2.actionPerformed(Grade.java:98) 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$200(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$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.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$1.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)
Here is my custom error checker code (which is what is supposedly causing the issue:
public boolean DoubleParseChecker(String value)
{
//Declare local variables
boolean errorFlag = false;
JOptionPane.showMessageDialog(null, "Double Parse Checker Ran!");
try
{
double tempNum = 0;
//attempt to convert the string to an int
tempNum = Double.parseDouble(value);
}
catch(NumberFormatException ne)
{
errorFlag = true;
JOptionPane.showMessageDialog(null, "Error: Double parse error", "Error", JOptionPane.ERROR_MESSAGE);
}
//returns if the string was able to be converted to int. True = yes. False = no.
return errorFlag;
}//end DoubleParseChecker