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

  • SEARCH
  • Home
  • 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 9075155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:48:21+00:00 2026-06-16T18:48:21+00:00

I am trying to take an image and turn it into tiles that are

  • 0

I am trying to take an image and turn it into tiles that are laid out in GridLayout, but receive a runtime error.

Here is the code:

public class TileList extends JPanel {

private static final int width = 16;            //width of a tile
private static final int height = width;
private int col = 1;
private int row = 1;

private BufferedImage image;
File tilesetImage = new File("image.png");
BufferedImage tileset[];

public void loadAndSplitImage (File loadImage) {
    try{
        image = ImageIO.read(loadImage);
    }catch(Exception error) {
        System.out.println("Error: cannot read tileset image.");
    }// end try/catch
    col = image.getWidth()/width;
    row = image.getHeight()/height;
    BufferedImage tileset[] = new BufferedImage[col*row];
}// end loadAndSplitImage

public TileList() {
    loadAndSplitImage(tilesetImage);
    setLayout(new GridLayout(row,col,1,1));
    setBackground(Color.black);

    int x=0;
    int y=0;
    for (int i = 0; i < (col*row); i++) {
        JPanel panel = new JPanel();
        tileset[i] = new BufferedImage(width, height, image.getType());  //first error
        tileset[i] = image.getSubimage(x,y,x+width,y+height);
        panel.add(new JLabel(new ImageIcon(tileset[i])));
        add(panel);
        x+=width;
        y+=height;
    }// end for loop
}// end constructor

public static void createAndShowGui() {
    JFrame frame = new JFrame("TileList");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new TileList());      //second error
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}//end createAndShowGui

public static void main (String [] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run () {
            createAndShowGui();           //third error
        }// end run
    });// end invokeLater
  }// end main
}// end class

This is the error I receive:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at TileList.<init>(TileList.java:55)
    at TileList.createAndShowGui(TileList.java:73)
    at TileList$1.run(TileList.java:82)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:721)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:682)
    at java.awt.EventQueue$3.run(EventQueue.java:680)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo
main.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:691)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre
ad.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.
java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre
ad.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

What does this error mean and how did I write the code wrong?

Edit: I changed my code as Hovercraft suggested, however now I have new errors:

tileset[] = new BufferedImage[col*row];
       ^                           error: not a statement

tileset[] = new BufferedImage[col*row];
         ^                         error: ';' expected

tileset[] = new BufferedImage[col*row];
                             ^     error: not a statement
  • 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-16T18:48:22+00:00Added an answer on June 16, 2026 at 6:48 pm

    Your error is that you are shadowing a variable — you’re re-declaring the variable tileSet inside constructor when it was already declared in the class. Thus the tileSet variable declared in the class is never initialized since only the local variable, the one declared in the constructor is initialized.

    The solution: Only declare tileSet once, in the class.

    Thus, don’t do this:

    public class TileList extends JPanel {
    
      //.... deleted for brevity
    
      BufferedImage tileset[];
    
      public void loadAndSplitImage (File loadImage) {
        try{
            image = ImageIO.read(loadImage);
        }catch(Exception error) {
            System.out.println("Error: cannot read tileset image.");
        }// end try/catch
        col = image.getWidth()/width;
        row = image.getHeight()/height;
        BufferedImage tileset[] = new BufferedImage[col*row]; // *** re-declaring variable!
      }
    

    but instead do this:

    public class TileList extends JPanel {
    
      //.... deleted for brevity
    
      BufferedImage tileset[];
    
      public void loadAndSplitImage (File loadImage) {
        try{
            image = ImageIO.read(loadImage);
        }catch(Exception error) {
            System.out.println("Error: cannot read tileset image.");
        }// end try/catch
        col = image.getWidth()/width;
        row = image.getHeight()/height;
    
        // ************
        // BufferedImage tileset[] = new BufferedImage[col*row]; // *****
        tileset[] = new BufferedImage[col*row]; // **** note the difference? ****
      }
    

    Note that even more important than the solution for this isolated problem is to gain an understanding on how to solve NPE (NullPointerExceptions). The key is to check all the variables on the line throwing the exception, check to see which one(s) is null, and then look back to see where one of them is not being initialized properly before you try using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to take an image that I have in a image object and
I am trying to figure out how to take an image and use Graphics2D
I am trying to take favicons and add them to a dynamic image that
Im trying to find a php/js script that will let me take an image,
I am trying to turn a mock-up into a page with css but I'm
I am trying to figure out how to take a image with the iPhone
I am trying to figure out how to use jquery to take an image
I'm trying to take an image file, do some stuff to it and save
I'm trying to take a full screen image of the camera picture without having
I am trying to take a picture from a camera and overlay the image

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.