Forums Register Login

FocusListener Null Pointer Exception

+Pie Number of slices to send: Send
I am trying to access the focus events a component within
another component.
I present here the basic idea of what I am trying to do.
Here is a Class containing a JTextField where I want to be notified of its FocusEvents from outside the class
<code><pre>public class PageContainingJTextField extends JPanel
{
JTextField newField = new JTextField();
public PageContainigJTextField()
{
//page initialization
}
public void addFocusListener(FocusListener newListener)
{
newField.addFocusListener(newListener);
}
}</pre></code>
And the class which calls it and uses the event information.
<code><pre>public class FocusPeeker implements FocusListener
{
PageContainingJTextField newPage = new PageContainingJTextField();
public FocusPeeker()
{
//other initilization
newPage.addFocusListener(this);
}
public void gainedFocus(FocusEvent e)
{
System.out.println("JTextField lost focus");
}
pubic void lostFocus(FocusEvent e)
{
System.out.println("JTextField gained focus");
}
}</pre></code>
I think I must be missing something.
I tried it both on Linux and Windows and both times received the nullPointer exception.
TIA,
Jeff Weinheimer

[This message has been edited by Jim Yingst (edited May 26, 2000).]
+Pie Number of slices to send: Send
Offhand, your code looks reasonable. The exceptions message should come with a stack trace too, which hopefully can identify exactly which line in which file is throwing the exception. You may need to compile using -g to get the most complete info possible here. If you post the exact message that may help - note that the line numbers won't mean as much to me without having the exact same files you have, so you should probably look in the files yourself to figure out exactly which statements are referenced in the call stack, at least for the first one or two in the list.
+Pie Number of slices to send: Send
Looking into the stack trace, the offending line occurs at
newField.addFocusListener(newListener);
in my above example.
(in the class containing the field to monitor)
If you add a JTextField to the FocusPeeker class, say JTF
then doing JTF.addActionListener(this) in the constructor of FocusPeeker properly registers and works.
Can it have something to do with the order in which the components are generated? If the FocusListener is registered prior to the JTextField in the JPanel being created, then there
would be no way to register an event with it. I noticed when I have a focus listener, on the other textfield that the focus gained and lost events are fired some time during the initilization of the object.
Yes, I believe this is whats happening. I just checked the pointer of the newField prior to the
newField.addFocusListener(newListener);
call and find it to be null.
Jeff Weinheimer
I think I can work around this by having a function which I call after the objects are created to register the action Listener.
Thanks again.
+Pie Number of slices to send: Send
Very strange. I ran this myself to get the stack trace:
<pre>Exception in thread "main" java.lang.NullPointerException
at PageContainingJTextField.addFocusListener(Test.java:14)
at javax.swing.JComponent.enableSerialization(JComponent.java:3861)
at javax.swing.JComponent.<init>(JComponent.java:200)
at javax.swing.JPanel.<init>(JPanel.java:66)
at javax.swing.JPanel.<init>(JPanel.java:100)
at PageContainingJTextField.<init>(Test.java:7)
at FocusPeeker.<init>(Test.java:20)
at Test.main(Test.java:38)</pre>
The problem seems to be that, during the initialization of the PageContainingFocusPeeker, part of the the JComponent initialization tries to add a focus listener for the PageContainingFocusListener. I'm not sure why it does this (has something to do with Serialization apparently) but it wouldn't be a problem except that you have redefined addFocusListener to add the listener to the JTextField, rather than to the Page object which owns the addFocusListener method. And since that object is still being initialized, its member fields such as newField haven't been initialized yet, and so newField is null when newField.addFocusListener() is called.
What I'd recommend is that you change the name of your Page's new addFocusListener method to create a new method rather than override the one inherited from Component, since that has its own responsibilities which have nothing to do with adding a focus listener to the JTextField contained within the Page. Change your new method to something like addFocusListenerToNewField(), and it should work fine. I think.
+Pie Number of slices to send: Send
Just what I needed!
Thanks to both of you.
this is supposed to be a surprise, but it smells like a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1417 times.
Similar Threads
JTextField Listener
JOptionPane on focusLost
Issue with selection of text when textfield component gains focus.
FocusListner not working on JMenu
regarding focus in swing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:51:04.