Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.NullPointerException... 1

Status
Not open for further replies.

B14speedfreak

Technical User
Mar 23, 2006
182
Hi all,

Thanks in advance for all help / posts on this one.

I have the following bit of code :



public void Person_create(String first_name, String surname, String title, int dob_year, int dob_day, int dob_month){

///load attributes into the class from the method above
this.First_Name = first_name;
this.Surname = surname;
this.Title = title;

///put the dobs into the dob date class


this.dob.set(dob_year, dob_month, dob_day);

}



When I try to create a new object of this class I get:

java.lang.NullPointerException

Any ideas where I am going (am using netbeans so the import statements are all sorted etc...)

thanks,



B14... aka... Marky Mark... the frozen monkey in the server room...
 
Appologies,

the dob variable is an object of class Calendar, the rest are Strings, and its when populating the dob variable which causes the problem (well thats what netbeans points to anyway...)

thanks,

B14... aka... Marky Mark... the frozen monkey in the server room...
 
ok heres the full class and what I am trying to execute in the main method:

package instructor;

import java.util.*;

/**
*
* @author MNewton
*/
public class Person {

private String First_Name;
private String Surname;
private String Title;
private Calendar dob;

/** Creates a new instance of Person with no Attributes */
public Person() {

}
/** Creates a new instance of the Person Class using attributes supplied by the user. Requires 3 ints for the dob, yyyy, dd, mm*/
public void Person_create(String first_name, String surname, String title, int dob_year, int dob_day, int dob_month){

///load attributes into the class from the method above
this.First_Name = first_name;
this.Surname = surname;
this.Title = title;

///put the dobs into the dob date class


this.dob.set(dob_year, dob_month, dob_day);

}

/** sets the firstname of the person, requires a String*/
public void set_First_Name(String first_name){
this.First_Name = first_name;
}

/** sets the surname of the person, requires String*/
public void set_Surname(String surname){
this.Surname = surname;
}

/** sets the title of the person, requires a String*/
public void set_Title(String title){
this.Title = title;
}

/** sets the dob of the person, requires 3 ints, YYYY, MM, DD */
public void set_Dob(int year, int month, int day){
this.dob.set(year, month, day);
}



public String get_Title(){
return this.Title;
}

public String get_Surname(){
return this.Surname;
}

public String get_First_Name(){
return this.First_Name;
}

public String get_dob(){
return this.dob.toString();
}

public int get_age(){
Date now = new Date();
int year;
year = now.getYear();
int dobyear;
dobyear = this.dob.get(1);
int age = year - dobyear;
return age;
}

public static void main(String args []){
Person person = new Person();
person.Person_create("mark", "newton", "mr", 2006, 01, 11);
////System.out.println("person created: " + person.get_First_Name() + person.get_Surname() + person.get_age() + person.get_dob());
}
}

Let me know what you think...

Thanks...

B14... aka... Marky Mark... the frozen monkey in the server room...
 
Point of interest for you : A missing import statement will never cause a NullPointerException.

You never instantiate 'dob'.

In the constructor, you should do :

this.dob = Calender.getInstance();

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
ah ok, didn't think of that,

thanks for the help and solution,



B14... aka... Marky Mark... the frozen monkey in the server room...
 
I feel ignored ...

Anyway, just in case you face more problems in the future, you could try debugging the application, that would give you the info for fixing it.

Cheers,
Dian
 
...my appologies, thanks for trying...

...not sure what you mean by debugging the application. I tried running it (via netbeans), but not sure what you mean by the debugger???

B14... aka... Marky Mark... the frozen monkey in the server room...
 
I believe you can also fix your problem by doing

dob = new Calendar();
dob.set(parameters...);

(assuming that Calendar has a default constructor written and set is a public mutator in the calendar class)

since you declared an instance of Calendar (dob) at the top you never initialized its data so throughout the execution dob has an address and its address points to null therefore you get that nullPointerException. once you use the key word new dob becomes and actual object that you can perform methods on.
 
I think we've done that point ....

Also, if you had bothered to check, Calendar is an abstract class, so cannot be instantiated using the "new" operator - my example is the only really valid way of creating a Calendar.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks for all the posts guys...

...they are very welcome. I am still learning to program in Java (messing around with JDBC and MYSQL at the moment). Its good to know that there are people like your selves out there willing to try and help me out.

My thanks for all your time and effort,

Seasons greatings to all.....

B14... aka... Marky Mark... the frozen monkey in the server room...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top