I am trying to create an instance variable that is an array. I have many methods that will produce certain statistics about the array. I'm wondering if someone can explain to me if I am going about this the correct way. I'm rather new to Java, so any pointers is greatly appreciated. When I run the program I get errors, such like Null. I'm not looking to fix these errors now, I'm just wondering if I am going about this the correct way.
My data class:
import java.util.Arrays;
public class Stat {
private double data[];
public Stat()
{
data = new double[1];
data[0]= 0.0;
}
public Stat(double[] d)
{
d = new double[d.length];
}
public double[] getData()
{
return data;
}
public void setData(double[] d)
{
}
Main method:
double[] data = {1,2,2,3,4,5};
Stat stat1 = new Stat(data);
System.out.println(stat1.getData());
System.out.println("stat1 data = " + stat1.toString());
System.out.println("stat1 min = " + stat1.min());
System.out.println("stat1 max = " + stat1.max());
System.out.println("stat1 average = " + stat1.average());
System.out.println("stat1 mode = " + stat1.mode());
System.out.println("stat1 data = " + stat1.toString());
NullPointerException? Including the stack trace is always a good idea when asking for help (and pointing out which line gets reported, because we don't have your exact file, so line numbers without some indication of where the problem actually is are meaningless.