Skip to content
Advertisement

Creating multiple objects using the same instance

I just saw this tutorial creating multiple objects using the same instance by applying the DAO pattern and tried it in a simple console, but I always get this message java.lang.NullPointerException I’m now confused, as far as I know, a constructor can be used once only, and the object will be immutable. Kindly look at this:

Fighter.java

JavaScript

FightersDAO.java

JavaScript

Demo.java

JavaScript

Why is it null? What part did went wrong

Advertisement

Answer

The variable fighterDAO is never initialized. Therefore you get a NPE here:

JavaScript

To fix that use:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement