Im trying to load a file from someone's computer and just put it into a label as an icon. When I try to run it I get a NullPointer error. It breaks when I get to the setIcon code
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TheChooser frame = new TheChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public TheChooser() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton searchButton = new JButton("Search Picture");
searchButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
fc.setFileFilter(filter);
int response = fc.showOpenDialog(null);
try{
if (response == JFileChooser.APPROVE_OPTION) {
String pathName = fc.getSelectedFile().getPath();
JOptionPane.showMessageDialog(null, pathName);
ImageIcon icon = new ImageIcon(pathName);
picPanel.setIcon(icon);
} else {
JOptionPane.showMessageDialog(null, "Feel Free to Look Later");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
searchButton.setBounds(141, 11, 139, 23);
contentPane.add(searchButton);
JLabel picPanel = new JLabel("");
picPanel.setIcon(null);
picPanel.setBounds(10, 58, 414, 192);
contentPane.add(picPanel);
}
What about this is not working?
I tried to do the fix down below but I still get a long list of errors
java.lang.NullPointerException
at chooser.TheChooser$2.mouseClicked(TheChooser.java:70)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
at chooser.TheChooser$2.mouseClicked(TheChooser.java:70)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
My guess, you have picPanel
declared as a class member. The JLabel picPanel
(towards the bottom) is not the one you are referencing from setIcon
in the mouseClicked
. It would cause a compile error if there wasn't a class member picPanel
because the local picPanel
would need to be declared before you are trying to access it in the mouseClicked
.
Moving the JLabel picPanel = new JLabel(""); to before you add the listener, may fix the problem. And get rid of the setIcon(null)
. But then again, I don't know what else in your code you may be trying to do with the picPanel
, so you may want to get rid of the local declaration, as it's shadowing the class member. So basically instead of
JLabel picPanel = new JLabel();
searchButton.addMouseListener(new MouseAdapter()
Use
picPanel = new JLabel();
searchButton.addMouseListener(new MouseAdapter()
Here are the fixes, in a complete program
public class TheChooser extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TheChooser frame = new TheChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
private JPanel contentPane;
private JFileChooser fc = new JFileChooser();
private JLabel picPanel;
public TheChooser() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
//JLabel picPanel = new JLabel("");
picPanel = new JLabel();
JButton searchButton = new JButton("Search Picture");
searchButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
fc.setFileFilter(filter);
int response = fc.showOpenDialog(null);
try {
if (response == JFileChooser.APPROVE_OPTION) {
String pathName = fc.getSelectedFile().getPath();
JOptionPane.showMessageDialog(null, pathName);
ImageIcon icon = new ImageIcon(pathName);
picPanel.setIcon(icon);
} else {
JOptionPane.showMessageDialog(null, "Feel Free to Look Later");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
searchButton.setBounds(141, 11, 139, 23);
contentPane.add(searchButton);
picPanel.setBounds(10, 58, 414, 192);
contentPane.add(picPanel);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.