ただ単に画像を表示するだけのプログラムを作って実行したのですが次のエラーがでます
lang
1 Exception in thread "main" java.lang.NullPointerException 2 at javax.swing.ImageIcon.<init>(ImageIcon.java:181) 3 at game_st.MainPanel.loadImage(MainPanel.java:22) 4 at game_st.MainPanel.<init>(MainPanel.java:13) 5 at game_st.HelloWorld.<init>(HelloWorld.java:9) 6 at game_st.HelloWorld.main(HelloWorld.java:15)
ソースコードは
lang
1import java.awt.*; 2import javax.swing.*; 3 4public class HelloWorld extends JFrame{ 5 public HelloWorld(){ 6 setTitle("helloworld"); 7 MainPanel panel = new MainPanel(); 8 Container contentPane = getContentPane(); 9 contentPane.add(panel); 10 pack(); 11 } 12 public static void main(String args[]){ 13 HelloWorld frame = new HelloWorld(); 14 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 15 frame.setVisible(true); 16 } 17} 18
lang
1import java.awt.*; 2import javax.swing.*; 3 4public class MainPanel extends JPanel{ 5 private static final int WIDTH = 480; 6 private static final int HEIGHT = 480; 7 private Image heroImage; 8 9 public MainPanel(){ 10 setPreferredSize(new Dimension(WIDTH,HEIGHT)); 11 loadImage(); 12 } 13 public void paintComponent(Graphics g){ 14 super.paintComponent(g); 15 16 17 g.drawImage(heroImage, 0, 0, this); 18 } 19 private void loadImage(){ 20 ImageIcon icon = new ImageIcon(getClass().getResource("Image/hero.gif")); 21 heroImage = icon.getImage(); 22 } 23} 24
どうすれば良いでしょうか
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/07/08 13:23