• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing/modifying elements in an array  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, here's the deal. I'm still quite fresh to programming. My task is to create a program, that can draw any number of balls, and then make them bounce across the canvas. The part of the task I am stuck on is converting convert a method that draws two balls to a canvass and makes them bounce, into a method that draws an number of balls and makes them bounce.
My problem, is that I can get any specified number of balls drawn, but I cant get them to all move at once. This works fine (original code attached to message).
This code is the ammendments i have made:
----------------------------------------------------------------------
public void bounce(int numb)
{
int ball;
int ground = 400; // position of the ground line
myCanvas.setVisible(true);
// draw the ground
myCanvas.drawLine(50, ground, 550, ground);
// crate and show the balls
int xpos = 20;
allBalls = new BouncingBall[numb];
for (ball = 0; allBalls.length < numb; ball++)
{
allBalls[ball] = new BouncingBall(xpos, 20, 16, Color.blue, ground, myCanvas);
xpos = (xpos + 20);
allBalls[ball].draw();}
myCanvas.wait(50);
for (ball = 0; ball < numb; ball++)
{ allBalls[ball].move();

// make them bounce
while(allBalls[ball].getXPosition() >= 550)
{
allBalls[ball].erase();
}}
}
}
------------------------------------------------------------------------------------
and the all I can get is nullpointer exception... I know that a null pointer is normally thrown when objects haven't been initialized, but this is twisting my melon a bit. I did have it drawing the blls, without moving them, but i started getting outofbounds exceptions so I messed with it a bit more, and now this. can somebody please help me? Coming to forum is a last resort after having spent about a week on the projct as a whole, but then this one task has so far taken 2 days to try to figure out.
 
Bartender
Posts: 9444
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. it doesn't seem that you included the original code (the one that works)
2. use the UUB Code tag to preserve your code's indents
3. if you are getting an exception, include the exception message and stack trace. We can't reproduce the error from the code snip you gave us.
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandy:
Welcome to JavaRanch!

The problem is with your for loop. allBalls.length is not the number of current elements in the array, it is the declared length of (number of slots in) the array.
What this means is that this loop is never executed (allBalls.length is never less than numB, which is itself). Thus, when you hit the (correct) for loop later on that moves the balls, you have nothing in your array, thereby producing the NullPointerException
What you want to say is:

[ December 04, 2003: Message edited by: Joel McNary ]
 
sandy Bond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Joel, I have managed to get one ball drawn now, but it still wont seem to go thru, my own thoughts are that if the variable xpos isn't incremented properly...hang on, i've just this second managed to get all the specified balls showing up.....Thanks Joel...
btw i found getting them all showing it had nothing to do with xpos...i switched it so that the condition

woohoo... problem now is getting the move loop to work...
thanks for your help Joel
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!