NullPointerException Issue
I am relatively new to Java programming and I have honestly hit a brick wall with this class lab.
I am creating a program that will do operations on a series of number matrices that will take an array as a parameter. It will then store the result in a third matrix that has an array as a parameter. Think of it as matrix[1] + matrix[1] = matrix[1] and so on. Where I am hitting a brick wall is that I am getting a null pointer exception. I commented it in my code on the line where my JUnit test is seeing it.
So here's the code.
Code Java:
/**
* The DVector class will create a object to be used by the NVector interface
* to implement its methods
*
* @author
*/
public class DVector implements NVectorADT
{
private double[] ary1;
private double[] aryResult;
private int size;
/**
* Constructs the first array and the results array.
* Will throw an unchecked exception if the length of the first array is
* either 0 or array is null.
*
* @param ary1 parameter of the first array
*/
public DVector(double[] ary1)
{
size = ary1.length;
if (ary1.length == 0 || ary1 == null) {
throw new IllegalArgumentException ("Illegal Array");
}
else {
aryResult = new double[ary1.length];
}
}
/**
* Adds the elements of the first matrix and the second matrix and stores the
* result in the result matrix.
* @param y
* @return
*/
@Override
public NVectorADT plus(NVectorADT y) {
DVector ary2 = (DVector) y;
int i = 0;
if (this.size != ary1.length) { //null pointer exception here
throw new IndexOutOfBoundsException("Array indices are of different length");
}
else {
while (i < this.size) {
aryResult[i] = ary1[i] + y.size();
}
}
i++;
size++;
return y;
}
@Override
public NVectorADT minus(NVectorADT y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public NVectorADT mult(NVectorADT y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public NVectorADT divby(NVectorADT y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public double sum() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public NVectorADT sqrt() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int size() {
return size;
}
@Override
public double[] getAsArray() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public boolean equals(NVectorADT y) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public String toString()
{
throw new UnsupportedOperationException("Not supported yet.");
}
}
If you have any questions let me know and I'll answer them to the best of ability. Any help would be appreciated.
Re: NullPointerException Issue
The field ary1 was never initialized in your constructor. It therefore takes the default value of null. Note that this.ary1 != ary1 passed to the constructor as a parameter.
Re: NullPointerException Issue
okay so I initialized the constructor for ary1 like so, ary1 = new double[size];. The constructor now looks like this
Code :
public DVector(double[] ary1)
{
size = ary1.length;
this.ary1 = new double[size];
if (ary1.length == 0 || ary1 == null) {
throw new IllegalArgumentException ("Illegal Array");
}
aryResult = new double[size];
}
When I added the constructor line another error popped up. It threw the IndexOutOfBoundsException error. The NullPointerException error still gets thrown so that suspects to me they are related. Also I am not seeing your note of this.ary1 != ary1 and perhaps you could explain it to me what you are seeing where I am not.