Forums Register Login

Why can't I populate an array of objects from inside the constructor?

+Pie Number of slices to send: Send
Good Morning everyone.

(Colleagues, I have tried hard to see the problem, but to no avail).

Experiment 14.

The experiments will slowly converge towards one big experiment: a simple game. I have just a little interest in games (perhaps I should have more), but making one - even a simple one - should be self rewarding.

However, now to the point.

* The experiment creates an array of rectangle objects.
* The rectangles are painted inside a Frame object at random x,y coords generated by a random number generator
* The rectangles are stationary.
* The rectangles are each assigned their own random colour.
* The array of rectangle objects is created inside the constructor of the class.

The actual code contains various other variables and methods which would distract from a quick analysis, so below is code
which has the same logical structure
which also fails (instead of array of rectangles, I have used arrays of integers).

As always, thank you. Maybe I can help someone else one day.







+Pie Number of slices to send: Send
This is not really a GUIs question, so I've added it to the Beginning Java section.

1. I don't see anywhere that you have initialized the array variable a, so it will be null.

2. Lok closely at your loop condition:How many times do you expect that loop to iterate? (Hint: it won't.)
+Pie Number of slices to send: Send
 

Mohammed Azeem wrote:The actual code contains various other variables and methods which would distract from a quick analysis


Here's a tip: code a little at a time and test frequently. When the code doesn't do what you expect, it's usually easy to write up a test class with that code in the main and maybe one or two other methods; it makes it easier and quicker to find what's wrong and correct it.
+Pie Number of slices to send: Send
Ok Darryl,

I forgot that creating an array is a two step process; first declare the array and then set the size of the array.

So line 13 has been amended to

However, I can't seem to understand my the iteration condition is wrong:



Read out aloud it says "for i=0 until i becomes greater than 10".

Time for my Saturday coffee outing - this will also stop my brain from exploding.
+Pie Number of slices to send: Send

Oh I see, (I think):

The loop will only iterate while some condition is stil true.

So the statement:

will never iterate because the loop's condition: i>N will always return false. (i is initially set to zero, which ofcourse is not greater than 10.

However changing to i<N throws a "nullPointerException" which I'll look into when I return from my coffee.

+Pie Number of slices to send: Send
That's right. The second part of a for loop construct is a "while" clause, not an "until" clause. The following two loops are equivalent:


The NullPointerException is unrelated to the for loop. In line 14 your define "Random randgen" but don't initialise it so it's null, then on line 22 you attempt to call .nextInt(100) on it.
+Pie Number of slices to send: Send

Very good, very good.

I see.

By the way Darryl you gave me a cow the other day.

Thank you very much for that. But what do I do with it?
+Pie Number of slices to send: Send
Get the low-down on Cows at our Wiki page: RanchCows
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 670 times.
Similar Threads
Newbie having trouble with the basics
loop over array to check for duplicates
Poker Game Evaluator
Traffic light simulation
Combining two classes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 03:38:51.