Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Null pointer help  (Read 1319 times)
0 Members and 1 Guest are viewing this topic.
Offline Robbo

Junior Newbie





« Posted 2005-11-16 00:15:34 »

hey guys, not sure if this is the correct section so i apologise in advance, but im having some problems getting my game started.

I am using a tile map and at the moment all i want to display on the screen is the contents of my map text file, the code compiles fine, i run it and it switches to full screen and exits almost straight away.  I then get a scary null pointer which looks a bit like this:

java.lang.NullPointerException
   at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
   at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
   at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
   at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2759)
   at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2749)
   at Game.draw(Game.java:28)
   at GameCore.gameLoop(GameCore.java:72)
   at GameCore.run(GameCore.java:29)
   at Game.main(Game.java:10)

i believe it doesnt like something i have in a class ive called game so i will include that to see if thats where the problem lies:

Quote
import java.awt.*;

public class Game extends GameCore {

   TileMap map;
   
   public static void main(String[] args) {
   
      Game a = new Game();
      a.run();
      
   }
   
   public void init() {
   
   super.init();
   map = new TileMap(2, 2);
   map.loadMap("map.txt");
   
   
   }
   
   
   public void draw (Graphics2D g) {
   
      for (int y = 0; y <= map.getHeight(); y++){
         for (int x = 0; x <= map.getWidth(); x++){
            g.drawImage(map.getTileImage(x,y), x*40, y*40, null);
            
            }
      }
      
      
}            

}

any help would be very appreciated  Wink
(if any other information is needed let me know )



Offline kevglass

« JGO Spiffy Duke »


Medals: 211
Projects: 24
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-11-16 00:31:51 »

I'd guess at "map.getTileImage(x,y)" returning null, might help to see the other class (TileMap)

Kev

Offline Robbo

Junior Newbie





« Reply #2 - Posted 2005-11-16 00:35:42 »

okay here is my tilemap class

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
import java.awt.*;
import java.util.*;
import javax.swing.ImageIcon;
import java.io.*;

public class TileMap
{
private Image [][] tiles;
private char [] [] chartiles;
private LinkedList sprites;

private Image crowdImg,boardsImg;

// create a new tile map with specified height and width
public TileMap (int width, int height)
 {
 tiles  = new Image [width] [height];
 sprites  = new LinkedList();
 
  crowdImg = new ImageIcon("images/crowd.png").getImage();
  boardsImg = new ImageIcon("images/board.png").getImage();
 
  }
 
  // gets the width of the tile map
  public int getWidth()
  {
  return tiles.length;
  }
 
  // get the height of the tilemap
 
  public int getHeight()
   {
   return tiles[0].length;
   }
   
   // get a tile at a specfied location.
   public Image getTile(int x, int y)
   {
   if (x<0|| x>= getWidth()|| y<0 || y>= getHeight())
   {
   return null;
   }
   else {
   return tiles [x] [y];
    }
  }
 
  // sets the tile at a specified location
  public void setTile(int x,int y, Image tile)
  {
  tiles[x][y] = tile;
 }
 
 

 
 // read in the slighty scary tilemap file
 public void loadMap(String filename) {
 
    ArrayList lines = new ArrayList();
    int width = 0;
    int height = 0;
   
    try
    {
    //read every line
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    while (true){
       String line = reader.readLine();
       //no more lines to read
       if (line==null){
          reader.close();
          break;
       }
       // add every line except for comments
       if(!line.startsWith("#")) {
          lines.add(line);
          width = Math.max(width,line.length());
       }
    }
   
    height = lines.size();
    tiles = new Image [width][height];
    chartiles = new char [width][height];
   
    for (int y=0; y<height; y++)
    {
       String line = (String)lines.get(y);
       for (int x=0; x<line.length(); x++) {
          char ch = line.charAt(x);
         
          chartiles[x][y] = ch;
          if (ch == 'c') tiles[x][y] = crowdImg;
          if (ch== 'b') tiles[x][y] = boardsImg;
        }
    }
    }
    catch (Exception e)
    {
       System.err.println("Failed to open tile map " + e);
    }
}          
         
         
public Image getTileImage(int x, int y) {
   return tiles[x][y];
}

public char getTileChar(int x, int y) {
   return chartiles[x][y];
}

 }// end of the TileMap class
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #3 - Posted 2005-11-16 01:09:59 »

Yes, KevGlass' suggestion seems likely.

AFAI can see, you only assign an image to a point in the 2D array when some character is 'b' or 'c'. Make sure that all the tiles are non-null, e.g. by filling out all the spaces of the array with a black image or something, so you can easily debug.

Also, your getTile method may return null if the requested indices are out of bounds. Maybe you should just make it throw ArrayIndexOutOfBoundsException, since that will cover the rest of the possible ways the null-pointer exception could occur.
Offline Robbo

Junior Newbie





« Reply #4 - Posted 2005-11-16 01:35:18 »

okay very silly error was causing the last problem, it was searching for 'b' and 'c' in the map but i had 'B' and 'C'  Grin

however it is now indeed throwing an array index out of bound error

java.lang.ArrayIndexOutOfBoundsException: 135
   at TileMap.getTileImage(TileMap.java:108)
   at Game.draw(Game.java:28)
   at GameCore.gameLoop(GameCore.java:72)
   at GameCore.run(GameCore.java:29)
   at Game.main(Game.java:10)
Exception in thread "main" Exit code: 1

what does this suggest ive done wrong? its trying to return something that is out of bounds?
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #5 - Posted 2005-11-16 04:42:04 »

1  
for (int x=0; x<line.length(); x++) {


Hmm.. what if line.length() != getWidth()



Offline Robbo

Junior Newbie





« Reply #6 - Posted 2005-11-16 10:57:13 »

hmm yeah it really didnt like that, failed to load map and threw in a null pointer for good measure  Sad

Failed to open tile map java.lang.StringIndexOutOfBoundsException: String index out of range: 0
java.lang.NullPointerException
   at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
   at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
   at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
   at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2759)
   at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2749)
   at Game.draw(Game.java:28)
   at GameCore.gameLoop(GameCore.java:72)
   at GameCore.run(GameCore.java:29)
   at Game.main(Game.java:10)
Exception in thread "main" Exit code: 1
There were errors
Offline Robbo

Junior Newbie





« Reply #7 - Posted 2005-11-16 13:18:43 »

okay sorted it now

was a silly mistake with my map itself

tried a different one and it worked fine

big thanks to all those who offered some input, nice to see a non elitist attitude when people ask for help..unlike some other places  Grin
Offline Jeff

JGO Coder




Got any cats?


« Reply #8 - Posted 2005-11-17 00:22:19 »

Thats what this topic is for, askign for help!

Actually thats about half of the purpose of EVERY topic here.

You may find people ocassionally give you short answers becuase their time is limited, or even ocassionally act annoyed if you havent checked yet what they consider basic reources, but in general the community here is pretty reliably helpful  Grin

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!