/*
姓名,学号,性别,年级,班级,总分
*/
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)
类Info第31行空指针异常,检查下吧
你看下你的sex 是否有值 。。这个是空指针异常
你看看你的输入文件是不是有错,你写的是按行读入 你文件每条数据是不是都一行 还有就是你每条数据是不是都有sexy这个属性,如果其中一个没有
肯定会有这个错的
a里面有东西吗,debug一下如果是空或者null,当然调后面的方法就会报空指针
空指针的意思是对象还未创建就去访问了,Java中所有的类都可以被认为是对象,从保存地点入手,检查对象的属性,数组等等是否初始化过
你贴了那么多代码就是没有Info.main 31 行的代码 我们怎么帮你看? 根据提示:空指针异常 看你的代码里面有对文件的操作,想必是File f 没有初始化就是调用f的方法了
File也许没有初始化导致f是null了。
可以给f赋初值,或者给get、set方法