Multiplayer Shooter mit NullPointerException

antonbracke

Mitglied
Hallo,

ich brauche unbedingt HIlfe bei meinem Spiel.
Es wird mir ein Fehler angezeigt den ich absolut nicht verstehen kann (oder will).

Fehler:

[WR]Exception in thread "Thread-3" java.lang.NullPointerException
at Game.Game.draw(Game.java:111)
at Game.Game.setnewPosi(Game.java:80)
at Game.Game.movePlayer(Game.java:187)
at Core.Protokoll.decode(Protokoll.java:42)
at Core.Client.run(Client.java:97)
at java.lang.Thread.run(Unknown Source)[/WR]

Eine typische NullPointerException halt! ;(

Aber sowas ist ja im normal Fall schnell behoben, außer bei dem hier:

Die GAME Klasse:
Java:
public class Game extends Container {
	private static final long serialVersionUID = 1L;
	static GUI gui = null;
	static Client client = new Client(null);
	static Textures texture = new Textures();
	static Map map = new Map();

	static int[][] ls_game = new int[20][20];
	static int[][] ls_posi = new int[10][2];
	static Graphics gr;
	int size = 25;
	int godirection = 0;
	Thread tshoot;
	static Shoot[] shoot = new Shoot[5];
	static int cshoot = 0;
	static int shoots = 5;
	static int guimode = 1;
	//gamemode 0: rect
	//gamemode 1: img

	public Game(GUI _gui) {
		if (_gui != null) {
			gui = _gui;
			gr = this.getGraphics();
			setBounds(10, 10, 500, 500);
		}
	}

	public void setnewPosi(int in, boolean send, int x, int y) {
		int type = in / 10;
		if (in == 0 || in == 1) {
			type = in;
		}
		int _x = ls_posi[type][0];
		int _y = ls_posi[type][1];
		gr = this.getGraphics();
		int direction = in - (type * 10);
		
		if (canset(x, y, in)) {
			clear(in, _x, _y);
			draw(in, x, y);		
			if (send) {
				client.sendMSG("pos&&" + GUI.id + "&&" + x + "&&" + y + "&&"
						+ direction, "all");
			}		
			ls_posi[type][0] = x;
			ls_posi[type][1] = y;
			ls_game[x][y] = type;	
		} 
		
		else {
			if (in == getPlayerid(GUI.id) + direction) {
				draw(in, _x, _y);
				ls_posi[type][0] = _x;
				ls_posi[type][1] = _y;
				ls_game[_x][_y] = type;	
			}
		}
	}
	
	public void draw(int in, int x, int y){
		int type = in / 10;
		if (in == 0 || in == 1) {
			type = in;
		}
		if (guimode == 0) {
			gr.setColor(texture.getColor(type));
			gr.fillRect(x * size, y * size, size, size);
		}
		if (guimode == 1) {
			gr.drawImage(texture.getSymbol(in), x * size, y * size, this);
		}
	}

	public boolean clear(int type, int x, int y) {
		if (canset(x, y, 0)) {
			if (ls_game[x][y] == type / 10) {
				System.out.println(gr);
				if (guimode == 0) {
					gr.setColor(Color.GRAY);
					gr.fillRect(x * size, y * size, size, size);
				}
				if (guimode == 1) {
					gr.drawImage(texture.getSymbol(0), x * size, y * size, this);
				}
				return true;
			}
		}
		return false;
	}

	public boolean canset(int x, int y, int type) {
		if (x < 0) {
			return false;
		}
		if (y < 0) {
			return false;
		}
		if (x > 19) {
			return false;
		}
		if (y > 19) {
			return false;
		}
		if (ls_game[x][y] == 1) {
			if (type == getPlayerid(GUI.id)) {
				return false;
			}
			if (type == getPlayerid(GUI.id) + 1) {
				return false;
			}
			if (type == getPlayerid(GUI.id) + 2) {
				return false;
			}
			if (type == getPlayerid(GUI.id) + 3) {
				return false;
			}
		}

		return true;

	}

}


und hier dann das Protokoll bei dem die anderern Spieler verschoben werden sobald eine neue Position eingeht (posi):

Java:
public String decode(String msg) {
		String res = "";
		String[] arr = null;

		if (msg != null && !msg.isEmpty()) {
			arr = msg.split("&&");
			if (arr.length >= 1) {
				if (arr[0].equalsIgnoreCase("start")) {
					System.out.println("start");
					gui.start();
					res = "";
				}
				if (arr[0].equalsIgnoreCase("id")) {
					if (arr.length == 2) {
						int id = Integer.valueOf(arr[1]);
						GUI.id = id;
						res = "";
					}
				}
				if (arr[0].equalsIgnoreCase("pos")) {
				    if(arr.length == 5){
				    	int id = Integer.valueOf(arr[1]);
				    	if(id != GUI.id){
				    		int x = Integer.valueOf(arr[2]);
				    		int y = Integer.valueOf(arr[3]);
				    		System.out.println("Spieler[" + id + "]hat seine Postion auf " +  x + "/" + y +" gewechselt!");
				    		game.movePlayer(game.getPlayerid(id), x, y);
				    		res = "";
				    	}
				    }
				}
			}
		}
		return res;
	}

Das Problem muss eindeutig die Graphics sein, dass hab ich schon heraus gefunden aber was ist daran falsch??? ;( :rtfm:



Ich es kann mir schnell jemand helfen!


Gruß Anton🙂
 
Das Problem muss eindeutig die Graphics sein, dass hab ich schon heraus gefunden

Wenn du es schon herausgefunden hast, dann mach doch einfach eine Abfrage ala if(gr!=null) ... in deiner draw-Methode. Oder besser du machst es gleich richtig und liest mal einen der unzähligen "Wie zeichne ich richtig"-Threads hier im Forum. Das this.getGraphics() ist pfui wie du ja schon bemerkt hast 🙂
 
Hallo eRaaaa,

ich verstehe das mit diser graphics methode nicht ganz!
Man schreibt ja (wenn man es eben kann):
Java:
 public void paint(Graphics g) {
        g.fillOval(x, y, 10, 10);
    }

aber wie mache ich es wenn ich unterschiedliche Dinge zeichnen will?
Ich kann ja nichts in die Methode reingeben (z.B. einen int für die x und y Position) , dass geht ja nicht!!! 😉

Ansonsten glaube ich dass ich das verstehe!

Gruß Anton
 

Zurück
Oben