You are trying to assign data to an object that hasn't been created yet. Also, you shouldn't start at index 1. Arrays begin at index 0, so essentially, you are robbing yourself of extra space and making things harder on yourself by doing that. This also means that you are creating 9 objects, not 10.
Use the implementation below.
Variable [] variableTab = new Variable [10];
for(n=0;n<10;n++){
variableTab[n]= "variable" +""+n;
//System.out.println(variableTab[n]);
}
Update per comments:
If you are trying to store the name of a object, you need to create a member variable to store that name in.
public class Variable {
private String name; //This will be used to store the unique name of the object
//Default constructor for our class
Variable () {
name = "";
}
//Constructor to initialize object with specific name
Variable (String name) {
this.name = name;
}
//We need a way to control when and how the name of an object is changed
public setName (String name) {
this.name = name;
}
//Since our "name" is only modifiable from inside the class,
//we need a way to access it from the program
public String getName () {
return name;
}
}
This would be the proper way to setup a class. You can then control the data for each class in a more predictable way, since it is only able to be changed by calling the setName method, and we control how the data is retrieved from other sections of the program by creating the getName method.