this is my first time posting a question that related to OOAD and I am a newbie to OOAD, the programming language I am using is Java. I met some difficulties with my coding, I tried to browse through the StackOverflow website but I am still unable to find the solution. Below are my codes with the main testing class which is RecordApplication, I am trying to create an object which is called subjectMark in my main testing class but in order to call it I have to create a new default constructor manually in my SubjectMark class because I have a constructor already or else if I don't create the default constructor, I am not able to create the object subjectMark in the main testing class, please correct me if I am wrong at this part.
The second problem is I put my ArrayList in the constructor of the SubjectMark class, is this a correct way to do this? Because I am not able to do this in my main testing class by calling this subjectMarks[i].getBm(), I got an runtime error from the console Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.size()" because "this.markList" is null which showing my markList is blank which is also an ArrayList from my SubjectMark class.
Please correct me if there's any problem. Thanks for reading the long question and my long coding.
This is my main testing class:
import java.util.Scanner; public class RecordApplication { public static void main (String [] args) { SchoolMember schoolMember = new SchoolMember(); SubjectMark subjectMark = new SubjectMark(); RecordApplication.AddAStudent(schoolMember); RecordApplication.DisplayStudentInfo(schoolMember, subjectMark); public static void AddAStudent(SchoolMember schoolMember) { Scanner scanner = new Scanner(System.in); System.out.print("Name : "); String name = scanner.nextLine(); // Input command..... System.out.print("Enter score Moral : "); int moral = scanner.nextInt(); System.out.print("Enter score Civic : "); int civic = scanner.nextInt(); int term = 1; Student student = new Student (name,id,classes); SubjectMark subjectMark = new SubjectMark(bm, en, bc, mt, sc, moral, civic, term); schoolMember.recordAStudent(student); subjectMark.recordAMark(subjectMark); } public static void DisplayStudentInfo(SchoolMember schoolMember, SubjectMark subjectMark) { Student [] students = schoolMember.getStudentList(); SubjectMark [] subjectMarks = subjectMark.getMarkList(); System.out.printf("%20s %20s %10s" , "NAME", "STUDENT ID", "CLASSES"); System.out.println(); for (int i =0; i<schoolMember.getNumberOfStudents(); i++) { System.out.printf("%20s %20s %10s" , students[i].getName() , students[i].getId() , students[i].getClasses()); System.out.println(""); } for (int i = 0; i<subjectMark.getNumberOfMarks(); i++ ) { System.out.println("\nBM:" + subjectMarks[i].getBm()); System.out.println("EN:" + subjectMarks[i].getEn()); System.out.println("BC:" + subjectMarks[i].getBc()); System.out.println("MT:" + subjectMarks[i].getMt()); System.out.println("SC:" + subjectMarks[i].getSc()); System.out.println("MORAL:" + subjectMarks[i].getMoral()); System.out.println("CIVIC:" + subjectMarks[i].getCivic()); } } } This is my Subject Mark Class:
import java.util.ArrayList; public class SubjectMark { private ArrayList<SubjectMark> markList; private int bm; private int en; private int bc; private int mt; private int sc; private int moral; private int civic; private int term; public SubjectMark() { } public SubjectMark(int bm, int en, int bc, int mt, int sc, int moral, int civic, int term) { markList = new ArrayList<SubjectMark>(); this.bm = bm; this.en = en; this.bc = bc; this.mt = mt; this.sc = sc; this.moral = moral; this.civic = civic; this.term = term; } public int getBm() { return bm; } //getter for each subject, I deleted some to make the code cleaner public int getCivic() { return civic; } public int getTerm() { return term; } public void recordAMark(SubjectMark subjectMark) { markList.add(subjectMark); } public int getNumberOfMarks() { return markList.size(); } public SubjectMark [] getMarkList() { SubjectMark [] subjectMarks = new SubjectMark[markList.size()]; for (int i=0; i < markList.size(); i++) { subjectMarks[i] = markList.get(i); } return subjectMarks; } } https://stackoverflow.com/questions/66718065/unable-to-read-an-arraylist-from-another-class-getting-runtime-error March 20, 2021 at 11:51AM
没有评论:
发表评论