这里的NullPointerException是怎么回事
来源:百度知道 编辑:UC知道 时间:2024/09/13 00:16:53
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
public class PreView extends JFrame{
private final String path = "D:\\Workspace\\p";
private JScrollPane sp3=new JScrollPane();
public PreView() {
setSize(1000, 800);
File f = new File("path");
String[] show=f.list();
// int n=show.length;
for(int i=0;i<show.length;i++){
sp3.add(new JLabel(new ImageIcon(show[i])));
}
getContentPane().add(sp3);
}
/**
* @param args
*/
public static void main (String[] args) {
// TODO Auto-generated method stub
PreView jf=new PreView();
jf.setVisible(true);
}
运行后,出现
Exception in thread "main" java.lang.NullPointerException
at PreView.<init>(PreView.java:18)
at Pre
class PreView extends JFrame {
private final String path = "D:\\Workspace\\p";
private JScrollPane sp3 = new JScrollPane();
public PreView() {
setSize(1000, 800);
File f = new File(path);
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException ex) {
Logger.getLogger(PreView.class.getName()).log(Level.SEVERE, null, ex);
}
}
String[] show = f.list();
// int n=show.length;
for (int i = 0; i < show.length; i++) {
sp3.add(new JLabel(new ImageIcon(show[i])));
}
getContentPane().add(sp3);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PreView jf = new PreView();