Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8374273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:53:14+00:00 2026-06-09T14:53:14+00:00

I’m making an rpg with a custom pixel engine, and when I try to

  • 0

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

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

    }

}

and this is the class that has render():

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 < height; y++){

            if(ypos + y < 0 || ypos + y >= h) continue;

            for(int x = 0; x < width; x++){

                if(xpos + x < 0 || xpos + x >= w) continue;

                int col = loader.pixels[x + (y * height)];

                if(col != -65281) pixels[(x + xpos) + (y + ypos)] = col;

            }

        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:53:16+00:00Added an answer on June 9, 2026 at 2:53 pm

    Seems like you never initiate the loader in your first class.

    You define your variables like this:

    public SpriteSheetLoader loader;
    private Screen screen = new Screen(WIDTH, HEIGHT, loader);
    

    Hence, you pass a null-object to your Screen. And from there you try to call a method on that null-object:

    loader.grabTile(tile, width, height);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.