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); } }
3 Answers
+ 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.
+ 2
studentArray[0] doesn't point to any instance of Student. To make that happen, write:
studentArray[0] = new Student();
0
ohhhh ok ok.. tqq now i understand