We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Null Pointer Exception Map Project(Please Help!)
Page Index Toggle Pages: 1
Null Pointer Exception Map Project(Please Help!) (Read 435 times)
Null Pointer Exception Map Project(Please Help!)
Feb 21st, 2008, 2:52am
 
I am trying to create a program that will load text files as String arrays using loadStrings() and assign those Strings as attributes of an Array of custom Tile Objects. I eventually want to be able to display these tiles to creap a custom map according to the saved text file.

Here's the code:

int tileSize = 20;
Tile grass = new Tile(color(0, 200, 0), tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 10, 10, "map1");

void setup(){
size(800, 500);

}

void draw(){
 background(0);
 grass.create(0, 0);
 map1.update();
 map1.create();
 PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 36);
 text(map1.returnMapTilesLength(), width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
color base;
 Tile(color dbase, float dh, float dw){
   base = dbase;
   h = dh;
   w = dw;
 }
void create(float dx, float dy){

 fill(base);
 rect(dx, dy, w, h);
 }
void identify(String dname){

 name = dname;
 }
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void create(){
      for(int i = 0; i < MapLoad.length; i++){
       
        MapTiles[i].identify(MapLoad[i]);
        }
      for(int i = 0; i < MapTiles.length; i++){
       MapTiles[i].create(i*tileSize, i*tileSize);
        }
   }
   void update(){
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
     MapSave = (String[])expand(MapSave, mapW * mapH);
     for(int i = 0; i < MapTiles.length; i++){
             MapSave[i] = "grass";
        }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapTiles.length; i++){
            if(i <= MapLoad.length-1){
            if(MapLoad[i] == "grass"){
              MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
             }
        }
      }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}



Thank You for any help!!!
Re: Null Pointer Exception Map Project(Please Help
Reply #1 - Feb 21st, 2008, 9:57am
 
since i/we dont know what the nullpointer exception is (is displayed in the console in red), or what is inside of the map1.txt file (cannot run the program), i am guessing what might not work in your code.

Code:

MapTiles = (Tile[])expand(MapTiles, mapW * mapH);

this expands your MapTiles array and reserves space in memory, but you dont have any tiles in there yet which causes each item in the array to be null, hence nullpointer

initialize your array items with something like
Code:

for(int i = 0; i < MapTiles.length; i++){
MapTiles[i] = new Tile(color(255), 0,0);
}


Code:

if(MapLoad[i] == "grass"){

this will never be true. compare strings with

Code:

MapLoad[i].equals("grass")


hope this helps.
andi
Re: Null Pointer Exception Map Project(Please Help
Reply #2 - Feb 21st, 2008, 5:52pm
 
ok, i revised my code as you suggested, and I'm still getting a nullpointer exception.


java.lang.NullPointerException
at Temporary_5190_502$Map.create(Temporary_5190_502.java:86)
at Temporary_5190_502.draw(Temporary_5190_502.java:14)
at processing.core.PApplet.handleDisplay(PApplet.java:1465)
at processing.core.PGraphics.requestDisplay(PGraphics.java:690)
at processing.core.PApplet.run(PApplet.java:1562)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException

at Temporary_5190_502$Map.create(Temporary_5190_502.java:86)

at Temporary_5190_502.draw(Temporary_5190_502.java:14)

at processing.core.PApplet.handleDisplay(PApplet.java:1465)

at processing.core.PGraphics.requestDisplay(PGraphics.java:690)

at processing.core.PApplet.run(PApplet.java:1562)

at java.lang.Thread.run(Unknown Source)

There's the exception, and map1.txt is as follows:

grass
grass

Here's my revised code:int tileSize = 20;
Tile grass = new Tile(color(0, 200, 0), tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 10, 10, "map1");

void setup(){
size(800, 500);

}

void draw(){
 background(0);
 grass.create(0, 0);
 map1.update();
 map1.create();
 PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 36);
 text(map1.returnMapTilesLength(), width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
color base;
 Tile(color dbase, float dh, float dw){
   base = dbase;
   h = dh;
   w = dw;
 }
void create(float dx, float dy){

 fill(base);
 rect(dx, dy, w, h);
 }
void identify(String dname){

 name = dname;
 }
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void update(){
      for(int i = 0; i < MapTiles.length; i++){
         MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
      }
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
     MapSave = (String[])expand(MapSave, mapW * mapH);
     for(int i = 0; i < MapTiles.length; i++){
             MapSave[i] = "grass";
        }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapTiles.length; i++){
            if(i <= MapLoad.length-1){
            if(MapLoad[i].equals("grass")){
              MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
             }
        }
      }
   }
       void create(){
      for(int i = 0; i < MapLoad.length; i++){
       
        MapTiles[i].identify(MapLoad[i]);
        }
      for(int i = 0; i < MapTiles.length; i++){
       MapTiles[i].create(i*tileSize, i*tileSize);
        }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}


Thank you for the help!
Re: Null Pointer Exception Map Project(Please Help
Reply #3 - Feb 21st, 2008, 6:24pm
 
Further revisions...I get the same NullPointer Exception, but it does display my tiles(although, not in the way i want.)

int tileSize = 20;
Tile grass = new Tile(color(0, 200, 0), tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 10, 10, "map1");

void setup(){
size(800, 500);

}

void draw(){
 background(0);
 grass.create(0, 0);
 map1.update();
 map1.create();
 map1.instantiate();
 PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 36);
 text(map1.returnMapTilesLength(), width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
color base;
 Tile(color dbase, float dh, float dw){
   base = dbase;
   h = dh;
   w = dw;
 }
void create(float dx, float dy){

 fill(base);
 rect(dx, dy, w, h);
 }
void identify(String dname){

 name = dname;
 }
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void update(){
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
     MapSave = (String[])expand(MapSave, mapW * mapH);
     for(int i = 0; i < MapTiles.length; i++){
             MapSave[i] = "grass";
        }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapTiles.length; i++){
            if(i <= MapLoad.length-1){
            if(MapLoad[i].equals("grass")){
              MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
             }
        }
      }
   }
       void create(){
      for(int i = 0; i < MapLoad.length; i++){
        MapTiles[i].identify(MapLoad[i]);
        }
      for(int i = 0; i < MapTiles.length; i++){
        int row = 1;
        MapTiles[i].create(i*tileSize, tileSize*row);
        }
        }
       void instantiate(){
         for(int i = 0; i < MapTiles.length; i++){
         MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
         }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}
Re: Null Pointer Exception Map Project(Please Help
Reply #4 - Feb 22nd, 2008, 1:44am
 
hi,
you did put
Code:

for(int i = 0; i < MapTiles.length; i++){
MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);
}


into the wrong place. put it after you expand your mapTiles

Code:

MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
for(int i = 0; i < MapTiles.length; i++){
MapTiles[i] = new Tile(color(0, 200, 0), tileSize, tileSize);


Re: Null Pointer Exception Map Project(Please Help
Reply #5 - Feb 22nd, 2008, 2:14pm
 
Thank you so much! It works like a well-oiled clock!
Re: Null Pointer Exception Map Project(Please Help
Reply #6 - Feb 22nd, 2008, 5:49pm
 
Ok, I added a new argument to the identify method, and I'm getting null pointer exception. My array of tiles is instantiated! I'm really not sure....

int tileSize = 20;
Tile grass = new Tile(color(0, 200, 0), tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 20, 5, "map1");

void setup(){
size(800, 500);

}

void draw(){
 background(0);
 grass.create(0, 0);
 map1.instantiate();
 map1.update();
 map1.create();
 //PFont fontA = loadFont("CourierNew36.vlw");
 //textFont(fontA, 36);
 //text(map1.returnMapTilesLength(), width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
color base;
 Tile(color dbase, float dh, float dw){
   base = dbase;
   h = dh;
   w = dw;
 }
void create(float dx, float dy){
 fill(base);
 rect(dx, dy, w, h);
 }
void identify(String dname, color dbase){
 base = dbase;
 name = dname;
 }
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void update(){
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
 //    MapSave = (String[])expand(MapSave, mapW * mapH);
   //  for(int i = 0; i < MapTiles.length; i++){
     //        MapSave[i] = "grass";
       // }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapTiles.length; i++){
            if(i <= MapLoad.length-1){
            if(MapLoad[i].equals("grass")){
              MapTiles[i].identify("grass", color(0, 200, 0));
             }
        }
      }
   }
       void create(){
      for(int i = 0; i < MapTiles.length; i++){
      for(int j = 0; j < mapW; j++){
      for(int k = 0; k < mapH; k++){
     
        MapTiles[i].create(j*tileSize, tileSize*k);
     }
   }  
 }
}
       void instantiate(){
         for(int i = 0; i < MapTiles.length; i++){
         MapTiles[i] = new Tile(color(255), tileSize, tileSize);
         }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}
Re: Null Pointer Exception Map Project(Please Help
Reply #7 - Feb 22nd, 2008, 7:57pm
 
mapTiles[0], [1] etc doesn't exist when you're trying to call the identify method.

add

println("MapTiles[0]: " + MapTiles[0]);

just before

MapTiles[i].identify("grass", color(0, 200, 0));

and you'll see it's null - can't call a method of a null class. you need a "MapTile[i] = new Tile(...)" in there somewhere. (just after the "for" seems to work)

(btw MapTiles is probably better being called mapTiles - convention is that initial caps is for class names)
Re: Null Pointer Exception Map Project(Please Help
Reply #8 - Feb 25th, 2008, 4:39pm
 
Thank you for all your help so far people, but i have another question.
I'm having trouble setting the base color value for each tile. Any help appreciated.
int tileSize = 20;
Tile grass = new Tile(tileSize, tileSize);
Map map1 = new Map(tileSize, tileSize, 20, 5, "map1");


void setup(){
size(800, 500);

}

void draw(){
 background(0);
 grass.create(0, 0);
 map1.update();
 map1.create();
 map1.MapTiles[0].base = color(0, 200, 0);
 //PFont fontA = loadFont("CourierNew36.vlw");
 //textFont(fontA, 36);
 //text(map1.returnMapTilesLength(), width/2, height/2);
 
}

class Tile{
String name;
float h;
float w;
color base;
 Tile( float dh, float dw){
   h = dh;
   w = dw;
 }
void create(float dx, float dy){
 fill(base);
 rect(dx, dy, w, h);
 }
void identify(String dname, color dbase){
 base = dbase;
 name = dname;
 }
void displayType(float dx, float dy){
 PFont fontA = loadFont("CourierNew36.vlw");
 textFont(fontA, 36);
 text(name, dx, dy);
}
}

class Map{
String name;
float tileW;
float tileH;
int mapW;
int mapH;
float w = tileW * mapW;
float h = tileH * mapH;
Tile[] MapTiles = new Tile[0];
String[] MapLoad;
String[] MapSave = new String[MapTiles.length];
   Map(float dtileW, float dtileH, int dmapW, int dmapH, String dname){
      tileW = dtileW;
      tileH = dtileH;
      mapH = dmapH;
      mapW = dmapW;
      name = dname;
   }
   
   void update(){
     MapTiles = (Tile[])expand(MapTiles, mapW * mapH);
     instantiate();
 //    MapSave = (String[])expand(MapSave, mapW * mapH);
   //  for(int i = 0; i < MapTiles.length; i++){
     //        MapSave[i] = "grass";
       // }
        //saveStrings( name + ".txt", MapSave);
        MapLoad = loadStrings(name + ".txt");
      for(int i = 0; i < MapLoad.length; i++){
            if(MapLoad[i].equals("grass")){
              MapTiles[i].identify("grass", color(0, 200, 0));
             }
       
      }
   }
       void create(){
      for(int i = 0; i < MapTiles.length; i++){
      for(int j = 0; j < mapW; j++){
      for(int k = 0; k < mapH; k++){
     
        MapTiles[i].create(j*tileSize, tileSize*k);
     }
   }  
 }
}
       void instantiate(){
         for(int i = 0; i < MapTiles.length; i++){
         MapTiles[i] = new Tile(tileSize, tileSize);
         }
   }
   int returnMapTilesLength(){
     return MapTiles.length;
   }
   int returnMapLoadLength(){
     return MapLoad.length;
   }
   int returnmapH(){
     return mapH;
   }    
   int returnmapW(){
     return mapW;
   }
}


Page Index Toggle Pages: 1