NickPick said:
I'd like to create an object array called test from the class
testclass. How exactly do I have to do this? The code below deosn't
work. What's wrong with it? Many thanks
Well, you haven't told us what is wrong, but I can guess.
public static void main(String[] args) {
// TODO code application logic here
testclass[] test = new testclass[10];
for (int x = 1; x <= 5; x++) {
test[x].check_status();
}
}
Problem 1: This does not compile. Specifically, the main needs to be in
a class of some sort, and we have no definition for `testclass' nor its
hypothetical check_status method.
Problem 2: Java coding standards highly recommend that class names begin
with a capital letter and that they should be in CamelCase.
Problem 3: The indexes of an array of length N go from 0 to N-1,
inclusive (or as Edgar Dijkstra would put it, 0 <= i < N).
Problem 4: Hardcoding the 5 is a good way to set yourself up for an
ArrayIndexOutOfBoundsException. Your for loop should therefore look more
like this:
for (int x = 0; x < test.length; x++)
Problem 5: You never initialized the array. All arrays are set to their
default values, which are 0 for numeric arrays, '0' for character
arrays, false for boolean arrays, and null for Object arrays. Therefore,
I am guessing that your problem is that you are getting a
NullPointerException. The way around this is to fill the array with
something first.