This discussion is archived
1 Reply Latest reply: Feb 28, 2013 10:33 PM by r035198x RSS

converting int to string, adding to string then pointing to an url

993828 Newbie
Currently Being Moderated
So I'm getting a null pointer exception here's a snippet of my code (theres plus signs on both sides of the a in my code):

...
private Image[] tiles;

public Map() {
for(int a=0; a<9; a++){
ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares"+a+".png"));
tiles[a] = aa.getImage();
}
...

I've also tried:

...
private Image[] tiles;

public Map() {
for(int a=0; a<9; a++){
ImageIcon aa = new ImageIcon(this.getClass().getResource("mapsquares$s.png", String.valueOf(a)));
tiles[a] = aa.getImage();
}
...

whats wrong with either of these?

Edited by: Wub on Feb 28, 2013 3:17 PM
  • 1. Re: converting int to string, adding to string then pointing to an url
    r035198x Pro
    Currently Being Moderated
    1.) You should use code tags when posting code.
    2.) You should read the stacktrace closely to find out which exact line number in your code threw the exception.
    My guess is that it's the line
    tiles[a] = aa.getImage(); 
    because you didn't initialize the array tiles using the new operator. This is just a guess since you didn't post the full stacktrace. Also, NullpointerExceptions are one of the easiest to debug. Just adding print out lines at strategic values can show you which variables are initialized and which ones are not.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points