Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
This is driving me batty! I am guessing there is some fundamental flaw. I just can't figure out whats wrong.
Note** All the code isn't included.
The error is pointing to ScrollbarListener.adjustmentValueChanged. All I am trying to do is get the value of the Scrollbar and display it as a label. Everytime the Scrollbar is moved I would like to update the label.
[ March 07, 2006: Message edited by: Jackie Davis ] [ March 07, 2006: Message edited by: Jackie Davis ]
And indeed, that's the case. You add(sbDeform), but you actually ininitialize a different, local variable named sbSmile, and add a listener to it -- but that ScrollBar is never added to the GUI anywhere.
This is a tiny bit better, as now there's an actual ScrollBar being added to the panel. But you're still using a local variable instead of that member; even though the names are now the same, they're still two separate variables. When the event handler tries to access the (still uninitialized) member, you get the NPE. The first line above should look like
sbDeform= new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 10);
Do you understand the problem now? It's actually quite important that you do -- this is a mistake that everybody makes once. Once you understand it, you won't make the same mistake again. When you specify the type for a variable in an assignment statement, like your version up there, you're declaring a new variable, distinct from any other previously existing ones. When you leave off that type, then you're referring to a variable that is declared elsewhere -- in this case, the member.
Joined: Mar 04, 2006
Thanks Ernest. That was very helpful.
I understand the concept now
Joined: Mar 04, 2006
Hi there, another concept Im struggling with
I have methods defined in a Mesh class. I want to use these methods as the scrollbar is adjusted - (i.e. within the ScrollbarListener class - geo.someMethod() ) However geo is inialised in the MeshTesterGLSpace class. How can I use this object (geo) in the ScrollbarListener class?