尝试/捕获和声明变量

时间:2015-04-01 19:48:11

标签: variables runtime-error try-catch filereader

我试过移动我的Try / Catch语句的位置,我找不到一种方法来将代码放入两个try语句而不会取消明确br或fr变量

以下是我得到的输出:

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个作业等级。

我对编程非常陌生,我从未遇到过编译但运行不稳定的“运行时”错误。当我问他时,我的TA无法找出问题。

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

            }
            }
        }

1 个答案:

答案 0 :(得分:0)

看起来您的运行时错误不是由于您的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);

希望这更清楚。您需要仔细考虑循环中调用的顺序。一旦进入循环的顶部,就会致力于遍历整个循环。