recommended way to access a components parent

Hi folks,
I've got a form whose top level jpanel is called 'mainPanel'. In the constructor for the form's bound class I have something like this:

KeyAdapter ka = new KeyAdapter() {
public void keyPressed(KeyEvent key) {
int code = key.getKeyCode();
if (code == KeyEvent.VK_DOWN) {
nextDocPressed();
} else if (code == KeyEvent.VK_UP) {
prevDocPressed();
} else if (code == KeyEvent.VK_RIGHT) {
nextPagePressed();
} else if (code == KeyEvent.VK_LEFT) {
prevPagePressed();
}
}
};
mainPanel.getParent().addKeyListener(ka); // ViewForm.java, line 87

However when I run it, I get:

java.lang.NullPointerException
at swingsearcher.gui.search.ViewForm.]]>(ViewForm.java:87)

What's the recommended way to add a keylistener to a form?

0

Please sign in to leave a comment.