simple game

R

rob bell

Hi im trying to create a basic game in java, and for now im just trying to
get a map to print. Everything compiles fine but i get a
nullpointerexception at the line:

labelArray[x][y].setLocation((y*20),(x*20));

I cant understand how seen as the array is initialised earlier.
heres the rest of the code, as u can see its pretty basic.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class MyGame {

JFrame gameFrame;
JPanel gamePanel;
int[][] mapArray = { {1,1,1,1,3,1,2,2,2,2},
{1,1,1,1,1,1,2,2,2,2},
{2,2,2,2,2,1,1,1,1,1},
{2,2,2,2,2,1,1,1,1,1} };

JLabel labelArray[][] = new JLabel[10][4];

public MyGame() {

gameFrame = new JFrame("Quest");
gamePanel = new JPanel();

ImageIcon grassIcon = new ImageIcon("img/grass.gif");

ImageIcon playerIcon = new ImageIcon("img/playerDown.gif");

ImageIcon waterIcon = new ImageIcon("img/water.gif");

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

for(int y = 0; y < 10; y++){

if(mapArray[x][y] == '1'){

labelArray[x][y] = new JLabel(grassIcon);
gamePanel.add(labelArray[x][y], new Integer(1));

}

if(mapArray[x][y] == '2'){

labelArray[x][y] = new JLabel(waterIcon);
gamePanel.add(labelArray[x][y], new Integer(1));

}

if(mapArray[x][y] == '3'){

labelArray[x][y] = new JLabel(playerIcon);
gamePanel.add(labelArray[x][y], new Integer(1));

}

}

}


gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

gameFrame.setDefaultLookAndFeelDecorated(true);

gameFrame.getContentPane().add(gamePanel, BorderLayout.NORTH);

gameFrame.pack();
gameFrame.setSize(600,450);
gameFrame.setResizable(false);

gameFrame.setVisible(true);

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

for(int y = 0; y < 10; y++){

labelArray[x][y].setLocation((y*20),(x*20)); //
heres the problem here
}

}

}


public static void main (String[] args){


MyGame newGame = new MyGame();


}

}

any help will be appreciated, also any tips on how i could improve or any
major pitfalls ive made.

thankyou

Rob
 
T

Thomas G. Marshall

rob bell said:
Hi im trying to create a basic game in java, and for now im just
trying to get a map to print. Everything compiles fine but i get a
nullpointerexception at the line:

labelArray[x][y].setLocation((y*20),(x*20));


WHOA there. Did this really elsewhere in the code?

You are declaring things as [10][4] and then initializing them as [4][10] !!

In general, try to always regard the array coordinates as [y][x]. Makes
things easier.








I cant understand how seen as the array is initialised earlier.
heres the rest of the code, as u can see its pretty basic.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class MyGame {

JFrame gameFrame;
JPanel gamePanel;
int[][] mapArray = { {1,1,1,1,3,1,2,2,2,2},
{1,1,1,1,1,1,2,2,2,2},
{2,2,2,2,2,1,1,1,1,1},
{2,2,2,2,2,1,1,1,1,1} };

JLabel labelArray[][] = new JLabel[10][4];

public MyGame() {

gameFrame = new JFrame("Quest");
gamePanel = new JPanel();

ImageIcon grassIcon = new ImageIcon("img/grass.gif");

ImageIcon playerIcon = new
ImageIcon("img/playerDown.gif");

ImageIcon waterIcon = new ImageIcon("img/water.gif");

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

for(int y = 0; y < 10; y++){

if(mapArray[x][y] == '1'){

labelArray[x][y] = new JLabel(grassIcon);
gamePanel.add(labelArray[x][y], new
Integer(1));

}

if(mapArray[x][y] == '2'){

labelArray[x][y] = new JLabel(waterIcon);
gamePanel.add(labelArray[x][y], new
Integer(1));

}

if(mapArray[x][y] == '3'){

labelArray[x][y] = new JLabel(playerIcon);
gamePanel.add(labelArray[x][y], new
Integer(1));

}

}

}


gameFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

gameFrame.setDefaultLookAndFeelDecorated(true);

gameFrame.getContentPane().add(gamePanel,
BorderLayout.NORTH);

gameFrame.pack();
gameFrame.setSize(600,450);
gameFrame.setResizable(false);

gameFrame.setVisible(true);

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

for(int y = 0; y < 10; y++){


labelArray[x][y].setLocation((y*20),(x*20)); // heres the problem
here }

}

}


public static void main (String[] args){


MyGame newGame = new MyGame();


}

}

any help will be appreciated, also any tips on how i could improve or
any major pitfalls ive made.

thankyou

Rob
 
R

RJGraham

You're initiallizing the map array with ints but checking for chars.

e.g.

Shouldn't ,


if(mapArray[x][y] == '1'){

labelArray[x][y] = new JLabel(grassIcon);
gamePanel.add(labelArray[x][y], new
Integer(1));

}


really be

if(mapArray[x][y] == 1){

labelArray[x][y] = new JLabel(grassIcon);
gamePanel.add(labelArray[x][y], new
Integer(1));

}

?

-Randy
 
V

VisionSet

You're initiallizing the map array with ints but checking for chars.

Which is fine, the char is promoted to an int and then tested for equality,
though granted this is unlikely the intention here, since '1' != 1
Sorry, certification candidates like exsmokers eh?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top