Results 1 to 6 of 6
  1. #1
    riz618 is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default 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,


  2. #2
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    You should be refering to the name of your variable - Box, i.e. channge
    Java Code:
    Cell[i][j].setState(2)
    to read
    Java Code:
    Box[i][j].setState(2)
    -- Hope that helps

  3. #3
    riz618 is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    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

  4. #4
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    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

    Java Code:
    Box=new Cell[i][j];
    should be
    Java 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

  5. #5
    riz618 is offline Member
    Join Date
    Jan 2008
    Posts
    8
    Rep Power
    0

    Default

    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

  6. #6
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    Having made the changes as said above, it runs fine, output:

    Java 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
    Java 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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •