0

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!

1
  • Look at the exception's stack trace and see which line is throwing it? Then see how can the variable on that line be null. Commented Feb 8, 2014 at 0:55

2 Answers 2

1

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

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

Comments

0

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.

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.