Java:尝试创建存储的数据库

时间:2016-09-09 22:38:13

标签: java database

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

很抱歉发布了很多代码。但就在这里是问题所在。在下面的数据库类中。方法" void addCourses(StudentCourse e)"。运行测试类时。它在输入课程名称后立即崩溃,它不会像学生名一样存储它。我有点新的编程紧密。有人可以解释我错过了什么吗?

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

1 个答案:

答案 0 :(得分:1)

您尚未初始化courseList变量。您只在构造函数数据库中初始化了一个列表变量。添加课程时,addCourse()方法将抛出空指针异常。

在数据库构造函数中添加以下行:

courseList = new ArrayList<>();

此外,行courseList.addCourse(e)应该是编译错误(对我来说太愚蠢了)。 courseList是ArrayList类型的对象。 ArrayList类没有名为addCourse(Studentcourse e)的方法。它只有一个方法add(),它会在你的情况下采用StudentCourse类型的对象。所以你会看到找不到符号错误。

将该行更改为:

courseList.add(e);