View Single Post
  #1 (permalink)  
Old 05-05-2008, 04:33 PM
todd2230 todd2230 is offline
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 } }
Reply With Quote
Sponsored Links