usedelimiter scanner nextstring nextline nextdouble hasnext declarar java image file jframe java.util.scanner

nextstring - Problemas con Java Scanner(JFrame)



scanner on java (1)

Estoy tratando de usar un escáner para editar el nivel de mi juego de defensa de la torre. Sin embargo, no actualizará el nivel (las imágenes del mosaico) al del archivo personalizado (0 es el pasto 1 es piedra -1 no es nada, etc.). He encontrado el error, pero ¿cómo lo arreglo? ¿Qué debo agregar / cambiar para deshacerme de esto?

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

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

Este es el archivo del escáner:

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) { } } }

este es el archivo de la ventana:

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) { } } } }

Este es el archivo de la sala:

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); } } } }

este es el archivo de bloque:

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); } } }

Y, por último, este es el archivo personalizado que debe leer el escáner:

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

perdón por la estúpida pregunta, soy un principiante.


Una solución rápida y sucia es, para probar que Window.room no sea nulo, así como .block:

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

Una simple Testapp que he escrito funciona hasta ahora, si es así.

Pero necesita comprender qué es "estático", y por qué y cómo usarlo. Un error común en los principiantes es escribir palabras clave "estáticas" solo para silenciar al compilador.

Investigue, en qué orden para inicializar sus clases y sus atributos.

En Block, para acceder a la ventana, debe tener una referencia. La referencia se puede pasar al ctor de Bloque:

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); } } }

¿Quién crea Blocks? Es Habitación, por lo que la Habitación misma necesita saber acerca de la Ventana (siempre y cuando no cambie su diseño fundamentalmente).

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); } } }

Se crea una matriz de bloques, se inicializa y los bloques pasan el parámetro Ventana.

En draw, no recreas la matriz una y otra vez, ni recreas los bloques, sino que simplemente los vuelves a dibujar:

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); } } }

En Windows, usted crea la Habitación y la pasa por la ventana-referencia:

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