• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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);
    }
}

0
turtletimer
Asked:
turtletimer
  • 3
  • 2
1 Solution
 
CEHJCommented:
>>mageIcon bgIcon= new ImageIcon(bgImage);

should be

bgIcon= new ImageIcon(bgImage);
0
 
CEHJCommented:
Should have been:

 >>ImageIcon bgIcon= new ImageIcon(bgImage);

should be

bgIcon= new ImageIcon(bgImage);

of course
0
 
aozarovCommented:
you probably want  to replace:
 bgIcon.paintIcon(this,g,0,0);

with
g.drawImage(bgIcon.getImage(), 0, 0, this);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now