当前位置: 首页 > 知识库问答 >
问题:

线程“AWT-EventQueue-0”NumberFormatException中出现异常:空字符串

马欣德
2023-03-14

和代码

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);
                }

            }
        }); 
    }

} 

共有1个答案

岳俊晖
2023-03-14

当出现错误时,只需遵循StackTrace并查看包含由您创建的类的第一行,在本例中:

coursework.coursework$2.ActionPerformed(coursework.java:135)

在尝试将字符串转换为数字时,看起来您有NumberFormatException。

 类似资料: