Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Java nullPointerExeption

    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() +": ");
    		}
    	}
    }
    this code gives me a null pointer exeption at line 11...

    why? how can i fix it?

    thanks

  • #2
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    in line 11, you try to access student[i] ... but student is an object, not an array.

    What does your Student class look like?

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    from the code it looks like it would be an array of strings where each element in the array is a students name and the array is 500 names long...


    Jason

  • #4
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    that may be the intent, but it isn't the code. He'd need:

    Code:
    String[] student = new String[500];

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i found it, thanks guys


    Code:
    student[i] = new Student();
    thats all i forgot to do :P

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •