I am trying to add an image to a JPanel
package testing;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class Testing extends JPanel
{
public static void main(String[] args)
{
BufferedImage myPicture;
try
{
myPicture = ImageIO.read(new File("./src/testing/cal.png"));
}
catch (IOException ex) {
// handle exception...
}
JLabel picLabel = new JLabel(new ImageIcon(myPicture));//error is here
Testing t = new Testing();
t.add(picLabel);
}
}
I get the following error
variable myPicture might not have been initialize
I thought i already initialise myPicture with the following line
myPicture = ImageIO.read(new File("./src/testing/cal.png"));
It seems I am wrong about it , how do i resolve this error so that I can add a image to a JPanel