0

I wish to create a java method that returns an array of type: ABCout, where class ABCout is defined as:

public class ABCout {
    public int numOut;
    public double[] myArray;
}

and the java method is:

public ABCout[] GetABC( double myInput ) throws Exception {
   ABCout[] userABC = new ABCout[3];

   userABC[0].numOut = 10;
   userABC[0].myArray = new double[1];
   userABC[0].myArray[0] = myInput;
   /* here I only fill the 0'th element, but once working I will fill the others */

   return userABC;
}

but I'm getting the error: java.lang.NullPointerException : null

Anyone see what I'm doing wrong?

5 Answers 5

4

You initialized the array, but not the objects in it. You probably need to do:

ABCout[] userABC = new ABCout[3];
for (int i = 0; i < userABC.length; ++i) {
    userABC[i] = new ABCout();
}

Also, you need to instantiate myArray:

public class ABCout {
    public int numOut;
    public double[] myArray;

    public ABCout() {
        myArray = new double[10];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'd prefer to instantiate outside of the class definition because the length will depend on myInput (received in method: GetABC), although not illustrated this way above for simplicity.
2

You need to instantiate the ABCout object you store in the array.

public ABCout[] GetABC( double myInput ) throws Exception {
   ABCout[] userABC = new ABCout[3];

   userABC[0] = new ABCout(); // instantiate

   userABC[0].numOut = 10;
   userABC[0].myArray = new double[1];
   userABC[0].myArray[0] = myInput;
   /* here I only fill the 0'th element, but once working I will fill the others */

   return userABC;
}

Comments

2

You're declaring an array of ABCout, but you're trying to access the first element of that array before assigning it.

public ABCout[] GetABC( double myInput ) throws Exception {
    ABCout[] userABC = new ABCout[3];

    userABC[0] = new ABCout();
    userABC[0].numOut = 10;
    userABC[0].myArray = new double[1];
    userABC[0].myArray[0] = myInput;
    return userABC;
}

Comments

0
//add this
userABC[0] = new ABCount();

 userABC[0].numOut = 10;
   userABC[0].myArray = new double[1];
   userABC[0].myArray[0] = myInput;

Although you definetly need to study some object oriented coding before doing + you should read some basic java tutorial as well

Comments

0

As others have stated, there was no instantiation of the 0th object and therefore you had a runtime error. You could also do this:

public ABCout[] GetABC( double myInput ) throws Exception {
 ABCout[] userABC = new ABCout[3];

 ABCout current = new ABCout();
 current.numOut = 10;
 current.myArray = new double[1];
 current.myArray[0] = myInput;

 userABC[0] = current;

 return userABC;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.