Stijn Leenknegt
Stijn Leenknegt

Reputation: 1347

private property of my class is null

I've the follow code: http://aiids.pastebin.com/aLGYjraC

The problem is on line 84 it gives me a NullPointerException on programmaBesteller. When I debug, every private member (JMenu and JMenuItem) are null :s.

I don't see the problem because I do new DefaultMenuBar()...

Upvotes: 0

Views: 249

Answers (2)

Matthew Gilliard
Matthew Gilliard

Reputation: 9498

Here is some code which I think demonstrates the problem you are having:

public abstract class Super {

    public static void main(String[] args) {
        new Sub();
    }

    public Super() {
        printMe();
    }

    abstract void printMe();

    private static class Sub extends Super {

        private final Object x = new Object();

        public Sub() {
            super();
            printMe();
        }

        @Override
        protected void printMe() {
            System.out.println("printMe: " + x);
        }
    }
}

The output is:

printMe: null
printMe: java.lang.Object@1fee6fc

ie, the field x, which is explicitly initialised and looks like it cannot be null is not actually initialised at the time of the superclass constructor. Hope that makes it clearer.

Upvotes: 1

n0weak
n0weak

Reputation: 750

Maybe buildMenuProgramma method is called from the superclass constructor? Those fields are not yet instantiated on that phase.

Upvotes: 5

Related Questions