2014-04-12 3 views
1

나는 Student 객체의 네 요소 배열을 만드는 TestStudent 클래스라는 프로그램을 코딩했다. 사용자는 학생 ID, 이름, 부서 및 분류 등급을 입력하라는 메시지가 나타납니다.네 요소 배열 개체 설정 메서드?

import java.util.Scanner; 

public class TestStudent { 

public static void main(String[] args) { 
    //Create a Scanner object 
    Scanner input = new Scanner(System.in); 

    //Create a four element array of Student object 
    Student[] studentList = new Student[4]; 

    for (int i = 0; i < studentList.length; i++) { 
     int j = i + 1; 
     //Prompt user to enter student matrix number 
     System.out.println("Enter student " + j + " id : ");    
     studentList[i].setIdStudent(input.nextInt()); 

     //Prompt user to enter student name 
     System.out.println("Enter student " + j + " name : "); 
     studentList[i].setName(input.nextLine()); 

     //Prompt user to enter student department 
     System.out.println("Enter student " + j + " department : "); 
     studentList[i].setDepartment(input.nextLine()); 

     //Prompt user to enter student classification level 
     System.out.println("Enter student " + j + " classification : "); 
     studentList[i].setClassification(input.next()); 

     System.out.println("\n"); 
    } 

    //Print result 
    System.out.println("Id Student Name   Department  Classification"); 
    System.out.println("******************************************************************************"); 
    for (int i = 0; i < studentList.length; i++) { 
    System.out.println(studentList[i].getIdStudent() + " " + studentList[i].getName() + " " + studentList[i].getDepartment() + "  " + studentList[i].getClassification()); 
    } 

} 

    public class Student { 
     //Student matrix number 
     private int idStudent; 

     //Student name 
     private String name; 

     //Student department 
     private String department; 

     //Student classification level 
     private String classification; 

     //Construct a default Student object 
     public Student() { 
     idStudent = 0; 
     name = " "; 
     department = " "; 
     classification = " "; 
     } 

     //Construct a Student object 
     public Student(int idStudent, String name, String department, String classification) { 
     this.idStudent = idStudent; 
     this.name = name; 
     this.department = department; 
     this.classification = classification; 
     } 

     //Return student matrix number 
     public int getIdStudent() { 
     return idStudent; 
     } 

     //Return student name 
     public String getName() { 
     return name; 
     } 

     //Return student department 
     public String getDepartment() { 
     return department; 
     } 

     //Return student classification level 
     public String getClassification() { 
     return classification; 
     } 

     //Set student matrix number 
     public void setIdStudent(int newIdStudent) { 
     newIdStudent = idStudent; 
     } 

     //Set student name 
     public void setName(String newName) { 
     newName = name; 
     } 

     //Set student department 
     public void setDepartment(String newDepartment) { 
     newDepartment = department; 
     } 

     //Set student classification level 
     public void setClassification(String newClassification) { 
     newClassification = classification; 
     } 

     } 

} 

예상 출력은 다음과 같습니다 :

Id Student  Name   Department  Classification 
    ******************************************************************************** 
    1140   Will Gerard Music    Junior 
    1152   Julia Ross  Architecture  Freshman 
    1130   Fred Huan  Medic    Senior 
    1137   Clara Whist Aviation   Sophomore 

을하지만 오류가 발생했습니다 : 여기 코드는

Enter student 1 id : 
1140 
Exception in thread "main" java.lang.NullPointerException 
    at Asg2.TestStudent.main(TestStudent.java:28) 

나는 문제가 studentList[i].setIdStudent(input.nextInt()); 줄 것으로 보인다 있다고 가정합니다. 이 문제를 해결하는 방법에 대한 제안? 당신이 그것에 메서드를 호출하거나 필드
하나에 액세스하려고 할 때이

+0

['this'] (http://stackoverflow.com/questions/5364278/creating-an-array-of-objects-in-java)가 문제입니다. – GoldRoger

답변

1

1) (28)

2 행으로 이동) 당신은 순간에 null 값
을 가지고 몇 가지 기준을 가지고 (클래스 변수).

3)
null 이외의 값을 가지고 그것을 필요로 할 때 null가 아니다 그래서 당신이 그것을 사용하는
을 시도하기 전에 해당 참조를 초기화해야합니다.

+0

이 코드를 추가하겠습니다. studentList [i] = new Student(); '는 데이터 필드를 초기화합니다 (Student 클래스의 생성자 Student()에 의해)? –

+0

네, 맞습니다. –