I am currently learning Java and am trying to work through learning generics. I have the class Holder which takes varargs. I am trying to place these into an array myArray for future use. However I get a NullPointerException on the arraycopy line and I can't seem to figure out why.
class Holder<T> {
T[] myArray;
public Holder(T... args) {
System.arraycopy(args, 0, myArray, 0, args.length); //null pointer exception
}
This is what I am using to create the instance:
Holder<Integer> mySample = new Holder<>(1,2,3);
myArrayis not initialised (it isnull)argsormyArrayis null).myArray = args.clone();