0

I am doing the question below:


A university wishes to keep student records in such a way that there are no duplicate student record for any student. Student details are as follows: national ID card number, surname, first name, address and programme of study. i. Write a class named Student which will allow the creation of objects with the above-mentioned attributes. Only the implementation of the constructor is required.

ii. Amend the class above to make all the Student objects orderable based on their surname, in case two students have the same surname, their Id card number will be used to order them.

iii. Write code to declare a suitable data structure to store the records

iv. Write code to add a student object, S1, to the list.

v. Write code to ask the user for an integer, n, and then allows insertion of n Students objects in the data structure.

vi. Write code to display all the details of all the Student objects from the data structure.


Here is what I have come up with so far:

import java.util.Comparator;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.TreeSet;

public class Student {
    private String nID;
    private String lname;
    private String fname;
    private String address;
    private String pos;

   public Student(String nID, String lname, String fname, String address, String pos){

    this.nID=nID;
    this.lname=lname;
    this.fname=fname;
    this.address=address;
    this.pos=pos;
}

public Student() {
    // TODO Auto-generated constructor stub
}

final static Comparator<Student> nameComparator=new Comparator<Student>(){
    public int compare(Student A, Student B){
        if((A.lname.compareTo((B.lname)))<0){
            return -1;
        }
        else if(A.lname.compareTo(B.lname)>0){
            return 1;
        }

        else{
            return (A.nID.compareTo(B.nID));
        }
    }

};

public String toString(){
   return "National ID: "+ nID+ " First Name: "+ fname+ " Last Name: "+ lname+ " Address: " +address+ " PoS"+pos+"\n";
}

public static void main(String[] args){

    Scanner sc= new Scanner(System.in);
    SortedSet<Student> names= new TreeSet<Student>(nameComparator);

    int num;

    System.out.println("Enter the number of students");
    num=sc.nextInt();

    Student a = new Student("daaa", "Jam", "Jick", "iiii", "ooooo");
    Student b = new Student("bba", "Aarav", "Gash", "iiii", "pooo");
    Student c = new Student("ccaa", "Jam", "Kick", "iiii", "ooooo");

    names.add(a);
    names.add(b);
    names.add(c);

    for(int i=0; i<num; i++){
        names.add(new Student());
    }

    for(Student i: names){
        System.out.println(i.toString());
      }
    }

 }

I am not sure of how to do the 5th part of the question or if I understood it well. When I execute this program I get null point exception at the name comparison and the add method in the for loop. I know the null pointer exception is because I have not added any arguments in the student object names.add(new Student()); but I want to find the right way of doing it.

Tia
  • 1,120
  • 3
  • 17
  • 37
  • [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bernhard Barker May 20 '17 at 02:27

1 Answers1

1

This

for(int i=0; i<num; i++){
    names.add(new Student());
}

will always add Student having all "null" fields. Use your other constructor instead:

public Student(String nID, String lname, String fname, String address, String pos){
johnII
  • 1,383
  • 1
  • 11
  • 18