We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
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.
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.
https://forum.processing.org/two/discussion/13531/how-do-variable-declarations-work
There you go! Please help.
I was thinking more about a forum thread independent of your code here...
I've already explained why:
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?
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:
Fruit[] fruits;
fruits = new Fruit[2];
.Fruit[] fruits = new Fruit[2];
null
though.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: