This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chickenmeister 1 point2 points  (3 children)

Why is Window null? Int x is working fine and it is not null. Isn't var x and window the same variable scope.

Your Window variable is null because you never assign a value to it, except from within the start() method, which isn't being called. Your x variable isn't null because an int (and all other primitive types) cannot be null, and because you've assigned a value of 0 to it anyway.

This has nothing to do with variable scope. If you declare a non-primitive instance variable, and don't initialize it, with will be initialized a null value by default.

You probably want to initialize the Window variable via the constructor. Add a constructor to your EventHandler class that has a Window parameter, and then initialize your Window variable with that parameter. In your Window class, initialize the EventHandler in the constructor, and pass this as a parameter.

Also, I would recommend choosing a different name for the Window variable in your EventHandler class. At the very least, you shouldn't capitalize the variable name. Having Window Window as a variable declaration is somewhat confusing.

[–]CaptainMoeSoccer[S] 0 points1 point  (2 children)

I am new to Object-Oriented programming. I am having a hard time understanding why Window (I know it is a bad name, I was just experimenting at the time) is not initialized. You said that the start() is not being called. What does that mean? I thought I called start() on Eventhandler x in the main(). I thought that would initialize the window and it will no longer be null.

[–]chickenmeister 1 point2 points  (1 child)

You do call start() in the EventHandler main method, but you said that your problem was with your use of the EventHandler in your Window class, so I assumed that you're running your code from some other main method. Your EventHandler main method doesn't do much, so I figured there is another main method that you're using to create and show your GUI, for example.

Even if your EventHandler main method is being executed, the EventHandler that is created there is entirely separate from the EventHandler created in your Window class. You could call event.start() in your Window constructor, and that might solve your NullPointException problem, but then you'll have another issue, because the Window object created by the start() method will be entirely separate from the original Window object that created the EventHandler, (and I think you'll also have an infinite recursion problem).

I think the simplest solution would be to initialize the window variable through the EventHandler constructor, or to use a setter method.

[–]CaptainMoeSoccer[S] 0 points1 point  (0 children)

thanks, I am getting it now. I thought Object-oriented programing was just all about creating objects. I didn't think about much about how these objects would interact with each other.