Question

I have this constructor by parameters in my Cassette class:

public class Cassette {

private int cass_Num;
private String cass_Titre;
private String cass_Realisat;
private int nbCopie;
private int [] cass_Emprunteur;

//...code
public Cassette(int num, String titre, String real, int copies, int[] nbEmp){
    this.cass_Num = num;
    this.cass_Titre = titre;
    this.cass_Realisat = real;
    this.nbCopie = copies;
    for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];}
}
//set methods...
//get methods...
}//end

I want to instantiate a few objects of the Cassette class in my Main to test some functions for my assignment. I can't, for the life of me, find the proper way to do it without getting a null.Pointer.Exception

These are the lines in my Main:

//...code
Cassette [] tabCas = new Cassette[MAX_CASSETTES];
for(int i=0;i<tabCas.length;i++){tabCas[i]= new Cassette();}
tabCas[0] = new Cassette(0001,"Jurassic Pork","Steven Swineberg",7,new int[] {11111,44444});    //<--- error here
//...code

Thanks for help!

Was it helpful?

Solution 2

In your Cassette constructor, you want:

cass_Emprunteur = nbEmp;

instead of the line:

for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];}

Or, alternatively, you need to initialize the int[] before using it in the for loop.

OTHER TIPS

Look at your constructor "this.cass_Emprunteur" is null and you try to access with this.cass_Emprunteur[i]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top