I have with two fields: long nr and a vector of long like below:
public class Pair {
public long nr;
public Vector<Long> lines;
public Pair(long ap, long line){
this.nr=ap;
if (line!=0) lines.add(linie);
else lines=null;
}
public void create (long line){
nr++;
lines.add(line);
}
}
I want to have a function(create) so it modifies the fields of the class. In the main class I have
Pair per1=new Pair(0,0);
Pair per2=new Pair(0,0);
per1.create(3);
per2.create(4);
The constructor works fine, but create doesn’t. What is the explanation, and how should the function look like?Thanks a lot.
You never created instance of Vector by calling
new Vector<Long>()in your code. Moreover you are setting your class variablelinestonullifline == 0.Your code should like this: