0

Why it shows "exception in thread "main" java.lang.NullPointerException at TestArray.main(TestArray.java:11)"

class Student { int marks; } class TestArray { public static void main (String []args) { Student[] studentArray = new Student[7]; studentArray[0].marks = 100; System.out.println(studentArray[0].marks); } }

15th Jan 2017, 7:56 PM
K-t Tini
K-t Tini - avatar
3 Antworten
+ 2
It's because your array is empty (filled with nulls). You have to fill it with Student instances first (or/and check for nulls before accessing Student properties.
15th Jan 2017, 8:51 PM
Ivan G
Ivan G - avatar
+ 2
studentArray[0] doesn't point to any instance of Student. To make that happen, write: studentArray[0] = new Student();
15th Jan 2017, 8:52 PM
DaemonThread
DaemonThread - avatar
0
ohhhh ok ok.. tqq now i understand
17th Jan 2017, 1:44 AM
K-t Tini
K-t Tini - avatar