Sarp Kaya
Sarp Kaya

Reputation: 3784

Java array of an object variable change

I am new to Java. This is giving out an error.

private class applicantInfo {
    int Id;
    double quality;
}
private class allApplicants {
    applicantInfo[] applicantArr = new applicantInfo[20];
}
public void newGame {
    allApplicants applicants = new allApplicants();
    applicants.applicantArr[0].Id = 5;
}

I am getting an error at the point of applicants.applicantArr[0].Id = 5;.

All I want to do is similar to this in C:

typedef struct _applicantInfo{
    int Id;
    double quality;
} applicantInfo;

typedef struct _allApplicants {
    applicantInfo applicantArr[20];
} allApplicants;

int main () {
    allApplicants applicants;
    applicants.applicantArr[0].Id = 5;  
}

How can I do that in Java?

Upvotes: 0

Views: 137

Answers (3)

Kshitij
Kshitij

Reputation: 361

I would suggest a high-level structure for your code with comments and TODOs, you can fill in the details. And the last part, where I suggest the structure for newGame method, will help you get rid of the error you are getting.

The structure for ApplicantInfo class:

public class ApplicantInfo {
    private int ID;
    private double quality;

    // Constructor to create an instance with the specified ID value
    public ApplicantInfo(int id){
        // TODO: Initialize the value for ID field
    }

    // Method to get the value for ID
    public int getID(){
        // TODO: return value of ID field
    }

    // Method to set the value for ID
    public void setID(int id){
        // TODO: set the value for ID field
    }

    // Getter and setter methods for "quality" 
    // on the lines of the above methods
}

The structure for AllApplicants class:

public class AllApplicants {
    private ApplicantInfo[] applicantArr = new ApplicantInfo[20];

    // Method to get the applicant info at a given index
    public ApplicantInfo getApplicant(int index){
        // TODO: Get the applicant from the array present at the specified index
    }

    // Method to add an applicant info at a given index
    public boolean addApplicant(ApplicantInfo applicant, int index){
        // TODO: Try to add the specified applicant to the array at the specified index
        // Return true to indicate that the applicant was successfully added, 
        // Return false to indicate that an applicant is already present at the specified index
    }
}

As this is just a skeleton to The structure for newGame method:

public void newGame {
    AllApplicants applicants = new AllApplicants();

    // In order to achieve doing "applicants.applicantArr[0].Id = 5;", you
    // need to do the following.

    // Create a new applicant info with ID as 5
    ApplicantInfo applicant = new ApplicantInfo(5);

    // Add the applicant to the applicant array at index 0
    applicants.addApplicant(applicant, 0);
}

In addition to reading about array, as mentioned by @codeman, you may also want to take a look at Java Naming Convention

Upvotes: 0

codeMan
codeMan

Reputation: 5758

you need to do this in your newGame():

applicantInfo item = new applicantInfo();//first create a applicantInfo object
item.Id= 5;//set the object properties
applicants.applicantArr[0]= item;//assign the object to the array

this is because Arrays work in a different way in Java than those in C. Take a look at this

and also here is a tutorial to get you started with.

Upvotes: 1

Sinkingpoint
Sinkingpoint

Reputation: 7624

The difference between Java and C arrays is that C initialises all the values in the Array, whilst Java sets them to null. So when you call

applicants.applicantArr[0].Id = 5;

You will get a NullPointerException, as applicants.applicantArr[0] is null. You need to create a new applicantInfo and put it into the array before accessing it:

allApplicants applicants = new allApplicants();
applicants.applicantArr[0] = new applicantInfo();
applicants.applicantArr[0].Id = 5;

Upvotes: 4

Related Questions