1

画像を取得してGridLayoutに配置されたタイルに変換しようとしていますが、実行時エラーが発生します。

コードは次のとおりです。

public class TileList extends JPanel {

private static final int width = 16;            //width of a tile
private static final int height = width;
private int col = 1;
private int row = 1;

private BufferedImage image;
File tilesetImage = new File("image.png");
BufferedImage tileset[];

public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;
    BufferedImage tileset[] = new BufferedImage[col*row];
}// end loadAndSplitImage

public TileList() {
    loadAndSplitImage(tilesetImage);
    setLayout(new GridLayout(row,col,1,1));
    setBackground(Color.black);

    int x=0;
    int y=0;
    for (int i = 0; i < (col*row); i++) {
        JPanel panel = new JPanel();
        tileset[i] = new BufferedImage(width, height, image.getType());  //first error
        tileset[i] = image.getSubimage(x,y,x+width,y+height);
        panel.add(new JLabel(new ImageIcon(tileset[i])));
        add(panel);
        x+=width;
        y+=height;
    }// end for loop
}// end constructor

public static void createAndShowGui() {
    JFrame frame = new JFrame("TileList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TileList());      //second error
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}//end createAndShowGui

public static void main (String [] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run () {
            createAndShowGui();           //third error
        }// end run
    });// end invokeLater
  }// end main
}// end class

これは私が受け取るエラーです:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TileList.<init>(TileList.java:55)
    at TileList.createAndShowGui(TileList.java:73)
    at TileList$1.run(TileList.java:82)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

このエラーは何を意味し、どのようにコードを間違って書いたのですか?

編集:Hovercraftが提案したようにコードを変更しましたが、新しいエラーが発生しました:

tileset[] = new BufferedImage[col*row];
       ^                           error: not a statement

tileset[] = new BufferedImage[col*row];
         ^                         error: ';' expected

tileset[] = new BufferedImage[col*row];
                             ^     error: not a statement
4

1 に答える 1

2

エラーは、変数をシャドウイングしていることです。クラスで既に宣言されている場合は、コンストラクター内で変数tileSetを再宣言しています。したがって、コンストラクターで宣言されたローカル変数のみが初期化されるため、クラスで宣言されたtileSet変数は初期化されません。

解決策:クラスでtileSetを宣言するのは1回だけです。

したがって、これを行わないでください。

public class TileList extends JPanel {

  //.... deleted for brevity

  BufferedImage tileset[];

  public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;
    BufferedImage tileset[] = new BufferedImage[col*row]; // *** re-declaring variable!
  }

しかし、代わりにこれを行います:

public class TileList extends JPanel {

  //.... deleted for brevity

  BufferedImage tileset[];

  public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;

    // ************
    // BufferedImage tileset[] = new BufferedImage[col*row]; // *****
    tileset[] = new BufferedImage[col*row]; // **** note the difference? ****
  }

この孤立した問題の解決策よりもさらに重要なのは、NPE(NullPointerExceptions)を解決する方法を理解することです。重要なのは、例外をスローする行のすべての変数をチェックし、どの変数がnullであるかを確認してから、使用する前にそれらの1つが正しく初期化されていない場所を確認することです。

于 2013-01-06T02:34:29.927 に答える