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

05-05-2008, 04:33 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
[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.
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
}
}
|
|

05-05-2008, 04:48 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Posts: 353
|
|
|
you never called something new and whatever its name.
__________________
My IP address is 127.0.0.1
|
|

05-05-2008, 05:01 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
Hello,
See the code, It is explained on line number 32 and 42
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,संजीव
|
|

05-06-2008, 01:13 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
Thanks for the advice guys, got it workin now!!
|
|

05-06-2008, 07:17 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
Originally Posted by todd2230
Thanks for the advice guys, got it workin now!!
Ok.. mark your thread SOLVED
__________________
sanjeev,संजीव
|
|

05-06-2008, 07:22 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 3
|
|
|
And done, thanks again!
|
|

05-06-2008, 08:45 AM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 508
|
|
Originally Posted by todd2230
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|