Palermox Palermox - 2 months ago 6
Java Question

Java: Trying to create a database that stores

package student;
import javax.swing.JOptionPane;

public class Student
{
private Name name;
String idNUM, course;

public Student(Name n, String idNum){
this.name = n;
this.idNUM = idNum;
}
public Name getName(){
return name;
}
public String getId(){
return idNUM;
}
}



package student;
public class StudentCourse
{
Student studInfo, studentInfo;
String studentCourses, studentCourse;


StudentCourse(String sc)
{
studentCourses = sc;
}

public String getCourses(){
return studentCourses;
}
}


package student;

public class StudentAccounts
{
private Student stud;
private String addedClass;
String courses;

public StudentAccounts (Student s, String course)
{
stud = s;
courses = course;
}
public Student getStudent()
{
return stud;
}
public void insertClass(String cla)
{
courses = cla;
}
public String getCourses()
{
return courses;
}
}


Sorry for posting a lot of code. But right here is where the problem is. in the Database class below. The method "void addCourses(StudentCourse e)". When running the test class. It crashes right after entering a course name, it won't store it like the student name. Im a little new with programming cohesively. Can someone please explain what I am missing?

package student;
import java.util.ArrayList;


public class DataBase
{
ArrayList <StudentAccounts> list;
ArrayList <StudentCourse> courseList;

StudentAccounts sa ;
StudentCourse sc;
int index;
boolean found = false;

DataBase()
{
list = new ArrayList<> ();
}
ArrayList getList()
{
return list;
}
ArrayList getCourseList()
{
return courseList;
}
StudentCourse getCourse(){
return sc;
}
StudentAccounts getAccount()
{
return sa;
}
StudentAccounts delete (int d)
{
return list.remove(d);
}

boolean inList() //Looks in the ArrayList
{
return found;
}
boolean isEmpty()
{
return list.isEmpty();
}
int getIndex()
{
return index;
}
int getSize() // return the amount of strings in the Array
{
return list.size();
}

void add(StudentAccounts s)
{
list.add(s);
}
void addCourse(StudentCourse e)
{
courseList.addCourse(e);
}

void search(String key)
{
found = false;
int i = 0;

while (!found && i < list.size() )
{
StudentAccounts sl = list.get(i);
if(sl.getStudent().getId().equalsIgnoreCase(key))
{
sa =sl;
found = true;
index = i;
}
else
i++;
}
}
}

Answer

You have not initialized your courseList variable. You have initialized only the one list variable in the constructor Database. When you add a course, the addCourse() method will throw a null pointer exception.

Add the following line in your Database constructor:

courseList = new ArrayList<>();

Also, the line courseList.addCourse(e) should be a compilation error (so silly of me). courseList is an object of type ArrayList. ArrayList class does not have a method called addCourse(Studentcourse e). It only has a method add() which will take an object of type StudentCourse in your case. So you will see a cannot find symbol error.

Change that line to:

courseList.add(e);
Comments