Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: NullPointerException Issue

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    /**
     * 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.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NullPointerException Issue

    okay so I initialized the constructor for ary1 like so, ary1 = new double[size];. The constructor now looks like this

    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.
    Last edited by Nightshade; October 2nd, 2011 at 02:15 AM.

Similar Threads

  1. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 11:35 AM
  2. NullPointerException ... and does it work?
    By thebestpearl in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 18th, 2011, 01:32 PM
  3. [SOLVED] Nullpointerexception
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 14th, 2010, 10:33 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 12:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 07:06 PM