I am supposed to be able to sort the students
array by last name, then first name. My compareTo
method works for it, until the last iteration. It then throws a NullPointerException
and I have no idea why. I have been scouring my book and the internet for almost 12 hours now. I have tried everything I have found, and still no dice.
Here is the code for the program:
/*
* 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);
}
}
Here is the data in my Students.txt file:
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
Here is the code in my Student.java file:
/*
* 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;
}
}
For some reason, it is creating a NullPointerException
during the last iteration on line 232 (if (pivot.compareTo(a[mid]) < 0)
) of ComparableTimSort.java
file.
The question is how to prevent the NullPointerException
and why it is being thrown when the code is not supposed to be ran if either the lastName
or firstName
variables are null
.
double x = i
just before thefor loop
, why not simplyint x = i
? Any specific reasons !!! – nIcE cOw Feb 28 '13 at 6:47int
,double
is just the one I used. It doesn't matter which one is used.ComparableTimSort.java
is a built in Java file. I believe it is built in with Java and not just with NetBeans (the program I am using). – Tytus Strube Feb 28 '13 at 23:43