1

i have three classes. 1.Class:`

public class Handler {
private String name;
private String short_name;
private int semester; 
private int modul_number; 
private String prof; 
private int credits; 
private double note; 
private ArrayList<Handler_date> dates;
public Handler() {
}
public Handler(String _name, String _short_name, int _semester, int _modul_number,
 String _prof, int _credits, double _mark) {
 this.modul_number=_modul_number;
 this.name = _name;
 this.short_name = _short_name;
 this.semester = _semester;
 this.prof = _prof;
 this.credits = _credits;
 this.note= _mark;
 dates = new ArrayList<Handler_date>();
}
public void add_date(String _room, int _time, 
 String _day) {
 Handler_date temp = new Handler_date(_room, _time, 
 _day);
 dates.add(temp);
}`

and the 2.class (Elementclass):

`public class Handler_date {
private String room; 
private int time; 
private String day; 
public Handler_date() {
}
public Handler_date(String _room, int _time, 
 String _day) {
 this.room = _room;
 this.time = _time;
 this.day = _day;
}

}

I want to add a modul, but i get a NullPointerException for dates.add(temp);

i call the method with templist.search_modul_number(modulnumber).add_date("room", 1, "monday");

My Handler-Objects are saved in a extra Objectlist/class to an arrayList ...private ArrayList<Handler> handlerlist; Anyone an idea what I am doing wrong?

Thanks!

asked Feb 12, 2012 at 21:08
1
  • What's the full stack trace of the NullPointerException? Commented Feb 12, 2012 at 21:10

3 Answers 3

2

Most likely you're not initializing private ArrayList<Handler_date> dates;.

You have a no-args constructor for Handler which doesn't create a list.

Change it to:

public Handler() {
 dates = new ArrayList<Handler_date>();
}
answered Feb 12, 2012 at 21:11

Comments

1

Your dates arraylist is not initialized. It is null, so you get this exception.

Are you calling new Handler(); - in that constructor, you don't init dates.

And in the future, please attach the full stacktrace to get help.

answered Feb 12, 2012 at 21:11

Comments

0

Your dates field is not initialized. Probably because you created Handler instance using no-arg constructor.

answered Feb 12, 2012 at 21:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.