This content has been marked as final. Show 10 replies
parent is null. When you try to call a method on it, you get a NPE
Try using a debugger
Been there...done that! It's null in the debugger also.
Do you have a setParent() method as well - was it initialized berfore calling getParent()
Or is text null assuming text is a string or string...
Without seeing more code those are guesses.
For getParent() to return non-null, this must have been added to some container previously.
C_OptionsPanel parent = (C_OptionsPanel)this.getParent();
will not work inside a constructor (for example), because the object is still being constructed, it has not been added to a container.
Why would I need to use setParent(). My first class calls the second class. The parent is set automatically....isn't it?
I have the getParent call inside the actionPreformed method of my class. Are you saying that somewhere in the constructor for my second class, I need to make a call to "this"?
Here's an example
JPanel foo = new JPanel, bar = new JLabel("bar");
Object o = bar.getParent(); // problem!!! no parent
Object o = bar.getParent(); // OK!! Now has parent
JPanel foo = new JPanel();
JLAbel bar = new JLabel("bar");
and so on...
Okay, I understand. When I run the app I get all my panels to show up from both classes (the first one and the second) so I am assuming that everything (even the radio button that causes the actionPreformed) is added.
I stumped with this one. Any other insight?
I cannot guess anymore what this is and what container your have added it to and where the button is and what you have added that to. I am not psychic. Whatever this represents in your original post does not have a parent (this means it has not been added to a container). There are several ways you could get the button to display, but not have the C_OptionsSubPanel added to a container. I have no idea from the two lines of code you have posted above what you did. This answer will require more information than I am able to get from the posts above.
Option 1: Post your code
Option 2: Find out why C_OptionsSubPanel is not being added to a container