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 7595161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:36:16+00:00 2026-05-30T21:36:16+00:00

I want to create 2D array of objects, but i get null pointer exception

  • 0

I want to create 2D array of objects, but i get null pointer exception no matter how I create it. How should I make array of objects to get them work? Here is my code:

Game.java
package com.jakob.game1;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import java.awt.Canvas;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferStrategy;
import java.util.Random;

import javax.swing.JFrame;


public class Game extends Canvas implements Runnable, MouseMotionListener{
    private static final long serialVersionUID = 1L;

    public static final int gHeigh = 500;
    public static final int gWidth = 500;
    public static final int tHeigh = gHeigh/5;
    public static final int tWidth = gWidth/5;
    private boolean running;
    public Random random;

    public BufferStrategy b; 
    public Graphics g;
    public Image bi;

    public Pixel[][] pixel = new Pixel[tHeigh][tWidth]; 

    private void start(){
        running = true;
        new Thread(this).start();
    }

    public static void main(String args[]){
        Game game = new Game();

        JFrame frame = new JFrame("Game test");
        frame.setLayout(new BorderLayout());
        frame.setBounds(0, 0, gHeigh, gWidth);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(game, BorderLayout.CENTER);
        //frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true); 

        game.start();
    }

    public void run() {
        bi = createImage(gHeigh, gWidth);
        g = bi.getGraphics();
        g.setColor( Color.black );
        g.fillRect( 0, 0, gHeigh, gWidth);
        g.setColor( Color.white );
        addMouseMotionListener( this );


        for(int i=0; i<tHeigh; i++){
            for(int j=0; j<tWidth; j++){
                if(pixel == null){
                    pixel[i][j] = new Pixel; //No matter what I write here it can't create this
                }
            }
        }



        if(pixel != null){
            for(int i=0; i<tHeigh; i++){
                for(int j=0; j<tWidth; j++){
                    pixel[i][j].setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
                }
            }
        }

        while(running){
            for(int i=0; i<tHeigh ;i++){
                for(int j=0; j<tWidth; j++){
                    paint(g, i*5, j*5);
                }
            }
        }
    }

    public void paint(Graphics g, int x, int y){
        g.setColor(pixel[x][y].getColor());
        g.fillRect(x, y, 5, 5);
    }

    public void mouseDragged(MouseEvent e) {
        g.fillRect(e.getX()-2, e.getY()-2, 4, 4);
        repaint();
        e.consume();
    }

    public void mouseMoved(MouseEvent e) {

    }

    public void update( Graphics g ) {
       g.drawImage( bi, 0, 0, this );
    }

    public void paint( Graphics g ) {
       update( g );
    }
}

Pixel.java

package com.jakob.game1;

import java.awt.Color;

public class Pixel {
    private Color color;

    public Pixel(){
        color = new Color( 0,0,0);
    }

    public void setColor(Color color){
        this.color = color;
    }

    public Color getColor(){
        return this.color;
    }
}
  • 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-05-30T21:36:17+00:00Added an answer on May 30, 2026 at 9:36 pm

    Your condition is wrong

    if(pixel == null){
    

    This will check if pixel object is null or not which is not null because you have already initialized it [public Pixel[][] pixel = new Pixel[tHeigh][tWidth];]. So it is not going in if condition at all. You should have:

    if(pixel[i][j] == null){
    

    After analyzing your code in pastebin I found that now error is not for pixel but for random variable. You have not initialized random in your code and so it is null and thus the error.

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

Sidebar

Related Questions

I want to create an array of objects, so what I did was to
I want to create in C++ an array of Objects without using STL. How
I want to create an array with a message. $myArray = array('my message'); But
I want to create an array which has two columns. The output should contain
i want to create an array with 2 keys and i want them in
I want to create an array of numbers: 10, 9,8...to 1. But when I
I want to create an array/list of strings of the Comma seperated strings (file
I want to create an array holding a function f(x,y,z) . If it were
Imagine that I want to create an array from another array like this: $array
I want to create an associative array: var aa = {} // Equivalent to

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.