Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException in my game, can't be fixed
    primarykey
    data
    text
    <p>I'm making an rpg with a custom pixel engine, and when I try to run it I just get a NullPointerException and I know why, but when I try to initialize that variable I get another one in a different location and I fix that and now it won't run at all and I still get a NullPointerException. This is the class that gets an error, and the console tells me that the error is on the line that says screen.render();</p> <pre><code>import java.awt.Canvas; import java.awt.Dimension; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import java.io.IOException; import java.util.Random; import javax.imageio.ImageIO; import javax.swing.JFrame; import sprites.SpriteSheetLoader; public class Game extends Canvas implements Runnable{ private static final long serialVersionUID = 1L; public static final int HEIGHT = 120; public static final int WIDTH = 120; public static final int SCALE = 3; public static final String NAME = "TypicalRPG"; public SpriteSheetLoader loader; private BufferedImage img = new BufferedImage(WIDTH * SCALE, HEIGHT * SCALE, BufferedImage.TYPE_INT_RGB); private int[] pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); private boolean running = false; private Screen screen = new Screen(WIDTH, HEIGHT, loader); Random random = new Random(); public void start(){ running = true; new Thread(this).start(); } public static void main(String args[]){ Game game = new Game(); game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); JFrame jf = new JFrame(NAME); jf.add(game); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.pack(); jf.setVisible(true); jf.setResizable(true); game.start(); } public void run(){ while(running){ tick(); render(); try{ Thread.sleep(5); } catch(InterruptedException e){ e.printStackTrace(); } } } public void stop(){ running = false; } public void tick(){ screen.render(0, 0, 0, 16, 16); } public void render(){ BufferStrategy bs = getBufferStrategy(); if(bs == null){ createBufferStrategy(3); requestFocus(); return; } for(int i = 0; i &lt; screen.pixels.length; i++){ pixels[i] = screen.pixels[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(img, 0, 0, getWidth(), getHeight(), null); g.dispose(); bs.show(); } public void init(){ BufferedImage sheet = null; try { sheet = ImageIO.read(Game.class.getResourceAsStream("res/tiles.png")); } catch (IOException e) { e.printStackTrace(); } loader = new SpriteSheetLoader(sheet); screen = new Screen(WIDTH, HEIGHT, loader); } } </code></pre> <p>and this is the class that has render():</p> <pre><code>import sprites.SpriteSheetLoader; public class Screen{ public int[] pixels; private SpriteSheetLoader loader; private int w, h; int xoff = 0, yoff = 0; public Screen(int w, int h, SpriteSheetLoader loader){ this.loader = loader; this.w = w; this.h = h; pixels = new int[w * h]; } public void render(int xpos, int ypos, int tile, int width, int height){ loader.grabTile(tile, width, height); xpos -= xoff; ypos -= yoff; for(int y = 0; y &lt; height; y++){ if(ypos + y &lt; 0 || ypos + y &gt;= h) continue; for(int x = 0; x &lt; width; x++){ if(xpos + x &lt; 0 || xpos + x &gt;= w) continue; int col = loader.pixels[x + (y * height)]; if(col != -65281) pixels[(x + xpos) + (y + ypos)] = col; } } } } </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.
    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