JakobekS
JakobekS

Reputation: 257

Object array - Null Pointer Exception

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

Upvotes: 1

Views: 565

Answers (4)

Harry Joy
Harry Joy

Reputation: 59660

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.

Upvotes: 0

Isaac
Isaac

Reputation: 2721

pixel is not null and so pixel == null in

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

never evaluates to true. just get rid of it.

Upvotes: 0

assylias
assylias

Reputation: 328618

Have you tried:

if(pixel[i][j] == null){
    pixel[i][j] = new Pixel();
}

And actually you seem to run this loop only once so this should be sufficient:

    for(int i=0; i<tHeigh; i++){
        for(int j=0; j<tWidth; j++){
            pixel[i][j] = new Pixel();
        }
    }

And finally I am not a Swing expert but I don't think it is a good idea to play with the GUI outside the EDT.

Upvotes: 2

Egor
Egor

Reputation: 40203

When you call

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

you just get an array of Pixel objects containing nulls. You need to fill it with valid Pixel objects to use it. Hope this helps.

Upvotes: 0

Related Questions