2012-05-12 198 views
0

我正在嘗試使用掃描儀來編輯我的塔防遊戲的級別。但它不會更新自定義文件的級別(平鋪圖像)(0是草1是石-1沒有任何內容等)。我發現了錯誤,但我如何解決它,我需要添加/更改以擺脫此問題?Java掃描儀問題(JFrame)

java.lang.NullPointerException 
    at Levels.loadLevels(Levels.java:11) 
    at Window.define(Window.java:28) 
    at Window.paintComponent(Window.java:44) 

線11:for(int y=0;y<Window.room.block.length;y++) { 線28:levels.loadLevels(new File("levels/level1.level")); 線44:define();

這是掃描儀的文件:

import java.io.*; 
import java.util.*; 

public class Levels { 
    public void loadLevels(File loadPath) { 
     try { 
      Scanner loadLevelsScanner = new Scanner(loadPath); 

      while(loadLevelsScanner.hasNext()) { 

       for(int y=0;y<Window.room.block.length;y++) { 
        for(int x=0;x<Window.room.block[0].length;x++) { 
         Window.room.block[y][x].groundID = loadLevelsScanner.nextInt(); 
        } 
       } 

       for(int y=0;y<Window.room.block.length;y++) { 
        for(int x=0;x<Window.room.block[0].length;x++) { 
         Window.room.block[y][x].airID = loadLevelsScanner.nextInt(); 
        } 
       }  
      }    
      loadLevelsScanner.close(); 

     } catch(Exception e) {    
     } 
    } 
} 

這是窗口文件:

import javax.swing.*; 
import java.awt.*; 
import java.awt.image.*; 
import java.io.*; 

public class Window extends JPanel implements Runnable { 

    public Thread thread = new Thread(this);   
    public static Image[] tileset_ground = new Image[100]; 
    public static Image[] tileset_air = new Image[100];  
    public static int myWidth, myHeight;   
    public static boolean isFirst = true;  
    public static Room room; 
    public static Levels levels; 

    public Window() { 
     thread.start(); 
    } 

    public void define() { 
     room = new Room(); 
     levels = new Levels();   
     levels.loadLevels(new File("levels/level1.level")); 

     for(int i=0;i<tileset_ground.length; i++) { 
      tileset_ground[i] = new ImageIcon("resources/tileset_ground.png").getImage(); 
      tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32))); 
     } 

     for(int i=0;i<tileset_air.length; i++) { 
      tileset_air[i] = new ImageIcon("resources/tileset_air.png").getImage(); 
      tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32))); 
     }  
    } 

    public void paintComponent(Graphics g) { 
     if(isFirst) { 
      define();    
      isFirst = false; 
     }   
     g.clearRect(0, 0, getWidth(), getHeight());   
     room.draw(g); 
    } 

    public void run() { 
     while(true) {    
      if(!isFirst) { 
       room.physic(); 
      }    
      repaint();    
      try { 
       Thread.sleep(1); 
      } catch(Exception e) { 
      }    
     }   
    }  
} 

這是T.他的房間文件:

import java.awt.*; 

public class Room { 
    public int worldWidth = 40; 
    public int worldHeight = 20; 
    public int blockSize = 32; 

    public Block[][] block; 

    public Room() { } 
    public void define() { }  
    public void physic() { } 

    public void draw(Graphics g) { 

     block = new Block[worldHeight][worldWidth];   

     for(int y=0;y<block.length;y++) { 
      for(int x=0;x<block[0].length;x++) { 
       block[y][x] = new Block(x * blockSize, y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir); 
       block[y][x].draw(g); 
      } 
     } 
    }  
} 

這是塊文件:

import java.awt.*; 

public class Block extends Rectangle { 
    public int groundID; 
    public int airID; 

    public Block(int x, int y, int width, int height, int groundID, int airID) { 
     setBounds(x, y, width, height); 

     this.groundID = groundID; 
     this.airID = airID; 
    } 

    public void draw(Graphics g) { 
     g.drawImage(Window.tileset_ground[groundID], x, y, width, height, null); 

     if(airID != Value.airAir) { 
      g.drawImage(Window.tileset_air[airID], x, y, width, height, null); 
     }   
    }  
} 

最後一點,這是自定義文件掃描儀應該閱讀:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 

抱歉愚蠢的問題,我是一個初學者。

+4

*「是什麼讓我很沮喪最多的就是沒有錯誤」 *'}趕上(例外五){ }'更改是(每類似的聲明),以'趕上(例外e){e.printStackTrace(); '(或者交替留在黑暗中)。 –

+1

你如何知道自從你吞下你的異常之後沒有錯誤:'catch(Exception e){}'。很明顯,如果你忽略了可能拋出的異常,你會得到一個沒有錯誤的印象。但是這種印象很有可能不會成爲現實。 –

+0

*「對不起,很長的帖子」*爲了更好地幫助,請發佈[SSCCE](http://sscce.org/)。 –

回答

1

快速和骯髒的解決方案是,以測試Window.room不爲空,以及.block:

 Scanner loadLevelsScanner = new Scanner (loadPath); 
     if ((Window.room != null) && 
      (Window.room.block != null)) { 
      // ... block until catch block 
     } 

一個簡單的Testapp我寫的作品,到目前爲止,如果做所以。

但是,您需要了解「靜態」是什麼,以及爲什麼以及如何使用它。一個常見的初學者錯誤是,爲了使編譯器保持沉默,需要使用「靜態」關鍵字。

調查,以什麼順序初始化您的類和它們的屬性。

在塊中,要訪問窗口,必須有參考。該參考文件可以傳遞給Block:

class Block extends Rectangle { 
    public int groundID; 
    public int airID; 
    Window window; 

    public Block (int x, int y, int width, int height, int groundID, int airID, Window window) { 
     setBounds (x, y, width, height); 
     this.groundID = groundID; 
     this.airID = airID; 
     this.window = window; 
    } 
    public void draw (Graphics g) { 
     g.drawImage (window.tileset_ground [groundID], x, y, width, height, null); 
     if (airID != Value.airAir) { 
      g.drawImage (window.tileset_air [airID], x, y, width, height, null); 
     } 
    } 
} 

誰創建了Blocks?這是房間,所以房間本身需要了解窗戶(只要你不從根本上改變你的設計)。

public Room (Window w) { 
    block = new Block [worldHeight] [worldWidth]; 
    for (int y=0; y <block.length; y++) { 
     for (int x=0; x <block [0].length; x++) { 
      block [y] [x] = new Block (x * blockSize, y * blockSize, blockSize, blockSize, Value.groundGrass, Value.airAir, w); 
     } 
    } 
} 

塊數組被創建,初始化,塊被傳遞給Window參數。

在平局,你不要一遍又一遍地重新排列一遍,你也不重建塊,但只是重繪他們:

public void draw (Graphics g) { 
    for (int y=0; y <block.length; y++) { 
     for (int x=0; x <block [0].length; x++) { 
      block [y] [x].draw (g); 
     } 
    } 
} 

窗口,創建房間,並通過它的窗口參考:

public void define() { 
    room = new Room (this); 
    levels = new Levels(); 
+0

好吧,我知道只有Window.room.block爲空,但我該如何解決它? – core16

+0

我想我需要在Room.java中改變一些東西,但是什麼? – core16

+0

也許你可以把'block ='的初始化放入Room的ctor中?我想知道爲什麼你需要在每次調用'draw'時創建一個新的Block數組。但是我不確定你的代碼的語義。你不僅重新創建了塊數組,而且還重新創建了塊。 –