Results 1 to 7 of 7
  1. #1
    todd2230 is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default [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.

    Java 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
    	}
    	
    }

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    6

    Default

    you never called something new and whatever its name.
    My IP address is 127.0.0.1

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello,

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

    Java 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(); [B]// 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;[/B]
       }
       
       void printStudent(){
    	System.out.println(number);
    	System.out.println(name);
    	System.out.println(address);
    	System.out.println(post);
    	dob.printDob(); [B]// 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.[/B]
    	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,संजीव

  4. #4
    todd2230 is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks for the advice guys, got it workin now!!

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by todd2230 View Post
    Thanks for the advice guys, got it workin now!!
    Ok.. mark your thread SOLVED
    sanjeev,संजीव

  6. #6
    todd2230 is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    And done, thanks again!

  7. #7
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Quote Originally Posted by todd2230 View Post
    And done, thanks again!
    Don't forget to add reputation to those who helped/answers your problem.
    freedom exists in the world of ideas

Similar Threads

  1. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  2. Replies: 2
    Last Post: 05-01-2008, 10:14 AM
  3. Null pointer exception error
    By brownie_jedi in forum New To Java
    Replies: 3
    Last Post: 03-15-2008, 06:27 AM
  4. I can't figure this out
    By silvia in forum New To Java
    Replies: 3
    Last Post: 07-20-2007, 04:38 AM
  5. statement null pointer exception
    By bbq in forum JDBC
    Replies: 1
    Last Post: 07-05-2007, 04:23 AM

Posting Permissions

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