I have read your crossposted thread and found your updated code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageBrowser extends JFrame {
// ====================================================== fields
JTextField _fileNameTF = new JTextField(15);
JTextField _wordCountTF = new JTextField(4);
JFileChooser _fileChooser = new JFileChooser();
// ================================================= constructor
ImageBrowser() {
String newline = System.getProperty("line.separator");
_fileNameTF.setEditable(false);
_wordCountTF.setEditable(false);
JPanel content = new JPanel();
content.setLayout(new FlowLayout());
content.add(new JLabel("File Name:" + newline));
content.add(_fileNameTF);
content.add(new JLabel(newline + "Preview:"));
JMenuBar menubar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenuItem openItem = new JMenuItem("Open...");
openItem.addActionListener(new OpenAction());
menubar.add(fileMenu);
fileMenu.add(openItem);
this.setJMenuBar(menubar);
this.setContentPane(content);
this.setTitle("Select a Image File");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack(); // Layout components.
this.setLocationRelativeTo(null); // Center window.
}
public static String fileimage;
// /////////////////////////////////////////////////// OpenAction
class OpenAction implements ActionListener {
public void actionPerformed(ActionEvent ae) {
int retval = _fileChooser.showOpenDialog(ImageBrowser.this);
if (retval == JFileChooser.APPROVE_OPTION) {
File file = _fileChooser.getSelectedFile();
_fileNameTF.setText(file.getName());
File filetemp = _fileChooser.getSelectedFile();
String fileimage = filetemp.getName();
}
}
}
// ========================================================= main
static String path = fileimage;
public static void main(String[] args) throws IOException {
JFrame window = new ImageBrowser();
window.setVisible(true);
BufferedImage image = ImageIO.read(new File(path));
ImageBrowser test = new ImageBrowser(image);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(new JScrollPane(test));
window.setSize(400, 400);
window.setLocation(200, 200);
window.setVisible(true);
}
private static void showIcon(BufferedImage image) {
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon, JLabel.CENTER);
JOptionPane.showMessageDialog(null, label, "icon", -1);
}
BufferedImage image;
Dimension size = new Dimension();
public ImageBrowser(BufferedImage image) {
this.image = image;
size.setSize(image.getWidth(), image.getHeight());
}
static String imgfile = fileimage;
protected void paintComponent(Graphics g) {
int x = (getWidth() - size.width) / 2;
int y = (getHeight() - size.height) / 2;
g.drawImage(image, x, y, this);
}
public Dimension getPreferredSize() {
return size;
}
}
Compiling it gives the following error:
Exception in thread "main" java.lang.NullPointerException
at java.io.File.<init>(Unknown Source)
at ImageBrowser.main(ImageBrowser.java:71)
Looks like its to do with:
BufferedImage image = ImageIO.read(new File(path));
Because this is in your main method, when compiled, the program is looking for an image that doesn't exist.
static String path = fileimage;
fileimage is not set.