Artish1
Artish1

Reputation: 143

Enum variable is found to be null when not set that way

Whenever I try to get a field from an enum, it always returns null. I've set the value of it according to the enum constructor. It still comes out null.

public enum TeamType {
    RED("Red",ChatColor.RED,DyeColor.RED,Point.RED),
    BLUE("Blue",ChatColor.BLUE,DyeColor.BLUE,Point.BLUE);  //<----Set as Point.RED/BLUE



    private String name;
    private int crystalHealth = 50;
    private Point point;
    private int teamPoints;
    private ChatColor chatColor;
    private DyeColor dye;
    private HashSet<ArenaPlayer> playerList = new HashSet<>();
    private List<ArenaPlayer> queue = new ArrayList<ArenaPlayer>();
    private Location spawn;


    public Point getPoint()
    {
        if(point == null)
            System.out.println("WHY? for: " + this.toString()); //<---This always runs

        return point;
    }

    private TeamType(String name,ChatColor color,DyeColor dye,Point point1) {
        this.name = name;
        this.point = point1; // <--- My assignment
        this.dye = dye;
        this.chatColor = color;
    }

The Point enum class

public enum Point{
    RED(ChatColor.RED + "Red",TeamType.RED),
    BLUE(ChatColor.BLUE + "Blue",TeamType.BLUE),
    NEUTRAL(ChatColor.WHITE +"None",null);

    private String name;
    private TeamType teamOwned;
    private Point(String name,TeamType team) {
        this.name = name;
        teamOwned = team;
    }

    public TeamType getTeamOwned() {
        return teamOwned;
    }

     public String getName() {
        return name;
    }

     @Override
    public String toString() {
        return name;
    }

There's obviously something that's happening outside of my knowledge of Java. Could it possibly be that the Point Enum is not initialized yet when TeamType enum is. This could explain why it's null.

I need some help.

Upvotes: 0

Views: 924

Answers (2)

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

Suppose we have a class called A which has class B’s Object. (in UML terms A HAS B). at the same time we class B is also composed of Object of class A (in UML terms B HAS A). obviously this represents circular dependency because while creating the object of A, the compiler must know the size of B... on the other hand while creating object of B, the compiler must know the size of A. this is something like egg vs. chicken problem...

to avoid such type of circular dependency you can use any proxy interface .

https://dzone.com/articles/tackling-circular-dependency

Upvotes: 0

MC Emperor
MC Emperor

Reputation: 23057

Well, I think the source of the problem is that you have a circular reference. TeamType has a Point reference and vice versa.

Suppose the TeamType enum class is initialized, then the enum constants are initialized as well. These refer to Point, which is in turn initialized. The classloader loads the Point class, but will not initialize TeamType again. At this point, properties you expect to be non-null are still null.


The JLS § 12.4 defines this process.

Upvotes: 3

Related Questions