Tim_Wang__ 2017-01-21 09:54 采纳率: 50%
浏览 1357
已结题

一个简单的学生信息管理系统,类中报错 java.lang.NullPointerException

/*

  • 学生类
  • 姓名,学号,性别,年级,班级,总分
    */
    public class Student implements Comparable {

    private String name;
    private String num;
    private String sex;
    private String grade;
    private String banji;
    private String score;

    public Student(String name, String num, String sex, String grade, String banji, String score)
    {
    this.name = name;
    this.num = num;
    this.sex = sex;
    this.grade = grade;
    this.banji = banji;
    this.score = score;
    }

    public Student()
    {
    this.name = "未知";
    this.num = "未知";
    this.sex = "未知";
    this.grade = "未知";
    this.banji = "未知";
    this.score = "未知";
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getNum() {
    return num;
    }

    public void setNum(String num) {
    this.num = num;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }

    public String getGrade() {
    return grade;
    }

    public void setGrade(String grade) {
    this.grade = grade;
    }

    public String getBanji() {
    return banji;
    }

    public void setBanji(String banji) {
    this.banji = banji;
    }

    public String getScore() {
    return score;
    }

    public void setScore(String score) {
    this.score = score;
    }

    public String getStudent() {
    return name+" "+num+" "+sex+" "+grade+" "+banji+" "+score+"\r\n";
    }
    public int compareTo(Student other) {

    if (Integer.parseInt(num) return - 1 ;
    if (Integer.parseInt(num)>Integer.parseInt(other.num))

    return 1 ;

    return 0 ;

    }
    }

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Sort {

private File f;

public Sort(File f) {
    this.f = f;
}

public Sort() {     
}

public List<Student> filereader() throws IOException{//对原TXT文件的读取
    FileReader fr = new FileReader(f);
    BufferedReader cin=new BufferedReader(fr);
    List<Student> l=new ArrayList<Student>();
    Student[] student=new Student[1000];
    String s="";int i=0;
    while((s=cin.readLine())!=null)
    {
        student[i]=new Student();
        String[] a=new String[6];
        a=s.split(" ");
        student[i].setName(a[0]);
        student[i].setNum(a[1]);
        student[i].setSex(a[2]);;
        student[i].setGrade(a[3]);
        student[i].setBanji(a[4]);
        student[i].setScore(a[5]);
        i++;
        l.add(student[i]);
    }
    cin.close();
    fr.close();
    return l;
}


public void Sortsex(String sex,File f1) throws IOException{//指定性別后按学号排序
    FileWriter fw = new FileWriter(f1);
    @SuppressWarnings("resource")
    BufferedWriter cout=new BufferedWriter(fw);
    List<Student> a=new ArrayList<Student>();
    List<Student> b=new ArrayList<Student>();
    a=filereader();
    int i=0;    
    Student[] student=new Student[1000];
    for(int t=0;t<a.size();t++){
        if(a.get(t).getSex().equals(sex)){  /*******这行报错*********/
            student[i]=a.get(t);
            i++;
        }
    }
    Arrays.sort(student);
    for(int m=0;m<i-1;m++)
    {
    cout.write(student[i].getStudent());
    }
    System.out.println("已生成文件");
}

}

Exception in thread "main" java.lang.NullPointerException
at Info.main(Info.java:31)

  • 写回答

7条回答 默认 最新

  • Littlechoc 2017-01-21 11:32
    关注

    类Info第31行空指针异常,检查下吧

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?