View Single Post
  #3 (permalink)  
Old 05-05-2008, 05:01 PM
sanjeevtarar's Avatar
sanjeevtarar sanjeevtarar is offline
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,संजीव
Reply With Quote