Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 1264 times)
0 Members and 1 Guest are viewing this topic.
Offline Robbo

Junior Newbie





« Posted 2005-11-16 01: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 Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2005-11-16 01: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 01: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 Member




Java games rock!


« Reply #3 - Posted 2005-11-16 02: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 02: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




Where's the Kaboom?


« Reply #5 - Posted 2005-11-16 05: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 11: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 14: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 01: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.

pw (11 views)
2014-07-24 01:59:36

Riven (10 views)
2014-07-23 21:16:32

Riven (11 views)
2014-07-23 21:07:15

Riven (12 views)
2014-07-23 20:56:16

ctomni231 (42 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!