이것은 내 주요 코드입니다.
Scanner input= new Scanner(System.in);
Student[] starray=new Student[5];
for (int i=0; i<3; i++)
{
System.out.println("enter:");
starray[i].name=input.next();
System.out.println("enter:");
starray[i].family=input.next();
System.out.println("enter:");
starray[i].sid=input.nextInt();
}
for(int i=0; i<3; i++)
System.out.println(starray[i].name);
그리고 나는 하나의 수업이 있습니다.
String name,family;
Integer sid;
Student(){
name="kh";
family="kh";
sid=0;}
내가 실행할 때 아래에 예외가 있습니다 : Exception in thread "main"java.lang.NullPointerException at testcodes.TestCodes.main (TestCodes.java:19) Java Result : 1
이 경우 Java는 C ++와 매우 유사합니다. C ++에서 객체 배열을 선언 할 때 모든 객체는 아직 초기화되지 않았습니다 (이것들은 그 안에 실제 객체가 없음). 즉, 배열은 객체 의 자리 표시 자일 뿐입니다 .
그래서 당신의 진술
Student[] starray = new Student[5];
시각적 형태로
starray --> +------+------+------+------+------+
| null | null | null | null | null |
+------+------+------+------+------+
그리고이 진술 후에
starray[0] = new Student();
될 것이다
starray --> +------+------+------+------+------+
| | null | null | null | null |
+---|--+------+------+------+------+
|
v
+------------------+
| Student Instance |
+------------------+
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다