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 05-05-2008, 04:33 PM
Member
 
Join Date: May 2008
Posts: 3
todd2230 is on a distinguished road
[SOLVED] Cant figure out null pointer exception
Hey guys im fairly new to java. I've been trying to figure out why i'm getting this null pointer exception at runtime with no luck. Any advice would be much appreciated.

Code:
import java.io.*; public class MyData { public static void main(String[] args) { //Student aStudent = new Student(); Student bStudent = new Student("1234567","John", "12 Wick St Redwood","2344","08/08/1986","95066713"); bStudent.printStudent(); } } class Student{ private String number; private String name; //private boolean male; private String address; private int post; private Date dob; // not yet instantiated private String phone; //private double marks[] = null; Student(){ // default constructor System.out.println("Student Created"); } Student(String number, String name, String address, String post, String dobString, String phone){ // constructor this.number = number; this.name = name; this.address = address; this.post = Integer.parseInt(post); // convert string to int. Date dob = new Date(dobString); // intialize dob object this.phone = phone; dob.printDob(); } void printStudent(){ System.out.println(number); System.out.println(name); System.out.println(address); System.out.println(post); dob.printDob(); //ERROR HERE System.out.println(phone); } } class Date{ int day, month, year; Date(){ // default constructor day = 0; month = 0; year = 0; } Date(String dobString){ String input[] = new String[10]; // for storing day month year String patternStr = "/"; // character to split line at. input = dobString.split(patternStr); day = Integer.parseInt(input[0]); // convert string to int. month = Integer.parseInt(input[1]); year = Integer.parseInt(input[2]); } void printDob(){ // to print out date of birth System.out.println(day + "/" + month + "/" + year); //format dd/mm/yyyy } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-05-2008, 04:48 PM
Zosden's Avatar
Senior Member
 
Join Date: Apr 2008
Posts: 353
Zosden is on a distinguished road
you never called something new and whatever its name.
__________________
My IP address is 127.0.0.1
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-05-2008, 05:01 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Hello,

See the code, It is explained on line number 32 and 42

Code:
import java.io.*; public class MyData { public static void main(String[] args) { //Student aStudent = new Student(); Student bStudent = new Student("1234567","John", "12 Wick St Redwood","2344","08/08/1986","95066713"); bStudent.printStudent(); } } class Student{ private String number; private String name; //private boolean male; private String address; private int post; private Date dob; // not yet instantiated private String phone; //private double marks[] = null; Student(){ // default constructor System.out.println("Student Created"); } Student(String number, String name, String address, String post, String dobString, String phone){ // constructor this.number = number; this.name = name; this.address = address; this.post = Integer.parseInt(post); // convert string to int. Date dob = new Date(dobString); // intialize dob object this.phone = phone; dob.printDob(); // Here You are not getting error because you are using //Date object that is initialized at line 30 // If you put this, you will not get any error this.dob = dob; } void printStudent(){ System.out.println(number); System.out.println(name); System.out.println(address); System.out.println(post); dob.printDob(); // Here dob object is null, because in this case //the dob Object is referencing to Object that defined on // line number 17 and is not initiantiated. It is NULL. System.out.println(phone); } } class Date{ int day, month, year; Date(){ // default constructor day = 0; month = 0; year = 0; } Date(String dobString){ String input[] = new String[10]; // for storing day month year String patternStr = "/"; // character to split line at. input = dobString.split(patternStr); day = Integer.parseInt(input[0]); // convert string to int. month = Integer.parseInt(input[1]); year = Integer.parseInt(input[2]); } void printDob(){ // to print out date of birth System.out.println(day + "/" + month + "/" + year); //format dd/mm/yyyy } }
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-06-2008, 01:13 AM
Member
 
Join Date: May 2008
Posts: 3
todd2230 is on a distinguished road
Thanks for the advice guys, got it workin now!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 05-06-2008, 07:17 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Quote:
Originally Posted by todd2230 View Post
Thanks for the advice guys, got it workin now!!
Ok.. mark your thread SOLVED
__________________
sanjeev,संजीव
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-06-2008, 07:22 AM
Member
 
Join Date: May 2008
Posts: 3
todd2230 is on a distinguished road
And done, thanks again!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 05-06-2008, 08:45 AM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 508
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
Quote:
Originally Posted by todd2230 View Post
And done, thanks again!
Don't forget to add reputation to those who helped/answers your problem.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
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
Null pointer Exception. after a bit of execution!! Plz help me rohan Java Applets 2 05-01-2008 11:14 AM
Null pointer exception error brownie_jedi New To Java 3 03-15-2008 07:27 AM
I can't figure this out silvia New To Java 3 07-20-2007 05:38 AM
statement null pointer exception bbq Database 1 07-05-2007 05:23 AM
Null pointer Exception peiceonly New To Java 2 04-06-2007 03:41 PM


All times are GMT +3. The time now is 11:21 PM.


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