-2

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
Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
xCasper
  • 123
  • 10
  • 1
    `I have decided to try and use the Swing designer to make it look better` - not a good choice. The designer if not very good and is using a null layout which is NOT a good idea. Swing was designed to be used with layout managers. – camickr Jun 19 '14 at 03:58
  • Can you provide the `ErrorChecker` class? – MadProgrammer Jun 19 '14 at 04:02
  • Hrmm camickr, that's weird, I didnt notice that. It is set to absolute when look at the property panel, but it says null in the actual code. I will have to look and see if that's the issue. – xCasper Jun 19 '14 at 04:02
  • Absolute layout == null layout – MadProgrammer Jun 19 '14 at 04:03
  • Member `private ErrorChecker ec` is never initialized, hence the `NullPointerException` – tenorsax Jun 19 '14 at 04:03
  • MadProgrammer, do you want the entire errorcheck class? I provided the function that is being called when the error occurs. I can provide if you wish. – xCasper Jun 19 '14 at 04:04
  • 1
    OH DUH! Thank you so much Aqua!! – xCasper Jun 19 '14 at 04:04
  • I only wanted to be sure of my answer as you seem to think it's in a different location, but I can't see that been true based on my testing ;) – MadProgrammer Jun 19 '14 at 04:06
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Oleg Estekhin Jun 19 '14 at 05:58

1 Answers1

2

Simply put, you never initialise ec, it is null when the ActionListener is called

I suspect where you "think" the error is occurring and where it actually is occurring are two different things

While you have this error, it would be a great opportunity to learn how to use the debugger. Create a breakpoint just above where the code is failing and run the program in debug mode and inspect the start of your variables

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366