2013-02-28 82 views
0

我应该能够按姓氏,然后名字排序students数组。我的compareTo方法适用于它,直到最后一次迭代。然后它会抛出一个NullPointerException,我不知道为什么。我一直在搜索我的书和互联网近12个小时。我尝试了我找到的所有东西,而且还没有骰子。Java NullPointerException排序数组

下面是程序代码:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package ch11pr112; 

import java.io.*; 
import java.util.Arrays; 
/** 
* 
* @author Tytus 
*/ 
public class CH11PR112{ 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) throws IOException { 
     // TODO code application logic here 
     BufferedReader in = new BufferedReader(new FileReader("src//ch11pr112//Students.txt")); 
     Student[] students = new Student[100]; 
     int i = 0; 
     double sum = 0; 
     String line = in.readLine(); 
     while (line != null) 
     { 
      String[] studentParts = line.split(" "); 
      String firstName = studentParts[1]; 
      String lastName = studentParts[0]; 
      Double score = Double.parseDouble(studentParts[2]); 
      students[i] = new Student(firstName, lastName, score); 
      sum += score; 
      i++; 
      line = in.readLine(); 
     } 
     double average = sum/i; 
     double x = i; 
     Arrays.sort(students); 
     for (i = 0; i < x; i++) 
     { 
      String studentList = students[i].getLastName() + " " + students[i].getFirstName() + " " + students[i].getScore(); 
      if (students[i].getScore() < (average - 10)) 
      { 
       System.out.println(studentList + " BELOW AVERAGE"); 
      } 
      else 
      { 
       System.out.println(studentList); 
      } 
     } 
     System.out.println(); 
     System.out.println("Average:\t" + average); 
    } 
} 

这里是我的Students.txt文件中的数据:

Gator Ali 85 
Vator Ella 75 
Beam John 60 
Beam James 95 
Class Lastin 55 
Steelman Andrea 95 
Murach Joel 92 
Lowe Doug 82 
Murach Mike 93 

这里是我的Student.java文件中的代码:

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package ch11pr112; 

/** 
* 
* @author Tytus 
*/ 
public class Student implements Comparable<Student>{ 
    private String firstName; 
    private String lastName; 
    private double score; 

    public Student(String firstName, String lastName, double score) 
    { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.score = score; 
    } 

    public String getFirstName() 
    { 
     return firstName; 
    } 

    public void setFirstName(String firstName) 
    { 
     this.firstName = firstName; 
    } 

    public String getLastName() 
    { 
     return lastName; 
    } 

    public void setLastName(String lastName) 
    { 
     this.lastName = lastName; 
    } 

    public double getScore() 
    { 
     return score; 
    } 

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

    @Override 
    public int compareTo(Student x) { 
     int lastNameCompare = this.lastName.compareToIgnoreCase(x.getLastName()); 
     if (this.lastName != null && x.lastName != null) 
     { 
      if (lastNameCompare == 0) 
      { 
       int firstNameCompare =  this.firstName.compareToIgnoreCase(x.getFirstName()); 
       if (this.firstName != null && x.firstName != null) 
       { 
        if (firstNameCompare == 0) 
         { 
         return 0; 
        } 
        else if (firstNameCompare > 0) 
        { 
         return 1; 
        } 
        else if (firstNameCompare < 0) 
        { 
         return - 1; 
        } 
       } 
      } 
      else if (lastNameCompare > 0) 
      { 
       return 1; 
      } 
      else if (lastNameCompare < 0) 
      { 
       return - 1; 
      } 
     } 
     return 0; 
    } 
} 

由于某些原因,它在232行上最后一次迭代期间创建了NullPointerExceptionif (pivot.compareTo(a[mid]) < 0))的ComparableTimSort.java文件。

的问题是如何防止NullPointerException,为什么当是不应该的代码,如果任一lastNamefirstName变量null要运行它被抛出。

+0

你可以给堆栈跟踪的错误? – 2013-02-28 06:45:42

+0

为什么你在'for循环'之前使用'double x = i',为什么不简单'int x = i'?任何具体的原因! – 2013-02-28 06:47:36

+1

什么是ComparableTimSort.java – 2013-02-28 06:49:07

回答

4

我不相信你可以使用Arrays.sort排序包含空值的阵列,用于精确,最终它会碰上,你必须pivot == null的情况的原因,所以你尝试作出比较

null.compareTo(Object) < 0 

参见警告在的Arrays API

的方法在这个类顶部的所有抛出NullPointerException如果指定数组引用为null,除非另有说明。

+3

+1问题是'Student [100]',默认情况下会留下额外的条目以'null'填充。 OP需要使用重载版本['Arrays.sort(data,fromIndex,toIndex)'](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort%28java .lang.Object%5B%5D,%20int,%20int%29)。 – mellamokb 2013-02-28 06:54:04

+1

是,或者使用[以Comparator作为参数的排序方法](http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html#sort(T [],%20java.util.Comparator)),在这种情况下,可以通过[Comparator]有效处理空值(http://docs.oracle.com/javase/6/docs/api/java/util/Comparator的.html)。 – femtoRgon 2013-02-28 06:58:14

+0

不幸的是,方向表示必须使用“Comparable”。 – 2013-02-28 23:46:16

1
public static void main(String[] args) throws Exception { 
    BufferedReader in = new BufferedReader(new FileReader("src//ch11pr112//Students.txt")); 
    String strLine; 
    int count = 0; 
    while ((strLine = in.readLine()) != null) { 
     count++; 
    } 
     Student[] students = new Student[count]; 
    int i = 0; 
    double sum = 0; 
    in = new BufferedReader(new FileReader("src//ch11pr112//Students.txt")); 
    String line = in.readLine(); 
    System.out.println(line); 
    while (line != null) 
    { 
     String[] studentParts = line.split(" "); 
     String firstName = studentParts[1]; 
     String lastName = studentParts[0]; 
     Double score = Double.parseDouble(studentParts[2]); 
     students[i] = new Student(firstName, lastName, score); 
     sum += score; 
     i++; 
     line = in.readLine(); 
    } 
    double average = sum/i; 
    double x = i; 
    for(int w=0;w<students.length;w++) 
    System.out.println(students[w]); 
    Arrays.sort(students); 
    for (i = 0; i < x; i++) 
    { 
     String studentList = students[i].getLastName() + " " + students[i].getFirstName() + " " + students[i].getScore(); 
     if (students[i].getScore() < (average - 10)) 
     { 
      System.out.println(studentList + " BELOW AVERAGE"); 
     } 
     else 
     { 
      System.out.println(studentList); 
     } 
    } 
    System.out.println(); 
    System.out.println("Average:\t" + average); 
} 
+0

我想过把它放在'for'循环中,然后试了一下。唯一的问题是,代码永远不会继续循环,直到“排序”完成。 – 2013-02-28 23:54:28