ImageIcon[] icon=new ImageIcon[7];
for(int i=0;i<7;i++) {
Icon img=new ImageIcon(Memory.class.getResource("imagea"+i+".png"));
下面是报错
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
at java.desktop/javax.swing.ImageIcon.(ImageIcon.java:217)
at MainFrame.(MainFrame.java:16)
at MainFrame.main(MainFrame.java:30)
附上测试类代码
MainFrame(){ p1=new JPanel(); Block block[]=new Block[30];
for(int i=0;i<30;i++) {
block[i]=new Block();
}
ImageIcon[] icon=new ImageIcon[7];
for(int i=0;i<7;i++) {
Icon img=new ImageIcon(Memory.class.getResource("imagea"+i+".png"));
}
p2=new MemoryTestPane(block,icon,5,6); p3=new JPanel(); this.add(p1,BorderLayout.NORTH); this.add(p2,BorderLayout.CENTER); this.add(p3,BorderLayout.SOUTH); this.setVisible(true); this.setSize(800, 700); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null);
}