paintComponent overide = nullpointer

When the paintIcon line hits I get a huge stack trace with a nullpointerexception.  Any Ideas?

import javax.swing.*;
import java.io.*;
import java.awt.*;
import javax.imageio.*;

public class OnlineWindowPane extends JPanel{
      
      public Image bgImage;
      public ImageIcon bgIcon;
      private String userName;
      
      public OnlineWindowPane(){
            
            super(true);
            
            try{
            bgImage = ImageIO.read(new File("images/online.jpg"));
            ImageIcon bgIcon= new ImageIcon(bgImage);
        } catch(Exception e){}
       
       
      }
      
      public void paintComponent(Graphics g) {
        super.paintComponent(g);
        bgIcon.paintIcon(this,g,0,0);
    }
}

turtletimerAsked:
Who is Participating?
 
CEHJCommented:
Should have been:

 >>ImageIcon bgIcon= new ImageIcon(bgImage);

should be

bgIcon= new ImageIcon(bgImage);

of course
0
 
CEHJCommented:
>>mageIcon bgIcon= new ImageIcon(bgImage);

should be

bgIcon= new ImageIcon(bgImage);
0
 
aozarovCommented:
you probably want  to replace:
 bgIcon.paintIcon(this,g,0,0);

with
g.drawImage(bgIcon.getImage(), 0, 0, this);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
aozarovCommented:
For that you don't have to have an ImageIcon (Never tried the other way)...
0
 
turtletimerAuthor Commented:
I caught this right after I posted...tried to delete...but you fellas are to fast :)
0
 
CEHJCommented:
:-)

add(new JLabel(new ImageIcon("images/online.jpg")));

is another way of doing things
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.