Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-05-2007, 05:40 AM
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
ERROR: nullPointerException
Hi, Please check this
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 exception at line 11...
why? how can i fix it?
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-05-2007, 07:54 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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() +": "); }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
NullPointerException ravian New To Java 2 12-07-2007 05:20 PM
NullPointerException Feng New To Java 5 11-24-2007 08:51 PM
NullPointerException problem warship AWT / Swing 5 08-10-2007 05:43 PM
ERROR: nullPointerException in applet barney Java Applets 1 08-07-2007 08:11 AM
Error Java.lang.NullPointerException in JBuilder2006 Jack Other IDEs 2 07-02-2007 03:29 AM


All times are GMT +3. The time now is 07:52 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org