user1098185
user1098185

Reputation: 109

ArrayList from object and specific name for the objects

Im learning to make a game, and im nearly done with the main part, but cant get one thing working. I need to generate some fields from Field.java The Code from Field.java is the following

package matedor;

class Field{
    private String name;
    private int number;

    public Field (String fieldname, int fieldnumber){
        this.name = fieldname;
        this.number= fieldnumber;
    }

    public String getFieldname(){
        return name;
    }
    public int getFieldnumber(){
        return number;
    }

    public String toString(){
        return number+name;
    }

    public boolean equals(Object obj){
       Field field = (Field) obj;
        return (number == field.number && name.equals(field.name));
    }
}

In my main i need to generate the following which is not editable:

fieldname1, fieldname2.....to fieldname40

I know i have to do it with an ArrayList, and i want to create 40 instances of the field, so i have from fieldname1 to fieldname40. How do i do that?

In my main i have tried to do the following, but i get the null statement

 Field[] felterISpil=new Field[40];
 System.out.println(Arrays.toString(felterISpil));

Output:

[null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]

Upvotes: 1

Views: 64

Answers (3)

Rodrigo GdeA
Rodrigo GdeA

Reputation: 11

You need to create an instance of each Field Try the following:

List fields = new ArrayList();
for(int i = 1; i <= 40; i++){
    fields.add(new Field("Field"+i, i));
}

Upvotes: 1

Madhawa Priyashantha
Madhawa Priyashantha

Reputation: 9872

you need to initialize .you are getting null because default value of objects are null.you have declare but haven't initialize yet.

you need to pass String fieldname, int fieldnumber to the constructor when you initialize .your constructor have 2 parameters String fieldname, int fieldnumber

public Field (String fieldname, int fieldnumber)

that's why you need to pass a String and int

felterISpil[0]=new Field("fieldname",1);
felterISpil[1]=new Field("fieldname2",2);

you should pass values you want to the constructor .how ever if you want to test it without initializing one by one following code will initialize all element of your field array

for(int i=0;i<felterISpil.length;i++){
   felterISpil[i]=new Field("fieldname",1);
}
System.out.println(Arrays.toString(felterISpil));

output is not null anymore

Upvotes: 1

Curious Mind
Curious Mind

Reputation: 54

First of all keep in mind that you are creating constructor of class Field. So in main() after you have created a Array of field class by doing this Field felterISpil[] = new Field[40]; then before call any method of Field class you need to create object of it by doing this felterSpil[index] = new Field(FieldName, FieldNumber); As u specify in ur constructor this way syntax actual it will. felterSpil[0] = new Field(Field1, 1); And then you can call any method u will get proper value

Upvotes: 1

Related Questions