Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting NullPointerException while creating a game
    primarykey
    data
    text
    <p>I'm creating a game. It doesn't throw an error in Eclipse, but when I run it in debug mode it throws a <code>NullPointerException</code> at line: </p> <pre><code>System.out.println("Stone x: " + blocks.get(BlockRectangle.getByID(rectID)).getX() + " y: " + blocks.get(BlockRectangle.getByID(rectID)).getY()); </code></pre> <p>Game.java:</p> <pre><code>package lt.projecturanium; import java.awt.BorderLayout; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferStrategy; import java.util.HashMap; import javax.swing.JFrame; import lt.projecturanium.blocks.Block; import lt.projecturanium.blocks.BlockRectangle; import lt.projecturanium.entity.Player; @SuppressWarnings("unused") public class Game extends Canvas implements Runnable{ private static final long serialVersionUID = 1L; private static JFrame _frame; public static Game _instance; private static final String TITLE = "Project Uranium"; private static final int WIDTH = 650; private static final int HEIGHT = WIDTH * 3 / 4; private static final int UPDATE_RATE = 50; private static final int RENDER_RATE = 100; public static HashMap&lt;Block, Coordinates&gt; blocks = new HashMap&lt;Block, Coordinates&gt;(); public int rectx = 0; public int recty = 0; public int rectID = 0; public boolean hitted = false; public float interpolation; public static final Dimension SIZE = new Dimension(WIDTH, HEIGHT); private Thread _thread; private boolean _running; private int _totalTicks = 0; private int _tps = 0; private int _fps = 0; public Game() { _instance = this; setPreferredSize(SIZE); setMinimumSize(SIZE); setMaximumSize(SIZE); _frame = new JFrame(TITLE); _frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); _frame.setLayout(new BorderLayout()); _frame.add(_instance, BorderLayout.CENTER); _frame.pack(); _frame.setResizable(false); _frame.setLocationRelativeTo(null); _frame.setVisible(true); createBufferStrategy(2); blocks.put(new Block(new BlockRectangle(200)), new Coordinates(30, 50)); } public synchronized void start() { _running = true; _thread = new Thread(this, TITLE+"_main"); _thread.start(); } public synchronized void stop() { _running = false; if (_thread != null) { try { _thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void paint(Graphics g) { super.paint(g); // fixes the immediate problem. Graphics2D g2 = (Graphics2D) g; g2.drawString("FPS: " + _fps + "\n TPS: " + _tps, 10, 10); if (hitted) { recty = 0; rectx += 21; rectID++; blocks.put(new Block(new BlockRectangle(rectID)), new Coordinates(rectx, recty)); hitted = false; } recty++; g2.drawImage(Player.getTexture(), 60, 60, null); g2.drawRect(rectx, recty, 20, 20); g2.setColor(new Color(101, 67, 33)); g2.fillRect(0, 430, getWidth(), getHeight()); g2.setColor(new Color(0, 100, 0)); g2.fillRect(0, 420, getWidth(), 10); g2.setColor(Color.BLACK); if (recty == (419 - 20)) { hitted = true; } } public void run() { double lastUpdateTime = System.nanoTime(); double lastRenderTime = lastUpdateTime; final int ns = 1000000000; final double nsPerUpdate = (double) ns / UPDATE_RATE; final double nsPerRender = (double) ns / RENDER_RATE; final int maxUpdatesBeforeRender = 5; int lastSecond = (int) (lastUpdateTime / ns); int tickCount = 0; int renderCount = 0; while (_running) { long currTime = System.nanoTime(); int tps = 0; while ((currTime - lastUpdateTime) &gt; nsPerUpdate &amp;&amp; tps &lt; maxUpdatesBeforeRender) { update(); tickCount++; _totalTicks++; tps++; lastUpdateTime += nsPerUpdate; interpolation = Math.min(1.0F, (float) ((currTime - lastUpdateTime) / nsPerUpdate)); render(interpolation, getGraphics()); } if (currTime - lastUpdateTime &gt; nsPerUpdate) { lastUpdateTime = currTime - nsPerUpdate; } if (currTime - lastRenderTime == maxUpdatesBeforeRender + 1) { render(interpolation, getGraphics()); } renderCount++; lastRenderTime = currTime; int currSecond = (int) (lastUpdateTime / ns); if (currSecond &gt; lastSecond) { _tps = tickCount; _fps = renderCount; tickCount = 0; renderCount = 0; lastSecond = currSecond; _frame.setTitle(TITLE + " | TPS: " + _tps + " | FPS: "+ _fps); } while (currTime - lastRenderTime &lt; nsPerRender &amp;&amp; currTime - lastUpdateTime &lt; nsPerUpdate) { Thread.yield(); try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } currTime = System.nanoTime(); } } } public void update() { _frame.pack(); } public void render(float interp, Graphics g) { BufferStrategy myStrategy = getBufferStrategy(); Graphics gra = myStrategy.getDrawGraphics(); paint(gra); g.dispose(); myStrategy.show(); //System.out.println("Grass x: " + blocks.get("grass").getX() + " y: " + blocks.get("grass").getY()); System.out.println("Stone x: " + blocks.get(BlockRectangle.getByID(rectID)).getX() + " y: " + blocks.get(BlockRectangle.getByID(rectID)).getY()); } } </code></pre> <p>BlockRectangle.java:</p> <pre><code>package lt.projecturanium.blocks; import java.awt.Image; import java.io.IOException; import java.util.HashMap; import javax.imageio.ImageIO; import lt.projecturanium.Game; public class BlockRectangle extends Block{ private int id; private static HashMap&lt;Integer, BlockRectangle&gt; rects = new HashMap&lt;Integer, BlockRectangle&gt;(); public BlockRectangle(int id) { super(); this.id = id; rects.put(id, this); } public int getID() { return this.id; } public static BlockRectangle getByID(int id) { return rects.get(id); } public static Image getTexture() { try{ return ImageIO.read(Game._instance.getClass().getClassLoader().getResource("../res/player.png")); } catch(IOException e) { e.printStackTrace(); } return null; } } </code></pre> <p>Block.java:</p> <p>package lt.projecturanium.blocks;</p> <pre><code>public class Block { private Block block; public Block (Block block){ this.block = block; } public Block getBlock() { return block; } public Block getBlockById(int id) { return block; } public Block() { } } </code></pre> <p>Error:</p> <pre><code>Thread [Project Uranium_main] (Suspended (exception NullPointerException)) Game.render(float, Graphics) line: 186 Game.run() line: 139 Thread.run() line: not available </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload