G
Gelöschtes Mitglied 34033
Gast
tach alle zusammen
ich schreib zurzeit ein 2d spiel in java mit awt & swing (ist so ähnlich wie minecraft) und bin auf folgendes problem gestoßen:
wenn ich einen array mit this initialisiere wird die nullpointerexception geworfen
hier mein code in Block.java:
kann mir jemand helfen???
ich schreib zurzeit ein 2d spiel in java mit awt & swing (ist so ähnlich wie minecraft) und bin auf folgendes problem gestoßen:
wenn ich einen array mit this initialisiere wird die nullpointerexception geworfen
hier mein code in Block.java:
Java:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package swinggame;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
/**
*
* @author Roman
*/
class Block {
public static final Block dirt = new Block(0, 2, "dirt");
public static final Block grass = new Block(1, 3, "grass");
public static final Block stone = new Block(2, 1, "stone");
public static final Block glass = new Block(3, 49, "glass");
public static final Block lava = new Block(4, 255, "lava");
public static final Block sand = new Block(5, 18, "sand");
public static final Block sandstone = new Block(6, getDefaultTexture().getImage().getSubimage(0, 192, 16, 16), "sandstone");
public static final Block snow = new Block(7, 68, "snow");
public static final Block ice = new Block(8, 67, "ice");
public static final Block water = new Block(9, 223, "water");
public static final Block cactus = new Block(10, 70, "cactus");
public static int blockSize = 50;
public static Block[] blockList = new Block[5000];
public BufferedImage blockTexture;
public String blockName;
public int blockId;
public Block(int id, String texture, String name) {
if (texture == null) {
throw new GameCrashException("Texture can't be null!");
}
try {
blockTexture = ImageIO.read(this.getClass().getResourceAsStream(texture));
} catch (IOException ex) {
Logger.getLogger(Block.class.getName()).log(Level.SEVERE, null, ex);
}
this.blockName = name;
blockId = id;
if (blockList[id] != null) {
throw new GameCrashException("You can't override " + blockList[id].blockName + " with " + blockName + "!");
} else {
blockList[id] = this;
}
}
public Block(int id, BufferedImage texture, String name) {
if (texture == null) {
throw new GameCrashException("Texture can't be null!");
}
blockTexture = texture;
this.blockName = name;
blockId = id;
if (blockList[id] != null) {
throw new GameCrashException("You can't override " + blockList[id].blockName + " with " + blockName + "!");
} else {
blockList[id] = this;
}
}
public Block(int id, int tile, String name) {
Texture t = getDefaultTexture();
blockTexture = t.cut(tile);
this.blockName = name;
blockId = id;
if (blockList[id] != null) {
throw new GameCrashException("You can't override " + blockList[id].blockName + " with " + blockName + "!");
} else {
blockList[id] = this;
}
}
public static BufferedImage getTexture(String name) {
if (name == null) {
throw new GameCrashException("Texture can't be null!");
}
BufferedImage img = null;
try {
img = ImageIO.read((Block.class).getResourceAsStream(name));
} catch (IOException ex) {
Logger.getLogger(Block.class.getName()).log(Level.SEVERE, null, ex);
}
return img;
}
public static Texture getDefaultTexture() {
return new Texture(getTexture("/swinggame/terrain.png"), 16);
}
public void onBlockAdded(World world, int x, int y) {
}
public void onBlockRemoval(World world, int x, int y) {
}
public int getRenderType() {
return 0;
}
}