I am really new to programming and I am trying to get a student inventory set up and validate the input by checking each field.
It contains attendance for 10 days and I need to validate each field that its either Yes/No. And I am assigning them to class fields using getter/setter methods. Since Attendance is series of 10 inputs, I am assigning them as a list and passing them as a parameter to the set method and assign them to Class array.
Though the list is not empty, assigning it to array is throwing 'Null pointer exception' and unable to figure out why.
import java.util.*;
public class Studentdetail {
String studentName;
String studentId;
String classCode;
String[] attendence;//={"no"," yes", "yes"," no", "yes"," yes", "yes", "yes"," no"," yes"};
String test1;
String test2;
String tutorial;
String exams;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getStudentId() {
return studentId;
}
public void setStudentId(String studentId) {
this.studentId = studentId;
}
public String getClassCode() {
return classCode;
}
public void setClassCode(String classCode) {
this.classCode = classCode;
}
public String[] getAttendence() {
return attendence;
}
private void setAttendence(List<String> studentList) {
int j=1;
for(String attList: studentList){
if(attList != null){
attendence[j]= attList;
}
j++;
}
}
public String getTest1() {
return test1;
}
public void setTest1(String test1) {
this.test1 = test1;
}
public String getTest2() {
return test2;
}
public void setTest2(String test2) {
this.test2 = test2;
}
public String getTutorial() {
return tutorial;
}
public void setTutorial(String tutorial) {
this.tutorial = tutorial;
}
public String getExams() {
return exams;
}
public void setExams(String exams) {
this.exams = exams;
}
public static void main(String[] args) {
String sampleInput = "S0032124, Tan ah cat, ICT310-FT02, no, yes, yes, no, yes, yes, yes, yes, no, yes, 43, 55, 12, 53";
ArrayList<String> studentList = new ArrayList<String>();
for (String s : sampleInput.split(",")) {
studentList.add(s);
}
Studentdetail newStudent = new Studentdetail();
newStudent.setStudentId(studentList.get(0));
newStudent.setStudentName(studentList.get(1));
newStudent.setClassCode(studentList.get(2));
newStudent.setAttendence(studentList.subList(3, 12));
newStudent.setTest1(studentList.get(13));
newStudent.setTest2(studentList.get(14));
newStudent.setTutorial(studentList.get(15));
newStudent.setExams(studentList.get(16));
boolean value;
value = classCodeValidator(newStudent.getClassCode());
value = stuAttValidator(newStudent.getAttendence());
if (value == true)
System.out.println("Class code verified "
+ newStudent.getClassCode());
else
System.out.println("Class code invalid "
+ newStudent.getClassCode().trim().substring(6,7));
}
public boolean stuIdValidator(String stuId) {
if (stuId.length() == 8) {
if (stuId.substring(0, 1) == "S")
return true;
}
return false;
}
public static boolean classCodeValidator(String classCode) {
// ICT303-FT07
if (classCode.trim().length() == 11)
if (classCode.trim().substring(6,7).equals("-"))
if (classCode.trim().substring(1,7).length() == 6)
if (classCode.trim().substring(7, 11).length() == 4)
return true;
return false;
}
public static boolean stuAttValidator (String[] stuAtten){
for(String attMarker: stuAtten){
if(attMarker.equalsIgnoreCase("YES") || attMarker.equalsIgnoreCase("NO"))
return true;
}
return false;
}
}