如何在我的代码中修复NullPointerException

时间:2015-10-03 16:53:10

标签: java arrays sorting

所以这就是问题:

  

您的程序应该能够从文件中读取上述数据到一个数组中,并按照学生的名字按升序对数组进行排序。强烈建议在对象数组上使用选择排序算法。而且唱片办公室希望每个班级,新生,大二,大三和大四都有一个排序班级列表。

     

打印出以下内容:

     
      
  1. 排序的主列表,其中包含整个大学的平均GPA。
  2.   
  3. 新生名单与新生的平均GPA。
  4.   
  5. 大二学生名单,大二学生的平均GPA。
  6.   
  7. 青少年名单与青少年的平均GPA。
  8.   
  9. 老年人平均GPA的高级名单。
  10.   

到目前为止,我无法让我的清单自行排序。每次我尝试编译时,都会给我一个例外:

Exception in thread "main" java.lang.NullPointerException
    at Project1.main(Project1.java:33)

这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class Project1 {
    public static void main(String[] args) throws IOException {
        BufferedReader reader =
            new BufferedReader(new FileReader("/Users/Ayesha/Desktop/inputfile.txt"));
        ArrayList<String> list = new ArrayList<>();
        String data[] = new String[25];
        while (true) {
            String line = reader.readLine();
            for (int i = 0; i < 24; i++) {
                data[i] = line;
            }
            if (line == null) {
                break;
            }
            System.out.println(line);
            list.add(line);
        }

        // String data[]=list.toArray(new String[25]);

        reader.close();

        for (int i = 0; i < data.length - 1; ++i) {
            int minIndex = i;
            for (int j = i + 1; j < data.length; ++j) {
                if (data[j].compareTo(data[minIndex]) < 0) {
                    minIndex = j;
                }
            }
            String temp = data[i];
            data[i] = data[minIndex];
            data[minIndex] = temp;
        }
        System.out.println(data);
    }
}

这是源文件:

NAME  CLASS   GPA
Williams, Leonard Freshman    1.85
Smith, Sheila Senior  2.99
Anderson, Andy    Sophomore   3.01
Wiser, Bud    Freshman    4.00
Robertson, Jully  Junior  2,78
Koran, Korn   Junior  3.50
Smith, Sam    Junior  2.14
Johnson, Jim  Junior  3.05
Johnson, Jane Junior  3.75
Potter, Pam   Senior  2.98
Brown, Bill   Sophomore   2.55
Crooks, Cathy Freshman    1.99
Gregg, Howard Senior  2.44
Nicholas, Judy    Senior  3.69
White, Bob    Sophomore   1.64
Walsh, Fred   Junior  4.00
Dennis, Susan Senior  2.06
Roberts, Rachel   Sophomore   4.00
Fredericks, Mary  Freshman    2.89
Holmes, Wendy Senior  2.56
Edwards, James    Sophomore   3.00
Green, Barbara    Sophomore   3.67
Brown, David  Freshman    2.00
Williamson, Walt  Sophomore   2.95
Carson, Jim   Sophomore   2.03

2 个答案:

答案 0 :(得分:0)

    while (true) {
        String line = reader.readLine();
        for(int i=0; i<24; i++){
            data[i]= line;
        }
        if (line == null) {
            break;
        }
        System.out.println(line);
        list.add(line);
    }   

在此代码段中,您实际上正在从文件中读取一行,并将同一行插入到String data[]= new String[25];的25个插槽中的所有24个中。那可能是个错误;你应该问for loop在我小于25时运行。

更重要的是,您打算读取一行并将该行插入一个数据槽,然后继续下一行。因为对于可能不包含足够行的文件,假设它只有7行。当您在阅读第8行(文件阅读器转换为null)时,data[]中的所有字符串都将为空。

答案 1 :(得分:-1)

这是改进的解决方案代码,

%app_user%
%app_pass%