Creating multiple objects using the same instance

Tags: , ,

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:

public class Fighter {
    private String style;

    public Fighter() {}

    public String getStyle() {
        return style;
    public void setStyle(String style) { = style;

public class FightersDAO {
    public List<Fighter> getFighters(){

        List <Fighter> fighter = new ArrayList<>();
        String [] styles= { "Karate", "Sumo", "Pro-Wrestling" };

        for(int i=0; i < styles.length; i++) {
            Fighter temp = new Fighter();;
        return fighter;

public class Demo {
    private static FightersDAO fighterDAO;
    public static void main (String [] args) {
        List <Fighter> fighters = fighterDAO.getFighters();

        for(Fighter e: fighters) {
            System.out.println(e.getStyle()); //this should output the objects, but nothing shows

Why is it null? What part did went wrong


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

List <Fighter> fighters = fighterDAO.getFighters();

To fix that use:

private static FightersDAO fighterDAO = new FightersDAO();

Source: stackoverflow