繁体   English   中英

如何在我的代码中修复NullPointerException

[英]How to fix the NullPointerException in my code

所以这是一个问题:

您的程序应该能够从文件中将上述数据读取到数组中,并以学生的姓名升序对数组进行排序。 强烈建议在对象数组上使用选择排序算法。 唱片办公室还希望为每个班级(大一,大二,大三和大四)提供一个分类的班级列表。

打印以下内容:

  1. 排序后的主列表,其中包含整个学院的平均GPA。
  2. 大一新生的平均GPA列表。
  3. 二年级学生的平均GPA列表。
  4. 初中生平均GPA的初中生名单。
  5. 具有老年人平均GPA的老年人名单。

到目前为止,我还无法获取列表进行排序。 每次我尝试编译时,都会给我一个例外:

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

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

更重要的是,您打算读取一行并将该行插入一个数据槽,然后继续进行下一行。 因为对于一个可能没有足够多行的文件,假设它只有7行。 当您读取第8行时(文件读取器将其翻译为null), data[]所有Strings都将为null。

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

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>();
        while (true) {
            String line = reader.readLine();
            if (line == null) {
                break;
            }
            System.out.println(line);
            list.add(line);
        }

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

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

       // This is how you get each gpa for each line.   
       for (int i = 0; i < data.length; i++) {
          String[] words = data[i].split(" ");
          String gpa = words[3];
          System.out.println(gpa);
       }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM