aspose file tools*
The moose likes Java in General and the fly likes not sorting; getting null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "not sorting; getting null" Watch "not sorting; getting null" New topic
Author

not sorting; getting null

Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hello all, I had a question about what I am doing incorrectly. I have written this piece of code about 4 days ago; I have left it to come back to hopefully solve the problem but no luck. Anyways, I am tring to read from a txt file. I understand how to do this. But for some reason when I try to sort a group of students according to their marks I get a null pointer exception. Here is what I am receiving while trying to run my code; maybe its something simple.




next class
import java.util.*;
import java.text.DecimalFormat;
import java.io.*;


public class Student implements Comparable, Serializable {

// data fields
private String name;
private double [] scores;
private double average;

// Setup a format to deal with decimal places.
DecimalFormat fmt = new DecimalFormat("0.00");

// methods
// Create a StringTokenizer object, parse the received string in order to
// update and store the data in the name and scores[] fields.
void fillStudent(String s){
StringTokenizer t = new StringTokenizer(s);
name = t.nextToken();

for(int i=1;i<scores.length;i++){
scores[i]= Double.parseDouble(t.nextToken());
}
}

// A method for calculating the average in an [] of doubles.
void findAverage(){
double sum = 0.0;
int numberOfScores=scores.length-1;

for(int i = 0; i < scores.length;i++){
sum += scores[i];
}
if(scores.length > 0){
average = sum /(double) numberOfScores;
}else{
average = 0;
}
}

// A method to return name.
public String getName(){
return name;
}

// A method for returning the average.
double getAverage(){
return average;
}

// A method to compare two objects and to force upcasting to take place so
// that the Student's average field can be updated.
public int compareTo(Object obj) {
if(this.average > (((Student)obj).average)){
return 1;
}else if(this.average<(((Student)obj).average)){
return -1;
}else {
return 0;
}
}

// A method to return useful information for the student.
public String toString(){
String strTemp = "Student's Name: " + name +
"\nThe average of the test mark scores is " + fmt.format(average);
return strTemp;
}
public boolean equals(Student stud){
return this.name.equals( stud.name);
}


// constructor
// used during construction of the object, the constructor is called after
// the Student's object is instantiated. The contructor will initialize the
// data fields name and average while creating a scores [] to hold 5
// doubles.
Student(){
scores = new double [5];
name = "";
average = 0.0;
}
// postcondition - takes avg of type double as a parameter and sets
// the data field average
Student(double avg){
average=avg;
}
}
[/CODE]
Hopefully, someone can point to me where I goofed up. Thanks in advance.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Since you don't completely fill the Student[], when you pass the entire array off to your selectonSort(Comparable[]) method, and it calls the findPosMin(Comparable[],int) it iterates through the entire array calling compareTo() on each element, when it gets to an unfilled position, it throws a null pointer exception.
[ March 06, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Thanks, for the immediate feedback. That makes sense, but where do you conclude that I am passing my incomplete [] to the selectionSort method before it is completely full. I have changed the array size to 23 elements; that is 22 indexes and still get the same problem. Thanks again for any feedback.
Mike Smith
Ranch Hand

Joined: Sep 23, 2005
Posts: 85
Hi I also changed the following piece of code; and still the same error. I am starting to really love the NullPointerException.



I may need a another nudge. Thanks again.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

First, I'd like to comment on the following :

What is this loop, with a non-conditional break inside ?
You probably want to do :
stud[total] = new Student();, without the loop.

About your problem, how many lines do you have in your file ?
If you don't have 25 students, you'll get a NullPointerException later.


[My Blog]
All roads lead to JavaRanch
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
but where do you conclude that I am passing my incomplete [] to the selectionSort method before it is completely full.

I was guessing that since only 23 names printed out before the null value, that there were only 23 filled positions in your array, but now I think I found the real problem


Every time you break out of the for loop, control goes back to the beginning of the while loop and then back to the for loop where you reinitilize i. Therefore the array is not filled, you just keep putting new elements in stud[0].
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not sorting; getting null
 
Similar Threads
code help
average monthly temps and arrays
Counting in Array List/ Standard deviation
How to store ten scores in an array?
finding an average