Re: IllegalArgumentException with addVariableAttribute

Hi Mark:

you cant have an attribute with an array of Strings, only arrays of numbers are allowed. put it all in one string, and decode it when you read, eg

String v = "one, two";
ncFile.addVariableAttribute("edrmap", "units", v.getBytes());

when you read, you can use java.lang.StringTokenizer to break into tokens:

     Attribute att = edrmapVar.findAttribute("units");
     String val = att.getStringValue(); 
     StringTokenizer st = new StringTokenizer(val, ", ");
     while (st.hasMoreTokens()) {

   * Add an attribute of type Array to the named Variable. Must be in define 
* @param String varName: name of attribute. IllegalArgumentException if not valid name.
   * @param String attName: name of attribute.
* @param Object value: must be 1D array of double, float, int, short, char, or byte
public void addVariableAttribute(String varName, String attName, Object value) ;

Mark A Ohrenschall wrote:


I'm trying to define an array-valued variable attribute, e.g.,

ncFile.addVariableAttribute("edrmap", "units", new String [] {"one", "two"});

But I get a run-time error:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid Type: 
class java.lang.String
        at ucar.netcdf.Attribute.<init>(
        at edrmap.Edrmap.<init>(
        at edrmap.Edrmap.main(

I'm able to pass in an array of ints. The API documentation for addGlobalAttribute includes "Remark: this looks buggy; allowed types unclear." Perhaps this is true for addVariableAttribute as well?

I'm using 2.10 beta of the (Java) netCDF library.


Mark Ohrenschall

  • 2002 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: