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 › Cannot load a real file
Page Index Toggle Pages: 1
Cannot load a real file (Read 726 times)
Cannot load a real file
Jan 2nd, 2009, 3:21am
 
I'm pretty new to Processing, but I think what I've been trying to do is within my range.  I have a class Organism defined in its own file (the project is named MARS), which is passed a string address to its constructor.  The constructor loads the file named <address>.txt with the loadStrings function, but...take a look.

[code follows]

class Organism{
String address; //the "physical" name of this organism
String[] rawProgram;

Organism (String address){//setup block
  String fulladdr = address + ".txt";
  String rawProgram[] = loadStrings(fulladdr);
  this.address = address;
}
}

[code ends]

There are some other methods and such in the class, but they are irrelevant.

When I call upon a new instance of Organism in the beginning of the MARS code (not within any function or object), like this:

[code follows]
String address1 = "imp";
String address2 = "dwarf";

Organism player1 = new Organism(address1);
Organism player2 = new Organism(address2);
[code ends]

I get the following output:

[code follows]
The file "imp.txt" is missing or inaccessible, make sure the URL is valid or that the file has been added to your sketch and is readable.
java.lang.NullPointerException

at mars$Organism.cleanProgram(mars.java:72)

at mars$Organism.<init>(mars.java:67)

at mars.<init>(mars.java:23)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at java.lang.Class.newInstance0(Unknown Source)

at java.lang.Class.newInstance(Unknown Source)

at processing.core.PApplet.main(PApplet.java:6486)
[code ends]

Both the imp.txt and dwarf.txt files are in the sketches/mars/data directory.  Any ideas?
Re: Cannot load a real file
Reply #1 - Jan 2nd, 2009, 10:19am
 
I am not sure about the "inaccessible" message, but I see at least one logic error (or ambiguity, at least) in the Organism class: String rawProgram[] hides the String[] rawProgram; field!
Re: Cannot load a real file
Reply #2 - Jan 3rd, 2009, 5:12am
 
Amended String rawProgram[] to this.rawprogram, but it doesn't change anything.  I've tried those two lines a number of different ways, but no dice.
Re: Cannot load a real file
Reply #3 - Jan 3rd, 2009, 4:48pm
 
I see no other logic error. But from the stack trace, I guess (oops, actually, you mention it) that you call (indirectly) the loadStrings out of setup().

The problem is that "global" code, out of functions, is called first. setup() is called after that, and that's the routine that initialize the special folders, like data. See where I am going?

Common scheme is to declare the objects at global level, and initialize them in setup():
Code:
String address1 = "imp";
String address2 = "dwarf";

Organism player1;
Organism player2;

void setup()
{
player1 = new Organism(address1);
player2 = new Organism(address2);
}
Page Index Toggle Pages: 1