Java中的空指针异常

时间:2016-12-07 08:49:36

标签: java arraylist nullpointerexception

我是编程新手,我正在尝试设置学生库存并通过检查每个字段来验证输入。

它包含10天的出勤率,我需要验证每个字段的是/否。我正在使用getter / setter方法将它们分配给类字段。由于考勤是一系列10个输入,我将它们分配为列表并将它们作为参数传递给set方法并将它们分配给Class数组。

虽然列表不为空,但是将它分配给数组会抛出'空指针异常'并且无法找出原因。

import java.util.*;

公共课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;
}

}

1 个答案:

答案 0 :(得分:1)

首先,您需要初始化字符串数组注意事项。

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

否则,您无法从注意力中获得值,因为它只是指针指示null。 如果您熟悉较低级别的编程语言,则数组是指针,它们需要显示分配的内存空间的开头。通过说新的字符串[n],你可以分配n * sizeof(字符串)字节的内存空间。因此,如果您说注意[1],您将达到&amp; attendence + sizeof(string)的位置。

顺便说一句,string是一个char数组,所以你实际上得到了指针的指针。我的意思是sizeof(string)= 1个字..