我有一种方法可以通过其中一个值对一组对象进行冒泡。我一直得到一个空指针异常,我无法弄清楚它是如何得到一个空值。
java.lang.NullPointerException
at Student.sort(Student.java:96)
at Students.main(Students.java:58)
有什么想法吗?感谢
import java.util.Scanner;
public class Student
{private String fname, lname;
private int grade;
public Student(String fname, String lname, int grade)
{
this.fname = fname;
this.lname = lname;
this.grade = grade;
}
public String toString()
{
return fname + " " + lname + "\t" + grade;
}
public static void printAll(Student st[], int count)
{
for(int i = 0; i < count; i++)
{
System.out.println(st[i]);
}
System.out.println("***********************************************");
}
public static void firstName(Student st[], int count)
{
Scanner scan = new Scanner(System.in);
String param;
System.out.println("Enter parameter for first name: ");
param = scan.next();
for(int i = 0; i < count; i++)
{
if(st[i].fname.startsWith(param))
System.out.println(st[i]);
}
System.out.println("***********************************************");
}
public static void lastName(Student st[], int count)
{
Scanner scan = new Scanner(System.in);
String param;
System.out.println("Enter parameter for last name: ");
param = scan.next();
for(int i = 0; i < count; i++)
{
if(st[i].lname.startsWith(param))
System.out.println(st[i]);
}
System.out.println("***********************************************");
}
public static void interval(Student st[], int count)
{
Scanner scan = new Scanner(System.in);
int start, end;
System.out.println("Enter start: ");
start = scan.nextInt();
System.out.println("Enter end: ");
end = scan.nextInt();
for(int i = 0; i < count; i++)
{
if(st[i].grade >= start && st[i].grade <= end)
System.out.println(st[i]);
}
System.out.println("***********************************************");
}
public static void sort(Student st[], int count)
{
int swaps = 0;
Student t;
do {
swaps = 0;
for (int i = 0; i < count; i++)
{
if(st[i].grade > st[i+1].grade)
{
t = st[i];
st[i] = st[i+1];
st[i+1] = t;
swaps++;
}
}
} while (swaps > 0);
}
}
这是调用方法的主要部分
import java.util.Scanner;
import java.io.*;
public class Students
{
public static void main (String[] args) throws IOException
{ String first_name, last_name;
int grade, count = 0;
boolean flag = true;
Scanner fileInput = new Scanner(new File("students.txt"));
Scanner scan = new Scanner(System.in);
Student students[] = new Student[15];
while (fileInput.hasNext())
{
first_name = fileInput.next();
last_name = fileInput.next();
grade = fileInput.nextInt();
Student st = new Student(first_name, last_name, grade);
students[count] = st;
count++;
}
while(flag)
{
String choice = "";
System.out.println("Enter your choice: printall, firstname, lastname, interval, sort, end");
choice = scan.next();
if(choice.equals("printall"))
{
Student.printAll(students, count);
}
else if(choice.equals("firstname"))
{
Student.firstName(students,count);
}
else if(choice.equals("lastname"))
{
Student.lastName(students,count);
}
else if(choice.equals("interval"))
{
Student.interval(students,count);
}
else if(choice.equals("sort"))
{
Student.sort(students,count);
}
else
{
System.out.println("Thanks!,goodbye");
flag = false;
}
}
}
}