-4

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

}

fjosep
  • 1
  • 3
  • 5
    `attendence`is not initialized. Use a debugger to solve this kind of bug – jhamon Dec 07 '16 at 08:51
  • Generally, when asking on SO about any Exception you cannot figure out, it is a good idea to add the stacktrace ( that output like "Exception blah blah at xyz ... at abc ... at def ..." ) – Fildor Dec 07 '16 at 08:57
  • @jhamon - have added the entire code here. Hope this helps – fjosep Dec 07 '16 at 09:10

1 Answers1

1

First you need to initialize your string array attendence.

string[] attendence;

private void setAttendence(List<String> studentList) {

        int j=1;

        for(String attList: studentList){

            if(attList != null){
                attendence[j]= attList; // getting null pointer exception 
            }
            j++;
    }   
}
    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);
    }
attendence = new string[36];
newStudent.setAttendence(studentList.subList(3, 12)); 
}

Otherwise you can not reach a value from attendence because it is just a pointer indicates null. If you are familliar with lower level programming languages, arrays are pointers and they need to show beginning of allocated memory space. By saying new string[n] you allocate n*sizeof(string) bytes memory space. So if you say attendence[1] you will reach location of &attendence + sizeof(string).

By the way string is a char array so you actually get pointer of pointer. I mean sizeof(string) = 1 word..

Dogukan
  • 11
  • 3
  • Any suggestion how to get it initialized in the above code ? Because when i tried to initialize values during declaration, I am getting ArrayOutOfBound exception. – fjosep Dec 07 '16 at 09:21
  • Issue resolved. Added initialization for the string array and now there is no error. private void setAttendence(List studentList) { attendence = new String[] {"no"," yes", "yes"," no", "yes"," yes", "yes", "yes"," no"," yes"}; int j=1; for(String attList: studentList){ if((attList != null) && (attendence !=null)){ attendence[j]= attList; } j++; } – fjosep Dec 07 '16 at 09:37