Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-27-2008, 06:26 PM
Member
 
Join Date: Jan 2008
Posts: 8
riz618 is on a distinguished road
FireSimulation Program, got stuck with a bug...
Hi EveryOne,

I am writing a java application called FireSimulation and a I have a bug which needs to be resolved. I will appreciate your help. The code for the following is below.

GRID>JAVA

package Fire.Simulation.riz;

import java.util.Scanner;

public class Grid extends Cell {
public int probablityTree;
public int probablityFire;
boolean willGenerateTree;
boolean willCatchFire;
public double r;
public Cell[][] Box;

public Grid(int rows,int columns)
{
willGenerateTree = false;
willCatchFire = false;
Box= new Cell[rows][columns];

for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
System.out.println("first for");
Box=new Cell[i][j];
}
System.out.println("Second for");
}

}

public void GenerateGrid() {

}

public void ProbTree()
{
for(int i=0;i<(Box.length-1);i++)
{
for(int j=0;j<(Box[0].length-1);j++)
{
r=Math.random();
if(r>0 && r<=probablityTree)
willGenerateTree=true;
r=Math.random();
if(r>0 && r<=probablityFire)
willCatchFire=true;

if(willGenerateTree)
{
if(willCatchFire)
{
Cell[i][j].setState(2);
}
else
{
Cell[i][j].setState(1);
}
}
else
{
Cell[i][j].setState(0);
}
}


}

}

public static void main(String args[])
{
int row, column;
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the number of rows :");
row=scan.nextInt();
System.out.println("Please enter the number of columns :");
column=scan.nextInt();
Grid g = new Grid(row,column);
g.ProbTree();


}


}


Cell.java

package Fire.Simulation.riz;

public class Cell {

public int State;
public int s;

public Cell()
{

}

public int getState() {
return s;
}

public void setState(int s) {
this.s = s;
}

}


The bug is highlited here,

Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-27-2008, 06:31 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
You should be refering to the name of your variable - Box, i.e. channge
Code:
Cell[i][j].setState(2)
to read
Code:
Box[i][j].setState(2)
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-27-2008, 06:36 PM
Member
 
Join Date: Jan 2008
Posts: 8
riz618 is on a distinguished road
Even if i change to that, i get the following errors

Exception in thread "main" java.lang.NullPointerException
at Fire.Simulation.riz.Grid.ProbTree(Grid.java:62)
at Fire.Simulation.riz.Grid.main(Grid.java:80)

So there has to be something that needs a fix, if this is right.

Thanks for replying
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-27-2008, 06:47 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
your second problem is with the initialisation of your array, your constructor for grid has the variable and the class ( Box and Cell) slightly confused, i.e. you wrote

Code:
Box=new Cell[i][j];
should be
Code:
Box [i][j]=new Cell();
so that every entry in the two dimensional array gets allocated a new Cell object - actually I'm surprised the code as posted compiles
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-27-2008, 07:22 PM
Member
 
Join Date: Jan 2008
Posts: 8
riz618 is on a distinguished road
still did not solve my problem, if you could do me a fovour, compile the files i have provided and see if it works for you. That way you will have a better idea of what i am dealing with.

Thanks
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-28-2008, 04:48 PM
jelly's Avatar
Member
 
Join Date: Jan 2008
Location: Somerset, UK
Posts: 46
jelly is on a distinguished road
Having made the changes as said above, it runs fine, output:

Code:
Jackdaw:$ javac Grid.java Jackdaw:$ java Grid Please enter the number of rows : 4 Please enter the number of columns : 3 first for first for first for Second for first for first for first for Second for first for first for first for Second for first for first for first for Second for Jackdaw:$
code I ran
Code:
import java.util.Scanner; public class Grid extends Cell { public int probablityTree; public int probablityFire; boolean willGenerateTree; boolean willCatchFire; public double r; public Cell[][] Box; public Grid(int rows,int columns) { willGenerateTree = false; willCatchFire = false; Box= new Cell[rows][columns]; for(int i=0;i<rows;i++) { for(int j=0;j<columns;j++) { System.out.println("first for "+i+"**"+j); Box[i][j]=new Cell(); } System.out.println("Second for "+i+"**"); } } public void GenerateGrid() { } public void ProbTree() { for(int i=0;i<(Box.length-1);i++) { for(int j=0;j<(Box[0].length-1);j++) { r=Math.random(); if(r>0 && r<=probablityTree) willGenerateTree=true; r=Math.random(); if(r>0 && r<=probablityFire) willCatchFire=true; if(willGenerateTree) { if(willCatchFire) { Box[i][j].setState(2); } else { Box[i][j].setState(1); } } else { Box[i][j].setState(0); } } } } public static void main(String args[]) { int row, column; Scanner scan = new Scanner(System.in); System.out.println("Please enter the number of rows :"); row=scan.nextInt(); System.out.println("Please enter the number of columns :"); column=scan.nextInt(); Grid g = new Grid(row,column); g.ProbTree(); } public class Cell { public int State; public int s; public Cell() { } public int getState() { return s; } public void setState(int s) { this.s = s; } }
__________________
-- Hope that helps
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stuck on Two Questions, Please Help sylo18 New To Java 5 03-11-2008 02:03 AM
musically stuck cry for help 2 geork New To Java 0 02-07-2008 03:09 PM
musically stuck geork New To Java 1 02-06-2008 10:44 PM
Stuck and Frustrated. jazzinspace New To Java 7 01-12-2008 03:38 PM
I am completely stuck jpnym15 New To Java 2 11-14-2007 07:40 PM


All times are GMT +3. The time now is 12:22 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org