0

I have a method to do a bubble sort of an array of objects by one of the values in it. I keep getting a null pointer exception an i cant figure out how its getting a null value.

java.lang.NullPointerException
at Student.sort(Student.java:96)
at Students.main(Students.java:58)

Any ideas? thanks

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); 
}

}

Here is the main that calls the method

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;
        }
    }
}
}
GeoffM
  • 31
  • 1
  • 6

0 Answers0