I get this error and i don't know what is wrong i declared a new class and initialized it yet the compiler still gives me the error above.
public class Vec
{
private double[] v;
public Vec(int length)
{
double[] v= new double[length];
for(int i=0;i<length;i++)
{
v[i] = 0.0;}
}
public int length()
{return v.length;}
public double[] value()
{return v;}
public Vec(Vec in)
{v=new double[in.length()];
for(int i=0;i<in.length();i++)
v[i]=in.v[i];
return;
}
public Vec(double[]in)
{v= new double[in.length];
for(int i=0;i<in.length;i++)
this.v[i]=in[i];
return ;
}
public static Vec add(Vec in,Vec in2)
{
if(in.v.length!=in2.v.length) // Line 49
{
return null;
}
Vec out = new Vec(in.v.length);
for(int i=1;i<=in.length();i++)
{
out.v[i] = in.v[i]+in2.v[i];
}
return out;
}
public String tostring()
{
if(v.length==0)
{
return "Zero Dimensions";
}
String s="";
s="My vector is = ";
for(int i=0;i<v.length;i++)
s=s+Double.toString(v[i])+"n";
return s;
}
}
public class Main {
public static void main(String[] args){
Vec v1=new Vec(new double[]{1,3,5});
Vec v2=new Vec(new double[]{2,4,6});
Vec v3=new Vec(3);
v3=Vec.add(v1, v2); // Line 25
String s=v3.toString();
System.out.println(s);
System.exit(0);
}
}
Exception in thread "main" java.lang.NullPointerException
at javaapplication6.Vec.add(Vec.java:49)
at javaapplication6.Main.main(Main.java:25)
Java Result: 1