By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,738 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,738 IT Pros & Developers. It's quick & easy.

problem with setIcon()

P: 39
Hi,
I am developing an application which needs to dynamically show whether a host is up or down.I have used buttons on which i am pasting the icon indicating up or down.So, when a node is down, I am replacing the icon that indicates up with the icon that indicates down using setIcon() function.But instead of changing the icon a NullPointerException is generated.For example,

private JButton c1;
JButton c1 = new JButton();
Icon bug = new ImageIcon("image/coms1.gif");//this image is for up
Icon bug3= new ImageIcon("image/computer.gif");//for down
c1.setIcon(bug);
if(node is down)
c1.setIcon(bug3);

Thanks for any help.

jerico
Nov 13 '06 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Hi,
I am developing an application which needs to dynamically show whether a host is up or down.I have used buttons on which i am pasting the icon indicating up or down.So, when a node is down, I am replacing the icon that indicates up with the icon that indicates down using setIcon() function.But instead of changing the icon a NullPointerException is generated.For example,

private JButton c1;
JButton c1 = new JButton();
Icon bug = new ImageIcon("image/coms1.gif");//this image is for up
Icon bug3= new ImageIcon("image/computer.gif");//for down
c1.setIcon(bug);
if(node is down)
c1.setIcon(bug3);

Thanks for any help.

jerico
There is nothing wrong with seIcon. The JButton c1 is the one that is null at the point of setting the icon. You have

Expand|Select|Wrap|Line Numbers
  1.  private JButton c1; 
  2. JButton c1 = new JButton();
  3.  
which does not compile like that so I'm assuming you have these at different places in your code. Post the code and let's see what's null.
Nov 13 '06 #2

P: 39
Hi,this is the program containing the problem.Sorry, I could not make the indentation proper.

jerico

public class my extends JFrame implements Runnable
{

private Container con ;
private GridBagLayout gb ;
private GridBagConstraints gbc;
private Icon bug,bug1,bug3;
private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
Thread runner ;
Random rg= new Random();
msconnect ms = new msconnect();
String[] item = ms.sel();
public my()
{
super("NETWOK LOOKUP");
con = getContentPane();
gb = new GridBagLayout();
con.setLayout(gb);
gbc= new GridBagConstraints();
Icon bug = new ImageIcon("image/coms1.GIF");
Icon bug1 = new ImageIcon("image/hor.gif");
Icon bug2= new ImageIcon("image/ver.gif");
Icon bug3= new ImageIcon("image/computer.gif");

JButton c1 = new JButton();
JButton c2 = new JButton();
JButton c3 = new JButton();
JButton c4 = new JButton();
JButton c5 = new JButton();
c1.setIcon(bug);
c2.setIcon(bug);
c3.setIcon(bug);
c4.setIcon(bug);
c5.setIcon(bug);
c1.setText("AGENT(38)");
c2.setText("AGENT(37)");
c3.setText("NMS(42)");
c4.setText("AGENT(33)");
c5.setText("AGENT(47)");

JButton l= new JButton(new ImageIcon("image/welcomew.gif"));

//minmize the size of a button
c1.setMargin(new Insets(0,0,0,0));
c2.setMargin(new Insets(0,0,0,0));
c3.setMargin(new Insets(0,0,0,0));
c4.setMargin(new Insets(0,0,0,0));
c5.setMargin(new Insets(0,0,0,0));

l.setMargin(new Insets(0,0,0,0));

c1.setIconTextGap(0);
c2.setIconTextGap(0);
c3.setIconTextGap(0);
c4.setIconTextGap(0);
c5.setIconTextGap(0);

// suppress borders
c1.setBorderPainted ( false );
c2.setBorderPainted ( false );
c3.setBorderPainted ( false );
c4.setBorderPainted ( false );
c5.setBorderPainted ( false );

l.setBorderPainted ( false );
// suppress button press decoration
c1.setContentAreaFilled( false );
c2.setContentAreaFilled( false );
c3.setContentAreaFilled( false );
c4.setContentAreaFilled( false );
c5.setContentAreaFilled( false );

l.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
c1.setDefaultCapable( false );
c2.setDefaultCapable( false );
c3.setDefaultCapable( false );
c4.setDefaultCapable( false );
c5.setDefaultCapable( false );
l.setDefaultCapable( false );

// hide focus rectangle
c1.setFocusPainted( false );
c2.setFocusPainted( false );
c3.setFocusPainted( false );
c4.setFocusPainted( false );
c5.setFocusPainted( false );

l.setFocusPainted( false );

c1.setHorizontalTextPosition( SwingConstants.CENTER );
c1.setVerticalTextPosition( SwingConstants.TOP );

c2.setVerticalTextPosition(SwingConstants.BOTTOM);
c2.setHorizontalTextPosition(SwingConstants.LEFT);

c3.setHorizontalTextPosition( SwingConstants.CENTER );
c3.setVerticalTextPosition( SwingConstants.BOTTOM );

c4.setVerticalTextPosition(SwingConstants.BOTTOM);
c4.setHorizontalTextPosition(SwingConstants.RIGHT) ;

c5.setHorizontalTextPosition( SwingConstants.CENTER );
c5.setVerticalTextPosition( SwingConstants.BOTTOM );

JButton l1 = new JButton(bug2);
JButton l2 = new JButton(bug1);
JButton l3 = new JButton(bug1);
JButton l4 = new JButton(bug2);

l1.setMargin(new Insets(0,0,0,0));
l2.setMargin(new Insets(0,0,0,0));
l3.setMargin(new Insets(0,0,0,0));
l4.setMargin(new Insets(0,0,0,0));

// suppress borders
l1.setBorderPainted ( false );
l2.setBorderPainted ( false );
l3.setBorderPainted ( false );
l4.setBorderPainted ( false );

// suppress button press decoration
l1.setContentAreaFilled( false );
l2.setContentAreaFilled( false );
l3.setContentAreaFilled( false );
l4.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
l1.setDefaultCapable( false );
l2.setDefaultCapable( false );
l3.setDefaultCapable( false );
l4.setDefaultCapable( false );

// hide focus rectangle
l1.setFocusPainted( false );
l2.setFocusPainted( false );
l3.setFocusPainted( false );
l4.setFocusPainted( false );

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l,0,0,0,2);

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c1,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(c2,4,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c3,4,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c4,4,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c5,6,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l1,3,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l2,4,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l3,4,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l4,5,3,1,1);
setLocation(200,170);
show();*/
runner = new Thread(this);
runner.start();

}

public void run()
{
String m="" ;
while(true)
{
int i= Math.abs(rg.nextInt())%4;//item.length;
m = give(item[i]);
if(m.equals("1"))
{

if(i== 1)
{

c1.setIcon(new ImageIcon("image/computer.gif"));

}
if(i== 3)
{
c5.setIcon(bug3);

}
}
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{ }

}
private void addComponent( Component c , int r , int col , int w , int h)
{
gbc.gridx = col ;
gbc.gridy = r;
gbc.gridwidth = w;
gbc.gridheight = h;
gb.setConstraints(c,gbc);
con.add(c);
}

public static void main (String args[])
{
my app = new my();
app.setVisible(true);
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
}
) ;
}
}
Nov 13 '06 #3

10K+
P: 13,264
Hi,this is the program containing the problem.Sorry, I could not make the indentation proper.

jerico

public class my extends JFrame implements Runnable
{

private Container con ;
private GridBagLayout gb ;
private GridBagConstraints gbc;
private Icon bug,bug1,bug3;
private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
Thread runner ;
Random rg= new Random();
msconnect ms = new msconnect();
String[] item = ms.sel();
public my()
{
super("NETWOK LOOKUP");
con = getContentPane();
gb = new GridBagLayout();
con.setLayout(gb);
gbc= new GridBagConstraints();
Icon bug = new ImageIcon("image/coms1.GIF");
Icon bug1 = new ImageIcon("image/hor.gif");
Icon bug2= new ImageIcon("image/ver.gif");
Icon bug3= new ImageIcon("image/computer.gif");

JButton c1 = new JButton();
JButton c2 = new JButton();
JButton c3 = new JButton();
JButton c4 = new JButton();
JButton c5 = new JButton();
c1.setIcon(bug);
c2.setIcon(bug);
c3.setIcon(bug);
c4.setIcon(bug);
c5.setIcon(bug);
c1.setText("AGENT(38)");
c2.setText("AGENT(37)");
c3.setText("NMS(42)");
c4.setText("AGENT(33)");
c5.setText("AGENT(47)");

JButton l= new JButton(new ImageIcon("image/welcomew.gif"));

//minmize the size of a button
c1.setMargin(new Insets(0,0,0,0));
c2.setMargin(new Insets(0,0,0,0));
c3.setMargin(new Insets(0,0,0,0));
c4.setMargin(new Insets(0,0,0,0));
c5.setMargin(new Insets(0,0,0,0));

l.setMargin(new Insets(0,0,0,0));

c1.setIconTextGap(0);
c2.setIconTextGap(0);
c3.setIconTextGap(0);
c4.setIconTextGap(0);
c5.setIconTextGap(0);

// suppress borders
c1.setBorderPainted ( false );
c2.setBorderPainted ( false );
c3.setBorderPainted ( false );
c4.setBorderPainted ( false );
c5.setBorderPainted ( false );

l.setBorderPainted ( false );
// suppress button press decoration
c1.setContentAreaFilled( false );
c2.setContentAreaFilled( false );
c3.setContentAreaFilled( false );
c4.setContentAreaFilled( false );
c5.setContentAreaFilled( false );

l.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
c1.setDefaultCapable( false );
c2.setDefaultCapable( false );
c3.setDefaultCapable( false );
c4.setDefaultCapable( false );
c5.setDefaultCapable( false );
l.setDefaultCapable( false );

// hide focus rectangle
c1.setFocusPainted( false );
c2.setFocusPainted( false );
c3.setFocusPainted( false );
c4.setFocusPainted( false );
c5.setFocusPainted( false );

l.setFocusPainted( false );

c1.setHorizontalTextPosition( SwingConstants.CENTER );
c1.setVerticalTextPosition( SwingConstants.TOP );

c2.setVerticalTextPosition(SwingConstants.BOTTOM);
c2.setHorizontalTextPosition(SwingConstants.LEFT);

c3.setHorizontalTextPosition( SwingConstants.CENTER );
c3.setVerticalTextPosition( SwingConstants.BOTTOM );

c4.setVerticalTextPosition(SwingConstants.BOTTOM);
c4.setHorizontalTextPosition(SwingConstants.RIGHT) ;

c5.setHorizontalTextPosition( SwingConstants.CENTER );
c5.setVerticalTextPosition( SwingConstants.BOTTOM );

JButton l1 = new JButton(bug2);
JButton l2 = new JButton(bug1);
JButton l3 = new JButton(bug1);
JButton l4 = new JButton(bug2);

l1.setMargin(new Insets(0,0,0,0));
l2.setMargin(new Insets(0,0,0,0));
l3.setMargin(new Insets(0,0,0,0));
l4.setMargin(new Insets(0,0,0,0));

// suppress borders
l1.setBorderPainted ( false );
l2.setBorderPainted ( false );
l3.setBorderPainted ( false );
l4.setBorderPainted ( false );

// suppress button press decoration
l1.setContentAreaFilled( false );
l2.setContentAreaFilled( false );
l3.setContentAreaFilled( false );
l4.setContentAreaFilled( false );

// suppress the ability of the button to be triggered by enter
l1.setDefaultCapable( false );
l2.setDefaultCapable( false );
l3.setDefaultCapable( false );
l4.setDefaultCapable( false );

// hide focus rectangle
l1.setFocusPainted( false );
l2.setFocusPainted( false );
l3.setFocusPainted( false );
l4.setFocusPainted( false );

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l,0,0,0,2);

gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c1,2,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL;
addComponent(c2,4,1,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c3,4,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c4,4,5,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(c5,6,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l1,3,3,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l2,4,2,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l3,4,4,1,1);
gbc.fill = GridBagConstraints.HORIZONTAL ;
addComponent(l4,5,3,1,1);
setLocation(200,170);
show();*/
runner = new Thread(this);
runner.start();

}

public void run()
{
String m="" ;
while(true)
{
int i= Math.abs(rg.nextInt())%4;//item.length;
m = give(item[i]);
if(m.equals("1"))
{

if(i== 1)
{

c1.setIcon(new ImageIcon("image/computer.gif"));

}
if(i== 3)
{
c5.setIcon(bug3);

}
}
}
try
{
Thread.sleep(1000);
}
catch(InterruptedException ie)
{ }

}
private void addComponent( Component c , int r , int col , int w , int h)
{
gbc.gridx = col ;
gbc.gridy = r;
gbc.gridwidth = w;
gbc.gridheight = h;
gb.setConstraints(c,gbc);
con.add(c);
}

public static void main (String args[])
{
my app = new my();
app.setVisible(true);
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e)
{
System.exit(0);
}
}
) ;
}
}
There you go
Expand|Select|Wrap|Line Numbers
  1.  
  2. private JButton c1,c2,c3,c4,c5,l1,l2,l3,l4,e1,e2,e3,e4;
  3.  
Declares the buttons fine. They are global to the class and this is what you want to do.

Then
Expand|Select|Wrap|Line Numbers
  1.  JButton c1 = new JButton(); 
  2. JButton c2 = new JButton();
  3. JButton c3 = new JButton();
  4. JButton c4 = new JButton();
  5. JButton c5 = new JButton();
  6.  
is not what you want to do. This hides the previously defined buttons in the class. (eg) When you access c1 from outside the constructor, the global c1 is referenced and this has not yet been initialised because the one declared in the constructor is not visible outside the constructor. You should just do
Expand|Select|Wrap|Line Numbers
  1.  c1 = new JButton(); 
  2. c2 = new JButton();
  3. c3 = new JButton();
  4. c4 = new JButton();
  5. c5 = new JButton();
  6.  
Inside the constructor.
Nov 13 '06 #4

P: 39
Thanks sir.You have saved me.

Jerico
Nov 14 '06 #5

10K+
P: 13,264
Thanks sir.You have saved me.

Jerico
don't you go about calling me "sir", what if I'm younger than you... And "saved" is an overstatement.
Call me anything you want but NOT "sir".
Nov 14 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.