Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Netcdf Array.factory(value) NullPointerException #927

Open
antoinetran opened this issue Oct 2, 2017 · 2 comments
Open

Netcdf Array.factory(value) NullPointerException #927

antoinetran opened this issue Oct 2, 2017 · 2 comments

Comments

@antoinetran
Copy link

Hi,

This impact Netcdf Java 4.6.2 and even latest alpha 5.0.0-alpha3:
https://github.com/Unidata/thredds/blob/v5.0.0-alpha3/cdm/src/main/java/ucar/ma2/Array.java

In the method reflectArrayCopyIn below:

  static private void reflectArrayCopyIn(Object jArray, Array aa, IndexIterator aaIter) {
    Class cType = jArray.getClass().getComponentType();
    if (cType.isPrimitive()) {
      aa.copyFrom1DJavaArray(aaIter, jArray);  // subclass does type-specific copy
    } else {
      for (int i = 0; i < java.lang.reflect.Array.getLength(jArray); i++)  // recurse
        reflectArrayCopyIn(java.lang.reflect.Array.get(jArray, i), aa, aaIter);
    }
  }

When the jArray is only a String, cType became null because this is what getComponentType is supposed to give:

If this class does not represent an array class this method returns null.

The caller of this method is Array.factory(Object javaArray), and even its javadoc says:

LOOK: not sure this works for reference types.

Thus the NullPointerException. I recommend adding a check like for example (not tested):

  static private void reflectArrayCopyIn(Object jArray, Array aa, IndexIterator aaIter) {
    Class cType = jArray.getClass().getComponentType();
    if (null == cType || cType.isPrimitive()) {
      aa.copyFrom1DJavaArray(aaIter, jArray);  // subclass does type-specific copy
    } else {
      for (int i = 0; i < java.lang.reflect.Array.getLength(jArray); i++)  // recurse
        reflectArrayCopyIn(java.lang.reflect.Array.get(jArray, i), aa, aaIter);
    }
  }
@antoinetran
Copy link
Author

This is a bit related to #507 because the latter deals with an Array of String instead of just a String.

@cwardgar
Copy link
Contributor

cwardgar commented Oct 5, 2017

Hmm, I think the fix should be made upstream. In Array.factory() (now Array.makeFromJavaArray() in 5.0), detect if javaArray is a reference type, and simply create a list for it if so. For example, a String argument becomes a one-element String[]. That's best because all of the downstream code expects for javaArray to actually be, you know, an array.

Whatever solution we choose, we'll need to add some thorough unit testing, which we obviously lack right now. #600 is also related.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants