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

please use code tags so we can read your code. Also, please delete your double post (if you can, or edit one so its says "admin please delete, double post" if there is no delete option!

if(in.v.length!=in2.v.length)

this is the line in question. There are four things which could throw the exception.

in.v

- if in is null

in2.v

- if in2 is null

in.v.length

- if in.v is null

in2.v.length

- if in2.v is null

Try using a debugger to check which is null? Alternatively, print out each component (one for each thing above) and you will find which specific part is null. You will then need to find out why, and correct it.

1. Program listing and Exception messages do not match (line 48 is not in Vec.add)
2. Please stop creating new threads for the same problem

Thanks for the code tags. I also suggest you use some kind of basic format, a for loop like this:

for(blabla)
{
   doThis();}

is confusing, keep to this:

for(blabla)
{
   doThis();
}
//or
for(blabla){
   doThis();
}

and try to keep your indentation. I compiled your code, and found the error was on the line

if(in.v.length!=in2.v.length)

as you indicated but on the one a few below:

out.v[i] = in.v[i]+in2.v[i];

I checked with a debugger and its the out.v that is null. You create out with the constructor Vec(int length) so the problem is there, specifically the first line (there is a difference between local and global variables)!

Also note that arrays start at 0 in Java, so your for loops should read:

for(int i=0;i<in.length();i++)
//not
for(int i=1;i<=in.length();i++)

Just to note, how I would format your code:

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)
		{
			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 Vec(int length)
    {
      double[] v= new double[length];
      for(int i=0;i<length;i++)
      {
          v[i] = 0.0;}
 
    }

in the for loop you must use "v.length()" or "length - 1" since arras start with 0, a 10 element arrays last element's index is 9.

public Vec(int length)
    {
      double[] v= new double[length];
      for(int i=0;i<length;i++)
      {
          v[i] = 0.0;}
 
    }

in the for loop you must use "v.length()" or "length - 1" since arras start with 0, a 10 element arrays last element's index is 9.

err, I think the for loop is correct, the issue is with the first line. "length" is the number of array elements, since he is starting indexing at 0 and increasing the index while it is less than the length, then the for loop will terminate at 9. Which is correct. v.length() doesn't return the the array length - 1, since he is declaring the length of v with length, length and v.length() are identical.

Some of his other for loops were incorrect though (starting at 1 and ending <= v.length())

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.