-1

I'm making something but I can't seem to make it work. I get a nullpointerexception, but I can't figure out why.

public class Bus implements Serializable {

    ArrayList<Reiziger> reizigers;

    public String add(Reiziger reiziger) {
        reizigers.add(reiziger);
        return "lijst";
    }
}

The nullpointer happens at line "reizigers.add(reiziger);" in the add method of Bus.java

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Deflandrex
  • 11
  • 1
  • 1
    is bus null or reiziger? – LLL May 03 '17 at 20:19
  • reiziger gets filled up in the view. – Deflandrex May 03 '17 at 20:23
  • Hi, greetings from Utrecht... for future questions, please try to write a title that is abput the 'root-cause' of the problem. The NPE is the cause, where it happens is very relevant. The title of this question is totally unrelated to the problem but just a statement of you want to achieve. If you have a root cause it is easier to find similar questions. – Kukeltje May 03 '17 at 21:26
  • See also this: http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Kukeltje May 03 '17 at 21:27
  • And btw, you do not need to pass the reiziger as a parameter. It nost likely is available in the scope already (but it does work and is not 'wrong' ) but in general it looks like your 'model' is to tightly coupled to the ui now. http://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao – Kukeltje May 03 '17 at 21:35

1 Answers1

2

You should initialize ArrayList<Reiziger> reizigers before use it:

List<Reiziger> reizigers = new ArrayList<>();

Also, note that List interface type is used for reizigers collections instead of ArrayList implementation. Read here the reason

Community
  • 1
  • 1
Ivan Pronin
  • 1,768
  • 16
  • 14