JComponet.getName() returning null
I have various component. on JPanel.One of them is given some name using componet.setName("xyz").
I have to perform Action infousListenerfocuslost event depending on e.getComponent.getName()
if (e.getComponent.getName()==null ) //do this
else if (e.getComponent.getName().equals"xyz"l ) //do that.
when component .getName() returns null I get null pointer exception and code with in"if" block never gets executed.
how do I take care of null pointer exception here.Any help is greatly appreciated .Thanks in Advance
[552 byte] By [
ushradhaa] at [2007-11-26 18:37:03]

I don't think thats your problem. I think e is equal to null not the name,. for examples try this
import javax.swing.*;
public class NameTest
{
public static void main(String[] args)
{
JButton button = new JButton();
if (button.getName() ==null)
{
System.out.println("I dont have a name");
}
}
}
Message was edited by:
kikemelly
If you still dont understand then post some more code which shows your component objects. I don't think you have initialised them properly. The component is null not not the name.
you would onl;y get the initial exception if you are not getting the component correctly and 'e' is null.could stick it in a try/catch block and catch the exeption
If yo uare dealing with JButtons, you might want to use:((JButton)fe.getComponent()).getLabel()Or you might want to trap on focusGained for:if ( myButton.hasFocus() )~Bill
Like Kikemelly pointed its the JComponent that becoming null.
All my componentd are intialised but as soon as any of the JButton gains focus after JTable has lost it. they become null and null pointer exception get thrown for this component.
JButton btn=new JButton;
btn.setName("BTN1");
JTable searchTable=new JTable;
JButton btn2=new JButton;
FocusListener istener ;
listener = new FocusAdapter()
{
public void focusLost(FocusEvent e)
{
if (e.getComponent() instanceof JTable)
{
System.out.println("JTABLE " + e.getOppositeComponent());
if (e.getOppositeComponent().getName()==null)
{
//perform following action
//showFrame = true;
//statusButton_actionPerformed();
}
}
}
};
searchTable.addFocusListener(listener);
I have to perform certain action when j table looses focus but next component to gain focus is NOT btn.
Swing related questions should be posted in the Swing forum.
If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.
Try this code out and watch closely what you get back from FocusListener. If youhave further questions post code if you would.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SillyFrame extends JFrame
implements FocusListener {
JPanel jp;
JButton but1,but2,but3;
public SillyFrame(String title) {
super(title);
but1 = new JButton("But 1");
but2 = new JButton("But 2");
but3 = new JButton("But 3");
but1.addFocusListener(this);
but2.addFocusListener(this);
but3.addFocusListener(this);
jp = new JPanel();
jp.add(but1);
jp.add(but2);
jp.add(but3);
this.getContentPane().add(jp, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public void focusGained(FocusEvent fe) {
System.out.println("GAINED: "
+(but1.hasFocus() ? "but1 " : " ")
+(but2.hasFocus() ? "but2 " : " ")
+(but3.hasFocus() ? "but3 " : " ")
+fe.getComponent().getName()+" "
+fe.getComponent().getClass().getName()+" "
+((JButton)fe.getComponent()).getLabel());
}
public void focusLost(FocusEvent fe) {
System.out.println("LOST : "
+(!but1.hasFocus() ? "but1 " : " ")
+(!but2.hasFocus() ? "but2 " : " ")
+(!but3.hasFocus() ? "but3 " : " ")+" "
+fe.getComponent().getClass().getName()+" "
+((JButton)fe.getComponent()).getLabel());
}
}
public class SillyClass {
public static void main(String args[]) {
SillyFrame samplejframe = new SillyFrame("SillySilly");
samplejframe.setVisible(true);
}
}
JButton is not getting null anymore.But I run in to another issue
This is my dummy code which I wrote to paste on forum .
/* Requirement as soon as jtable looses focus Status btn text should become show Result and frame should be hidden but when status button is clicked toggling of frame should happen normal **/
/** Problem
when textFiled1 gains focus after button2 has lost it Frame doesnt hide if it was visible
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.util.ArrayList;
import javax.swing.*;
public class DummyCode extends JPanel implements ActionListener {
JComboBox cBox;
String[] cBoxOption;
JButton button1;
JButton button2;
JToolBar jToolBar1;
JFrame frame;
boolean showFrame;
private String newline = "\n";
JTextField textField1;
FocusListener listener;
JTable tbl;
public DummyCode() {
textField1 = new JTextField();
textField1.setColumns(10);
button1 = new JButton("Search");
button2 = new JButton("Status");
button2.setName("BTNStatus");
cBoxOption = new String[] { "A", "B", "C" };
cBox = new JComboBox(cBoxOption);
jToolBar1 = new JToolBar();
showFrame = false;
frame = new JFrame();
tbl = new JTable();
jbInit();
}
void jbInit() {
jToolBar1.addSeparator();
jToolBar1.add(cBox);
jToolBar1.addSeparator();
jToolBar1.add(textField1);
jToolBar1.addSeparator();
jToolBar1.addSeparator();
jToolBar1.add(button1);
jToolBar1.addSeparator();
jToolBar1.add(button2);
jToolBar1.addSeparator();
jToolBar1.addSeparator();
jToolBar1.addSeparator();
this.add(jToolBar1);
frame.setContentPane(tbl);
frame.setSize(400, 200);
frame.setLocation(666, 500);
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed();
}
});
textField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
showFrame = false;
button1_actionPerformed(e);
}
});
listener = new FocusAdapter() {
// when table looses the focus status button text should be "Show
// result" and
// and frame gets hidden
public void focusLost(FocusEvent e) {
if (e.getComponent() instanceof JTable)
if (e.getOppositeComponent().getName() == null) {
showFrame = true;
button2_actionPerformed();
}
}
};
tbl.addFocusListener(listener);
}
void button1_actionPerformed(ActionEvent e)
{
loadsearchResults();
}
// Toggle frame visisbility show /hide
void button2_actionPerformed()
{
if (!showFrame) {
frame.setVisible(true);
showFrame = true;
button2.setText("Hide Result");
} else {
frame.setVisible(false);
showFrame = false;
button2.setText("Show Result");
}
}
public void actionPerformed(ActionEvent e) {
}
public void loadsearchResults() {
frame.repaint();
frame.setVisible(true);
showFrame = true;
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("ObjectSearch");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DummyCode demo = new DummyCode();
frame.setContentPane(demo);
frame.setSize(800, 100);
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Based on above code I have following scenario
"button2 " should toggle frame (show/hide) and change text of button2 accordingly
button1 should bring the Jframe containing JTable
whenever Jtable looses focus.It should hide frameand text of of button2 should become "showResult"
But afte Button2 looses focus and TextField1 gains focus .Frame doesnt hide ?
Unless I totally don't know what you want to do, try this.
listener = new FocusAdapter() {
// when table looses the focus status button text should be "Show
// result" and
// and frame gets hidden
public void focusGained(FocusEvent e) {
System.out.println("In focusGained");
showFrame = true;
button2_actionPerformed();
}
};
textField1.addFocusListener(listener);
Why are you using a JTable here?
~Bill
Thanks for your answer but problem still persists
Its about Jtable loosing focus.It doesnt matter who else gains the focus so i add jtable. focus lost event.
I am facing another issue here
whenever table looses focus frame disappears from main screen but when i minimize all my windows on machine i see it on desktop so it doesnt hide really.
What can be done to rectify it.
Any help greatly appreciated.
I ran the last code you posted and I don't see anything geting lost ... I don't know what you mean. You have to do a better job of describing your problem.