iFeature.getValue - null pointer - Java

1021
3
06-04-2010 08:18 AM
LeoDonahue
Occasional Contributor III
ArcGIS Server 9.3.1

I have a featureCursor that contains the result of a spatial query.  There is one record returned from the spatial query.

Some of the field values in my record have no values, which I guess are null values?  Everytime I run this program, I get a null pointer exception, whenever the loop encounters a field value that is null.

I've tried assinging the result of iFeature.getValue(int) to a temp Object, then testing whether that temp Object==null.  If it is null, then don't assign this value to a Bean property.

This works on most of the fields, except some.  I looked up the Java Docs for IField to see what type of field value I should have from the SDE featureclass.
http://resources.esri.com/help/9.3/arcgisserver/adf/java/help/api/arcobjects/com/esri/arcgis/geodata...

Should I get the value of the field using either of the following?

iFeature.getFields().getField(int).getDefaultValue();
or
iFeature.getValue(int).toString();

featureCursor = featureClass.search(spatFilter, true);
...
iFeature = featureCursor.nextFeature();

if iFeature is not null
  loop through the number of features
    loop through the number of fields
      get the value of each field using the index - this is where I have a problem
      iFeature.getValue(int).toString();
0 Kudos
3 Replies
KirkKuykendall
Occasional Contributor III
Did you try checking to see if IField.getVarType() returns vbNull ?
0 Kudos
LeoDonahue
Occasional Contributor III
I wasn't, but I just did, and the null field in question returns a Long data type, which it is.

This code will loop through and print the field name, field value and field varType.  Fields that are null, will print null, but I never see a vbNull constant of 1 in the output.  Either 3's or 8's.

The Java Bean properties are all mapped to the same primitive data types as what is printed in the output.

The null pointer is now gone, using this code, but I have a NumberFormatException now on one of the Long data type fields that contains a null value.  The Tomcat exception report is pointing to a specific line number in the Java Bean.  That property at that line number is a Long, but the code is passing a null, so there is where my NumberFormatException is ocurring.

Looks like I'll have to change my primitive variable types to Object Wrapper types.

            if(iFeature != null){
                for(int i = 0; i < recordCount; i++){
                    for(int j = 0; j < fieldCount; j++){
                        tempValue = iFeature.getValue(j);
                        tempVarType = iFeature.getFields().getField(j).getVarType();
                        System.out.println(iFeature.getFields().getField(j).getName() + " : " + tempValue + " : " + tempVarType);
                        if((tempValue==null)||(tempVarType==1)){
                            
                        } else {
                            recordsetfields = iFeature.getValue(j).toString();
                        }
                    }
0 Kudos
LeoDonahue
Occasional Contributor III
This last post is mostly informational.  Thanks to Kirk, for helping me realize the solution.

The issue at hand is having a value of <null> in a field's value and needing to assign that field value to a variable of the field's datatype.  My null pointer was ocurring when trying to assign a <null> to a Long Integer, String, etc. variable.

I tested for field value and the field varType, then assigned the appropriate value (for my situation) to the variable.

if(iFeature != null){
    for(int i = 0; i < recordCount; i++){
        for(int j = 0; j < fieldCount; j++){
            tempValue = iFeature.getValue(j);
            tempVarType = iFeature.getFields().getField(j).getVarType();

            // check field valueType for Long Integer (3), Double (5), or String (8)
            // and convert <null> data to appropriate data type value
            if((tempValue==null)&&(tempVarType==3)){
                recordsetfields = "0";
            } else if((tempValue==null)&&(tempVarType==5)){
                recordsetfields = "0.0";
            } else if((tempValue==null)&&(tempVarType==8)){
                recordsetfields = "";
            } else {
                recordsetfields = iFeature.getValue(j).toString();
            }
        }
... etc
0 Kudos