Missing a }?

edited November 2015 in Questions about Code
Fruit[] apple= new Fruit[10];
float lastReposTime=0;
Grass[][] grass= new Grass[3][120];
Tree tree= new Tree(0,50,100,535);
Branch branch= new Branch(50, 50, 50, 50, tree);

void setup(){
 size(600,600); 


  for(int i=0; i<2;i++){
       loadImage("apple" +i + ".jpg");

       float branchPosX= tree.posX+tree.treeWidth;
       float branchPosY= random(tree.treeHeight/2, tree.treeHeight);
       float fruitPosX= random(branchPosX, branchPosX+ 50);
       float fruitPosY= 20*i;

       apple[i]= new Fruit(fruitPosX, fruitPosY, 20, 20);
  }


 for(int i=0; i<grass.length; i++){
   for(int j=0; j<grass[i].length; j++){ 
     grass[i][j] = new Grass(0+5*j, 590-10*i, 5, 10);
   }
  }


}

void draw(){
  background(255);

  for(int i=0; i<grass.length; i++){
    for(int j=0; j<grass[i].length; j++){ 
       grass[i][j].update();
     }
  }   

   branch.update();

   tree.update();
}


//===========

class Branch{
  float posX;
  float posY;
  float branchWidth=50;
  float branchHeight=50;
  float brownColor= #713131;
 Fruit[] fruits;


 Tree tree;




  Branch(float x, float y, float w, float h, Tree t){
    posX= x;
    posY= y;
    branchWidth= w;
    branchHeight = h;
    tree=t;

    x= t.posX+t.treeWidth;
    y= random(t.treeHeight/2, t.treeHeight);

   Fruit[] fruits= new Fruit[2];
  for(int i=0; i<2; i++){

   float fruitPosX= random(posX, posX+50);
   float fruitPosY= 20*i;
   fruits[i]= new Fruit(fruitPosX, fruitPosY, 20, 20);
 }

  }

  void update(){

     for(int i=0; i<2;i++){ 
       fruits[i].update();
      } 

    fill(brownColor);
    rect(posX, posY, branchWidth, branchHeight);
  }

}

        //===============

    class Fruit{
  PImage img;
  PImage img2;
  float posX=100;
  float posY=100;
  float velX=0;
  float appleW= 50;
  float appleH= 50;

 Fruit(float x, float y, float w, float h){

   //img2= loadImage(other);
   posX= x;
   posY= y;
   appleW= w;
   appleH= h;

  }

 void update(){
    fill(255,0,0);

 ellipse(posX, posY, appleW, appleH);


    if(mousePressed==true ){
        posX += velX;
    }
 }

  void rot(){
    image(img2, posX, posY, appleW, appleH);
  }

}

//=============================

class Grass{
  float posX;
  float posY;
  float grassW=5;
  float grassH=10;

  Grass(float x, float y, float w, float h){
  posX= x;
  posY= y;
  grassW= w;
  grassH= h;
  }

  void update(){
    fill(0, 255, 0);
    rect(posX, posY, grassW, grassH);

  }

}

//===============================
class Tree
{
  float posX=0;
  float posY=560;
  float treeWidth=100;
  float treeHeight=500;
  float brownColor;
  Branch[] branch;
    Tree(float x, float y, float w, float h)
    {
      posX= x;
      posY= y;
      treeWidth= w;
      treeHeight = h;
      brownColor= #713131;
      Branch[] branch= new Branch[2];
         for(int i=0; i<branch.length;i++)
         {
             float branchPosX= x+w;
             float branchPosY= random(treeHeight/2, treeHeight);
             branch[i]= new Branch(branchPosX, branchPosY, 50, 50, this);
         }
     }


  void update()
  {
       //for(int i=0; i<2;i++)
       //{
       //  Branch[] branches =new Branch[2];
       //  branches[i].update();
       //}
    fill(#713131);
    rect(posX, posY, treeWidth, treeHeight);
  } 
}
//END CODE

Answers

  • edited November 2015

    In order to run statements within the declaration section of a class, we need to put them inside a curly brace block scope.
    But the most expected place is have them inside the class' constructor instead. :-@
    Therefore, move your for loop block to Branch's constructor. :-\"

  • See, but if I do that then that then there's a NullPointerException in Branch at fruit[i].update() or line 79 in OP.

  • edited November 2015

    Local variable fruits inside Branch's update() method is overshadowing Branch's own fruits field.
    And since you had re-instantiated another Fruit array there, all of its content is obviously still null.

  • Could you just back-up slightly and explain that a bit more in-depth?

  • Problem is you don't even understand what is a variable declaration. :-<
    I'm glad to explain basic concepts as long as you open separate forum threads for each 1.

  • edited November 2015

    I was thinking more about a forum thread independent of your code here...

    I get a NullPointerException on the line of code fruit[i].update. Could someone explain why?

    I've already explained why:

    Since you had re-instantiated another Fruit array there, all of its content is obviously still null.

  • Ok so then I guess I'm missing the logical link between your comment and my apparent non-understanding of variable declaration. Explain that then? Please?

  • edited November 2015 Answer ✓

    Tutorial below explains the very basics of Java language:
    http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/index.html

    But gonna try to quickly explain how to identify a variable declaration in Java below:

    • Every time we see a datatype preceding a variable name, that's called declaration.
    • That's when a variable is born and decided which datatype it can store.
    • This is a variable declaration: Fruit[] fruits;
    • Variable fruits can only store arrays of type Fruit now.
    • This is an initialization assignment, when the newly created variable receives its 1st value:
      fruits = new Fruit[2];.
    • Notice that the absence of a type preceding the variable name means we're using the variable rather than creating 1.
    • We can have both declaration & initialization at the same line statement too:
      Fruit[] fruits = new Fruit[2];
    • Also notice that arrays are created empty. Both fruits[0] & fruits[1] are still null though.
    • We still need to create Fruit objects and assign them to each element of Fruit[].
    • fruits[0] = new Fruit();, fruits[1] = new Fruit();.
  • Great, so I pretty much copied the for-loop from the constructor so that the Fruit objects could be created and now my code looks like so:

    Fruit[] apple= new Fruit[10];
    float lastReposTime=0;
    Grass[][] grass= new Grass[3][120];
    Tree tree= new Tree(0,50,100,535);
    Branch branch= new Branch(50, 50, 50, 50, tree);
    
    void setup(){
     size(600,600); 
    
    
      for(int i=0; i<2;i++){
           loadImage("apple" +i + ".jpg");
    
           float branchPosX= tree.posX+tree.treeWidth;
           float branchPosY= random(tree.treeHeight/2, tree.treeHeight);
           float fruitPosX= random(branchPosX, branchPosX+ 50);
           float fruitPosY= 20*i;
    
           apple[i]= new Fruit(fruitPosX, fruitPosY, 20, 20);
      }
    
    
     for(int i=0; i<grass.length; i++){
       for(int j=0; j<grass[i].length; j++){ 
         grass[i][j] = new Grass(0+5*j, 590-10*i, 5, 10);
       }
      }
    
    
    }
    
    void draw(){
      background(255);
    
      for(int i=0; i<grass.length; i++){
        for(int j=0; j<grass[i].length; j++){ 
           grass[i][j].update();
         }
      }   
    
       branch.update();
    
       tree.update();
    }
    
    
    //===========
    
    class Branch{
      float posX;
      float posY;
      float branchWidth=50;
      float branchHeight=50;
      float brownColor= #713131;
     Fruit[] fruits;
    
    
     Tree tree;
    
    
    
    
      Branch(float x, float y, float w, float h, Tree t){
        posX= x;
        posY= y;
        branchWidth= w;
        branchHeight = h;
        tree=t;
    
        x= t.posX+t.treeWidth;
        y= random(t.treeHeight/2, t.treeHeight);
    
       Fruit[] fruits= new Fruit[2];
      for(int i=0; i<2; i++){
    
       float fruitPosX= random(posX, posX+50);
       float fruitPosY= 20*i;
       fruits[i]= new Fruit(fruitPosX, fruitPosY, 20, 20);
     }
    
      }
    
      void update(){
         Fruit[] fruits= new Fruit[2];
          for(int i=0; i<2; i++){
    
           float fruitPosX= random(posX, posX+50);
           float fruitPosY= 20*i;
           fruits[i]= new Fruit(fruitPosX, fruitPosY, 20, 20);
         }
         for(int i=0; i<2;i++){ 
           fruits[i].update();
          } 
    
        fill(brownColor);
        rect(posX, posY, branchWidth, branchHeight);
      }
    
    }
    
            //===============
    
        class Fruit{
      PImage img;
      PImage img2;
      float posX=100;
      float posY=100;
      float velX=0;
      float appleW= 50;
      float appleH= 50;
    
     Fruit(float x, float y, float w, float h){
    
       //img2= loadImage(other);
       posX= x;
       posY= y;
       appleW= w;
       appleH= h;
    
      }
    
     void update(){
        fill(255,0,0);
    
     ellipse(posX, posY, appleW, appleH);
    
    
        if(mousePressed==true ){
            posX += velX;
        }
     }
    
      void rot(){
        image(img2, posX, posY, appleW, appleH);
      }
    
    }
    
    //=============================
    
    class Grass{
      float posX;
      float posY;
      float grassW=5;
      float grassH=10;
    
      Grass(float x, float y, float w, float h){
      posX= x;
      posY= y;
      grassW= w;
      grassH= h;
      }
    
      void update(){
        fill(0, 255, 0);
        rect(posX, posY, grassW, grassH);
    
      }
    
    }
    
    //===============================
    class Tree
    {
      float posX=0;
      float posY=560;
      float treeWidth=100;
      float treeHeight=500;
      float brownColor;
      Branch[] branch;
        Tree(float x, float y, float w, float h)
        {
          posX= x;
          posY= y;
          treeWidth= w;
          treeHeight = h;
          brownColor= #713131;
          Branch[] branch= new Branch[2];
             for(int i=0; i<branch.length;i++)
             {
                 float branchPosX= x+w;
                 float branchPosY= random(treeHeight/2, treeHeight);
                 branch[i]= new Branch(branchPosX, branchPosY, 50, 50, this);
             }
         }
    
    
      void update()
      {
           //for(int i=0; i<2;i++)
           //{
           //  Branch[] branches =new Branch[2];
           //  branches[i].update();
           //}
        fill(#713131);
        rect(posX, posY, treeWidth, treeHeight);
      } 
    }
    //END CODE
    
Sign In or Register to comment.