• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.







 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohammed Azeem
Ranch Hand
Posts: 56
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Tim Cooke
Marshal
Posts: 4037
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the low-down on Cows at our Wiki page: RanchCows
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!