Dominik Banialis
Dominik Banialis

Reputation: 39

Exception in thread "AWT-EventQueue-0" NumberFormatException: empty String

I've got a problem with my GUI. The code is not finished, but it should still work. It gives this error after pressing the read button:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at coursework.Coursework$2.actionPerformed(Coursework.java:135) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6525) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6290) at java.awt.Container.processEvent(Container.java:2234) at java.awt.Component.dispatchEventImpl(Component.java:4881) at java.awt.Container.dispatchEventImpl(Container.java:2292) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462) at java.awt.Container.dispatchEventImpl(Container.java:2278) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4703) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:702) at java.awt.EventQueue$3.run(EventQueue.java:696) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:724) at java.awt.EventQueue$4.run(EventQueue.java:722) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75) at java.awt.EventQueue.dispatchEvent(EventQueue.java:721) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

And code

package coursework;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Coursework extends JFrame  {    
    public static void main(String[] args) {
     JFrame frame=new JFrame("myframe");
     JPanel panel = new JPanel();
     panel.setLayout(new GridLayout(18,1));
    JLabel StudentNameLabel = new JLabel("Student Name: ");
     final JTextField TypeNameArea = new JTextField(" "); 
     JLabel StudentSurnameLabel = new JLabel("Student Surname: ");
      final JTextField TypeSurnameArea = new JTextField(" ");
     JLabel StudentIDLabel = new JLabel("Student ID: ");
     final JTextField StudentIDArea = new JTextField("M00");
     JLabel LabTestLabel = new JLabel("Lab Test: ");
     final JTextField LabTestArea = new JTextField(" ");
     JLabel QuizLabel = new JLabel("Quizes: ");
     final  JTextField QuizArea = new JTextField(" ");
     JLabel ICourseworkLabel = new JLabel("Indiwidual CW: ");
     final JTextField ICourseworkArea = new JTextField(" ");
     JLabel GCourseworkLabel = new JLabel("Group CW: ");
     final JTextField GCourseworkArea = new JTextField(" ");
     panel.setBackground(Color.BLACK);
     StudentSurnameLabel.setForeground(Color.YELLOW);
     StudentNameLabel.setForeground(Color.YELLOW);
     LabTestLabel.setForeground(Color.YELLOW);
     StudentIDLabel.setForeground(Color.YELLOW);
     QuizLabel.setForeground(Color.YELLOW);
     ICourseworkLabel.setForeground(Color.YELLOW);
     GCourseworkLabel.setForeground(Color.YELLOW);
     JButton buttonSubmit = new JButton("Submit");
     buttonSubmit.setBackground(Color.BLACK);
     buttonSubmit.setForeground(Color.WHITE);
     JButton buttonRead = new JButton("Read");
     buttonRead.setBackground(Color.BLACK);
     buttonRead.setForeground(Color.WHITE);
     JButton buttonCancel = new JButton("Cancel");
     buttonCancel.setBackground(Color.BLACK);
     buttonCancel.setForeground(Color.WHITE);
     JButton buttonNext = new JButton("Next");
     buttonNext.setBackground(Color.BLACK);
     buttonNext.setForeground(Color.WHITE);

     panel.add(StudentNameLabel);
     panel.add(TypeNameArea);
     panel.add(StudentSurnameLabel);
     panel.add(TypeSurnameArea);
     panel.add(StudentIDLabel);
     panel.add(StudentIDArea);
     panel.add(LabTestLabel);
     panel.add(LabTestArea);
     panel.add(QuizLabel);
     panel.add(QuizArea);
     panel.add(ICourseworkLabel);
     panel.add(ICourseworkArea);
     panel.add(GCourseworkLabel);
     panel.add(GCourseworkArea);
     panel.add(buttonSubmit);
     panel.add(buttonRead);
     panel.add(buttonCancel);
     panel.add(buttonNext);
     frame.setSize(300, 800);
     frame.add(panel);
     frame.setVisible(true);

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    buttonNext.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                String name;
                String surname;
                String number;
                String gradeLabTest;
                String gradeQuiz;
                String gradeICourswork;
                String gradeGCourswork;
                name = TypeNameArea.getText();
                surname = TypeSurnameArea.getText();
                number = StudentIDArea.getText();
                gradeLabTest = LabTestArea.getText();
                gradeQuiz = QuizArea.getText();
                gradeICourswork = ICourseworkArea.getText();
                gradeGCourswork = GCourseworkArea.getText();
                String line = name + " " + surname + " " + number + " " + gradeLabTest + " " + gradeQuiz + gradeICourswork + gradeGCourswork + "\r\n";
                Writer output;
                try {
                    output = new BufferedWriter(new FileWriter("C:/Hola/test.txt",true));  //clears file every time
                    output.append(line);
                    output.close();
                } catch (IOException ex) {
                    Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }); 

     buttonRead.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                BufferedReader br ;
                try {
                    br = new BufferedReader(new FileReader("C:/Hola/test.txt"));
                    for(String line; (line = br.readLine()) != null; ) {
                    String[] token = line.split(" ");
                    String name = token[0];
                    String surname = token[1];
                    String number = token[2];
                    double g1 = Double.parseDouble(token[3]);
                    double g2 = Double.parseDouble(token[4]);
                    double g3 = Double.parseDouble(token[5]);
                    double g4 = Double.parseDouble(token[6]);

                    double av = 0.4*g1 + 0.2*g2 + 0.2*g3 + 0.2*g4;
                    int grade = 0;
                    String gradestring;




                    String line_input = name + " " + surname + " " + number + " " + av + " " + grade + "\r\n";
                    Writer output;
                    try {
                        output = new BufferedWriter(new FileWriter("C:/Hola/test2.txt",true));  //clears file every time
                        output.append(line_input);
                        output.close();
                    } catch (IOException ex) {
                        Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
                    }


                // line is not visible here.
            }
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Coursework.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }); 
    }

} 

Upvotes: 1

Views: 1528

Answers (2)

fedup
fedup

Reputation: 1259

Rather than trying to determine what is a valid String for conversion to a number, let Java figure it out for you. Catch the exception and do something that is acceptable for your specific application. In your case it is not that the String has invalid characters in it, the token has an empty String "".

double g1 = 0;
try {
    g1 = Double.parseDouble(token[3]);
catch (NumberFormatException e) {
    System.out.println(e.getMessage() + ": " + token[3]);
}

Upvotes: 0

Rafael R. S. Robles
Rafael R. S. Robles

Reputation: 897

When you have an error just follow the StackTrace and look at the first line that contains a class created by you, in this case:

coursework.Coursework$2.actionPerformed(Coursework.java:135)

It looks like you have a NumberFormatException when trying to convert a String to a number.

You have two options, you can verify that String contains a valid number before trying to convert it in the line 136-139. Or make sure that the input in lines 133-135 contain valid numbers.

Remember that empty strings are not valid numbers, and that's a very easy validation you can perform.

Upvotes: 2

Related Questions