Try/catch和声明的变量



我试着在Try/Catch语句的位置上移动,但如果不取消br或fr变量,我找不到将代码放入两个Try语句的方法

这是我得到的输出:

Suzanne Hilltop 75 88 94 81 91 94 83 88 100 100 84 97 100 96 88 89 96 94 74 97 98 100
Trina Waters 67 47 74 52 78 71 63 68 84 71 72 73 82 80 81 84 78 75 80 88 75 79
null
Exception in thread "main" java.lang.NullPointerException
    at java.io.StringReader.<init>(StringReader.java:50)
    at java.util.Scanner.<init>(Scanner.java:702)
    at CSCE111_textProject.main(CSCE111_textProject.java:26)

名称和编号分别为文件的第2行和第4行,共有5个名称,每个名称有22个分配等级。

我对编程非常陌生,从来没有遇到过它编译但不运行的"运行时"错误。当我问我的助教时,他也没能发现问题。

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;

class CSCE111_textProject {
    public static void main( String [] args) {
        try{
            FileReader Fr = new FileReader( "Students.txt" );
            BufferedReader Br = new BufferedReader( Fr);
            } catch (FileNotFoundException F) {
                System.out.println("I/O Exception");
            }
            String line = Br.readLine();
            try{
            while (line != null) {
                line = Br.readLine();
                System.out.println( line );

                    Scanner parser = new Scanner( line);
                    String firstname = parser.next();
                    String lastname = parser.next();
                    double test1 = parser.nextDouble();
                    double test2 = parser.nextDouble();
                    double test3 = parser.nextDouble();
                    double test4 = parser.nextDouble();
                    double quiz1 = parser.nextDouble();
                    double quiz2 = parser.nextDouble();
                    double quiz3 = parser.nextDouble();
                    double quiz4 = parser.nextDouble();
                    double quiz5 = parser.nextDouble();
                    double quiz6 = parser.nextDouble();
                    double quiz7 = parser.nextDouble();
                    double quiz8 = parser.nextDouble();
                    double hw1 = parser.nextDouble();
                    double hw2 = parser.nextDouble();
                    double hw3 = parser.nextDouble();
                    double hw4 = parser.nextDouble();
                    double hw5 = parser.nextDouble();
                    double hw6 = parser.nextDouble();
                    double hw7 = parser.nextDouble();
                    double hw8 = parser.nextDouble();
                    double hw9 = parser.nextDouble();
                    double hw10 = parser.nextDouble();
                    line = Br.readLine();
                    double testaverage = ((test1 * test2 * test3 * test4)/4);
                    double quizaverage = ((quiz1 * quiz2 * quiz3 * quiz4 * quiz5 * quiz6 * quiz7 * quiz8)/8);
                    double hwaverage = ((hw1 * hw2 * hw3 * hw4 * hw5 * hw6 * hw7 * hw8 * hw9 * hw10)/10);
                    double totalaverage = ((testaverage * .6)+(quizaverage * .25)+(hwaverage * .15));

                    FileWriter fw = new FileWriter( "Average.txt");
                    BufferedWriter bw = new BufferedWriter( fw );
                    String Line1 = (firstname + lastname + totalaverage);
                    bw.write(Line1);
                    bw.newLine();
                    String Line2 = (firstname + lastname + totalaverage);   
                    bw.write(Line2);
                    bw.newLine();
                    String Line3 = (firstname + lastname + totalaverage);
                    bw.write(Line3);
                    bw.newLine();
                    String Line4 = (firstname + lastname + totalaverage);
                    bw.write(Line4);
                    bw.newLine();
                    String Line5 = (firstname + lastname + totalaverage);
                    bw.write(Line5);
            } 
            } catch (IOException I ) {
                System.out.println("I/O Exception");
            }
            }
        }

您的运行时错误似乎不是由try/catch块引起的。

看起来你有一个空指针异常,因为你在读取文件时的循环逻辑有点错误。我添加了一些内联注释来说明。

while (line != null) {
    // AKIRA: at this point, line != null, otherwise we wouldn't have entered the loop
    line = Br.readLine();
    // AKIRA: read off the next chunk from the file and assigned that to the var line, so it's got a new value
    System.out.println(line);
    /// AKIRA: what's the last thing that gets printed in your output before the exception is thrown?
    Scanner parser = new Scanner( line);

希望这能让事情变得更清楚。您需要仔细考虑循环中调用的顺序。一旦您进入循环的顶部,您就致力于运行整个循环。

最新更新