Results 1 to 2 of 2
Thread: ERROR: nullPointerException
- 08-05-2007, 04:40 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 40
- Rep Power
- 0
ERROR: nullPointerException
Hi, Please check this
This code gives me a null pointer exception at line 11...Java Code:import TerminalIO.KeyboardReader; public class manyStudents { public static void main (String [] args) { KeyboardReader reader = new KeyboardReader(); Student[] student = new Student[500]; int stuNumb = reader.readInt ("Enter the number of students (max is 500): "); for (int i = 0; i < stuNumb; i++) { String stuName = reader.readLine ("Enter the name of sutden #"+ (i+1) +": "); student[i].setName(stuName); int testNumb = reader.readInt ("Enter the number of tests for "+ student[i].getName() +": "); } } }
why? how can i fix it?
Thanks
- 08-05-2007, 06:54 AM #2
Java Code:Student[] students = new Student[500]; // All elements of this array are null. for(int j = 0; j < 10; j++) System.out.println("students["+j+"] = " + students[j]); // If the Student class has a no-argument constructor you could // instantiate them all at once: for(int j = 0; j < stuNumb; j++) students[j] = new Student(); // You could also instantiate a student for each new element as you go: for (int i = 0; i < stuNumb; i++) { String stuName = reader.readLine ("Enter the name of sutden #"+ (i+1) +": "); students[i] = new Student(); // Now we have a non-null array element to work with. students[i].setName(stuName); int testNumb = reader.readInt ("Enter the number of tests for "+ students[i].getName() +": "); }
Similar Threads
-
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM -
NullPointerException problem
By warship in forum AWT / SwingReplies: 5Last Post: 08-10-2007, 04:43 PM -
ERROR: nullPointerException in applet
By barney in forum Java AppletsReplies: 1Last Post: 08-07-2007, 07:11 AM -
Error Java.lang.NullPointerException in JBuilder2006
By Jack in forum Other IDEsReplies: 2Last Post: 07-02-2007, 02:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks