Hi all,
I have to do a research project for my Research class, and am programming it in Greenfoot. However, I'm having some difficulties. I have a method that prints out the number of Organism.class in the world along with the act cycle. However, I only want it to print every 100th act cycle, but I keep getting a null pointer exception.
Here's the offending code:
public void printNumberOfOrganisms()
{
File f = new File("output.txt");
String curDir = f.getAbsolutePath();
String strFilePath = curDir;
try {
if (everyHundreth() % 500 == 0) {
FileOutputStream fos = new FileOutputStream(strFilePath, true);
String printCycle = Long.toString(((Desert) getWorld()).printCycle());
String strContent = Long.toString(numberOfOrganisms());
strContent = strContent + " " + printCycle;
String newLine = System.getProperty("line.separator");
fos.write(strContent.getBytes());
fos.write(newLine.getBytes());
fos.close();
}
}
catch(FileNotFoundException ex) {
System.out.println("FileNotFoundException: " + ex);
}
catch(IOException ioe) {
System.out.println("IOException: " + ioe);
}
}
public double everyHundreth()
{
double x = ((Desert) getWorld()).printCycle();
return x;
}
And the error:
java.lang.NullPointerException
at Organism.everyHundreth(Organism.java:384)
at Organism.printNumberOfOrganisms(Organism.java:358)
at Organism.act(Organism.java:109)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:346)
at greenfoot.core.Simulation.run(Simulation.java:178)
Any help would be appreciated!