Could anyone suggest me how to store .TIF formatted image to ImageIcon
and add this image to list model? I tried this but gives me java.lang.NullPointerException
.
public static void main(String[] args) throws Exception {
String path = "C:\\project\\aimages";
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
DefaultListModel listModel = new DefaultListModel();
System.out.println("listOfFiles.length="+listOfFiles.length);
int count = 0;
for (int i = 0; i < listOfFiles.length; i++) {
//System.out.println("check path"+listOfFiles[i]);
String name = listOfFiles[i].toString();
System.out.println("name"+name);
// load only JPEGs
if (name.endsWith("jpg") || name.endsWith("JPG")|| name.endsWith("tif") || name.endsWith("TIF")) {
if(name.endsWith("tif") || name.endsWith("TIF"))
{
BufferedImage image = ImageIO.read(listOfFiles[i]);
BufferedImage convertedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
ImageIcon ii = new ImageIcon(image);
Image img1 = ii.getImage();
Image newimg = img1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(img1);
listModel.add(count++, newIcon);
}
else
{
ImageIcon ii = new ImageIcon(ImageIO.read(listOfFiles[i]));
Image img1 = ii.getImage();
Image newimg = img1.getScaledInstance(75, 75, java.awt.Image.SCALE_SMOOTH);
ImageIcon newIcon = new ImageIcon(newimg);
listModel.add(count++, newIcon);
}
}
}
JList p2 = new JList(listModel);
}
}
here i had edited my code and this is my error msg Exception in thread "main" java.lang.NullPointerException at javax.swing.ImageIcon.(ImageIcon.java:228) at ListImage1.main(ListImage1.java:48)
image
other than for querying the dimensions, thusconvertedImage
would not contain any actual image information. You're missing the conversion here. – DominickdominieListImage1
code. – Milden