1

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]
1
  • please keep your bracket style consistent and format your code properly. Commented Oct 26, 2014 at 12:03

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

10 Comments

i get an error when i do that. Create constructor field() in Matedor.Field
@user1098185 you should pass String fieldname, int fieldnumber to the constructor
Doesnt give any sense to me, what you are trying to say :) I want to create it from the Class Field
Thanks! that was greatfull.
@user1098185 felterISpil[0]=new Field("Start",1);
|
1

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

1 Comment

My suggestion may be looks so improper i have Written it through my mobile but its upto the point n cover what u need ok. Hope it helps u
1

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));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.