I am extremely new with java. I have been working on a project and am completely stumped.
What I am trying to do is I need to build a application that generates an ArrayList of certain students and grades, and then i will be able to manipulate them from there.
I thought I had it down to where I could get it to create the object at the very least, but it looks like that is not the case. It compiles correctly, but when I try to create the object using the Student() constructor with no arguments it gives me the following stack trace:
java.lang.NullPointerException
at Student.addInitialStudents(Student.java:61)
at Student.<init>(Student.java:23)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at bluej.runtime.ExecServer$3.run(ExecServer.java:759)
from reading the stack trace it looks like the issue is in my constructor.
import java.util.ArrayList;
public class Student
{
public ArrayList<Student> students = new ArrayList<Student>();
private String studentID;
private String firstName;
private String lastName;
private String email;
private int age;
private int grade1;
private int grade2;
private int grade3;
private int[][] grades;
private int i;
private int x;
/**
* Constructor for objects of class Student
*/
public Student(){
addInitialStudents();
}
public Student(String studentID, String firstName, String lastName, String email, int age, int[][] grades)
{
this.studentID = studentID;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.age = age;
this.grades = grades;
}
private void addInitialStudents(){
String[] id = {"1","2","3","4","5"};
String[] fName = {"John", "Suzan", "Jack", "Erin", "Sam"};
String[] lName = {"Smith", "Erickson", "Napoli", "Black", "smith"};
String[] mail = {"John1989@gmail.com","Erickson_1990@gmailcom","The_lawyer99yahoo.com","Erin.black@comcast.net","ssmith@gmail.com"};
int[] listAge = {20, 19, 19 ,22, 28};
int[][] iniGrades =
{
{88, 79, 59},
{91, 72, 85},
{85, 84, 87},
{91, 98, 82},
{99, 87, 92}
};
for (i = 0; i <= 4; i++){
studentID = id[i];
firstName = fName[i];
lastName = lName[i];
email = mail[i];
age = listAge[i];
for (x = 0; x <= 2; x++){
grades[i][x] = iniGrades[i][x];
}
students.add(new Student(studentID, firstName, lastName, email, age, grades));
}
}
public void getID(){
System.out.print(students.get(0));
}
}
Anyone have any tips on this?
BlueJ is highlighting the line
grades[i][x] = iniGrades[i][x];
I'm sorry if this is a duplicate, but I couldn't find a solution no matter how much research i tried to do and no matter how many changes I make.