FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Strange OutOfMemory/NullPointer Error
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Strange OutOfMemory/NullPointer Error  (Read 484 times)
skloopy

WWW
Strange OutOfMemory/NullPointer Error
« on: Apr 4th, 2003, 9:44am »

Take a look at this. I'm not sure if it's an error or something i'm overlooking. I simplified this code down from a more complex program.
 
Code:
String startUrl = "http://www.yahoo.com/";
 
int maxNodes = 200;
PNode nodes[] = new PNode[maxNodes];
int nNodes;
 
void setup()
{
  size(200, 200);
  noStroke();
   
  nodes[0] = new PNode(startUrl, random(width), random(height), 0);  // NullPointer error here
  nNodes++;
}
 
void loop()
{
 
}
 
class PNode
{
  float x, y, z;
 
  String url;
  String baseUrl;
 
  int maxLinks = 99;
  String links[] = new String[maxLinks];
  int nLinks;
 
  PNode linkedNodes[] = new PNode[maxLinks];
  int nLinkedNodes;
 
  PNode(String url, float x, float y , float z) {
    this.x = x;
    this.y = y;
    this.z = z;
 
    this.url = url;
    this.baseUrl = parentUrl(url);
  }
}
 
String parentUrl(String url)
{
  return url.substring(0, url.lastIndexOf("/") + 1);
}

 
Every time I run this sketch it either gives a NullPointer error at the line above, or immediatley runs out of memory before the sketch window even opens.
 
I narrowed the problem down to the call to parentUrl() when making a new PNode. If you comment the line out the problem goes away. But even if I change the parentUrl method to
 
Code:
String parentUrl()
{
  return "lalalala";
}

 
it still does the same thing.
 
I can't find anything wrong with the code. Do any of you guys have any ideas?
 
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Strange OutOfMemory/NullPointer Error
« Reply #1 on: Apr 4th, 2003, 9:58am »

scoping problem. here's the modified code.
 
Code:
String startUrl = "http://www.yahoo.com/";  
 
int maxNodes = 200;  
PNode nodes[] = new PNode[maxNodes];  
int nNodes;  
 
void setup()  
{  
  size(200, 200);  
  noStroke();    
}  
 
void draw()  
{  
  nodes[0] = new PNode(startUrl, random(width), random(height), 0);  // NullPointer error here  
  nNodes++;  
  println(nodes[0].toString() + " / nodes[0] contains url " + nodes[0].url);
}  
 
class PNode  
{  
  float x, y, z;  
 
  String url;  
  String baseUrl;  
 
  int maxLinks = 99;  
  String links[] = new String[maxLinks];  
  int nLinks;  
 
  PNode linkedNodes[] = new PNode[maxLinks];  
  int nLinkedNodes;  
 
  PNode(String url, float x, float y , float z) {  
    this.x = x;  
    this.y = y;  
    this.z = z;  
 
    this.url = url;  
    this.baseUrl = parentUrl(url);  
  }  
  
  String parentUrl(String url)  
  {  
    return url.substring(0, url.lastIndexOf("/") + 1);  
  }  
 
}
« Last Edit: Apr 4th, 2003, 10:00am by Martin »  
skloopy

WWW
Re: Strange OutOfMemory/NullPointer Error
« Reply #2 on: Apr 4th, 2003, 12:25pm »

Thanks. I guess I should have known about that huh. It seems kind o strange though that i've called some other methods from inside the node object and it worked fine even though they were out of it's scope.
 
I think the problem only shows up in the constructor. I tried even using println() but it just runs out of memory.
 
Is this the compiler bug that people were talking about? But i thought it only applied to p5 specific methods..
 
Thanks!
« Last Edit: Apr 4th, 2003, 12:25pm by skloopy »  
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Strange OutOfMemory/NullPointer Error
« Reply #3 on: Apr 4th, 2003, 4:52pm »

i'm not too clear about what u mean. sample code pls?
 
skloopy

WWW
Re: Strange OutOfMemory/NullPointer Error
« Reply #4 on: Apr 4th, 2003, 7:40pm »

Sorry, all I mean is that code like this:
Code:
String hi = "hello";
sayHi theThingThatSaysHi;
 
void setup() {
  theThingThatSaysHi = new sayHi(hi);
}
 
void draw() {
  
}
 
class sayHi {
  sayHi(String whatChaGonnaSay) {
    println(whatChaGonnaSay);  // Crashes here
  }
}

where a class uses an outside method (like println()) when it's created always errors somehow. Earlier it was giving me outOfMemory errors and now it just crashes p5. It's just strange, that's all. I've just been working around it by just using a seperate init() method in each class.
 
So instead of that, you'd have:
Code:
class sayHi {
  sayHi() {}
  void init(String whatChaGonnaSay) {
    println(whatChaGonnaSay);
  }
}

and then just call init() after creating the object.
« Last Edit: Apr 4th, 2003, 7:41pm by skloopy »  
Martin

122417302122417302martingomez_listsmg1ph WWW Email
Re: Strange OutOfMemory/NullPointer Error
« Reply #5 on: Apr 5th, 2003, 7:20am »

this should work...
 
Code:
String hi = "hello";  
sayHi theThingThatSaysHi;  
 
void setup() {  
  theThingThatSaysHi = new sayHi("hi");  
}  
 
void draw() {  
   
}  
 
class sayHi extends BApplet {  
  sayHi(String whatChaGonnaSay) {  
    // println(whatChaGonnaSay);  // Crashes here  
    println(whatChaGonnaSay); // if extends BApplet is there
    // // System.out.println(whatChaGonnaSay); // if not    
  }  
}

 
benelek

35160983516098 WWW Email
Re: Strange OutOfMemory/NullPointer Error
« Reply #6 on: Apr 6th, 2003, 7:01am »

ah yes... it's not just p5-specific classes, but also p5-specific abreviations and functions. note that random(a) won't work inside the constructor either.
 
-jacob
 
Pages: 1 

« Previous topic | Next topic »