NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.
To learn about what's going on, see About the Archive Site.
Dear Beáta, I have answered to you at StackOverflow: You are declaring your variables twice, once as static: public static Variable time; and other in the local scope of your switch: Variable time = ncfile.addVariable(...); and therefore you are "losing" the initialized Object in the local scope.Replace the local declaration by just the assigning the initialized Object to the static variable declaration:
time = ncfile.addVariable(...); Regards Antonio S. Cofiño P.S.: I have already answered to your question at StackOverflow: https://stackoverflow.com/questions/50964120/how-can-i-initialize-the-netcdf-varables-as-global-variables-to-avoid-java-lang On 22/06/18 08:43, Beáta Szabó-Takács wrote:
Dear All,I would like to write a netCDF4 file in Java based on https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfWriting.html. I have a lot of variables and I would like to select just some and write only them to netcdf file. I tried to solve this by the the following way:publicclassJavaDB{ publicstaticVariabletime; publicstaticVariablecloud_1st; publicstaticVariablecloud_2nd; publicstaticVariablecloud_3th; publicstaticVariablecloud_4th; publicstaticVariablecloud_5th; publicstaticVariableLayer_1st; publicstaticVariableLayer_2nd; publicstaticVariableLayer_3th; publicstaticVariableLayer_4th; publicstaticVariableLayer_5th; publicstaticVariableCBN;publicstaticvoidgetDataNC(Stringlocation, ArrayList<Integer> Values) throwsException{Stringversion = System.getProperty("java.version");NetcdfFileWriterncfile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, location, null);// add dimensions DimensionheightDim = ncfile.addDimension(null,"height",1539); DimensiontimeDim = ncfile.addUnlimitedDimension("time"); //define variables for(intk=0; k<Values.size(); k++){ switch(Values.get(k)){case1: Variabletime = ncfile.addVariable(null, "time", DataType.DOUBLE,"time");time.addAttribute( newAttribute("units", "days since 1970-01-01")); break;case2: Variablecloud_1st = ncfile.addVariable(null,"cloud_1st", DataType.INT,"time");cloud_1st.addAttribute( newAttribute("long_name", "cloud amount in 1st layer1"));cloud_1st.addAttribute( newAttribute("units", "octa")); cloud_1st.addAttribute(newAttribute("_FillValue",-9999)); cloud_1st.addAttribute(newAttribute("Vertical Visibility",9)); cloud_1st.addAttribute(newAttribute("No enough data",99)); break;case3: Variablecloud_2nd = ncfile.addVariable(null,"cloud_2nd", DataType.INT,"time");cloud_2nd.addAttribute( newAttribute("long_name", "cloud amount in 2nd layer"));cloud_2nd.addAttribute( newAttribute("units", "octa")); cloud_2nd.addAttribute(newAttribute("_FillValue",-9999)); cloud_2nd.addAttribute(newAttribute("Vertical Visibility",9)); cloud_2nd.addAttribute(newAttribute("No enough data",99)); break;case4: Variablecloud_3th = ncfile.addVariable(null,"cloud_3th", DataType.INT,"time");cloud_3th.addAttribute( newAttribute("long_name", "cloud amount in 3th layer"));cloud_3th.addAttribute( newAttribute("units", "octa")); cloud_3th.addAttribute(newAttribute("_FillValue",-9999)); cloud_3th.addAttribute(newAttribute("Vertical Visibility",9)); cloud_3th.addAttribute(newAttribute("No enough data",99)); break;case5: Variablecloud_4th = ncfile.addVariable(null, "cloud_4th", DataType.INT, "time");cloud_4th.addAttribute( newAttribute("long_name", "cloud amount in 4th layer"));cloud_4th.addAttribute( newAttribute("units", "octa")); cloud_4th.addAttribute(newAttribute("_FillValue",-9999)); cloud_4th.addAttribute(newAttribute("Vertical Visibility", 9)); cloud_4th.addAttribute(newAttribute("No enough data",99)); break;case6: Variablecloud_5th = ncfile.addVariable(null, "cloud_5th", DataType.INT, "time");cloud_5th.addAttribute( newAttribute("long_name", "cloud amount in 5th layer"));cloud_5th.addAttribute( newAttribute("units", "octa")); cloud_5th.addAttribute(newAttribute("_FillValue",-9999)); cloud_5th.addAttribute(newAttribute("Vertical Visibility",9)); cloud_5th.addAttribute(newAttribute("no enough data",99)); break;case7: VariableLayer_1st= ncfile.addVariable(null,"Layer_1st",DataType.INT,"time");Layer_1st.addAttribute(newAttribute("long_name", "1st cloud layer height"));Layer_1st.addAttribute(newAttribute("units","meter")); Layer_1st.addAttribute(newAttribute("_FillValue",-9999)); break;case8: VariableLayer_2nd= ncfile.addVariable(null,"Layer_2nd",DataType.INT,"time");Layer_2nd.addAttribute(newAttribute("long_name", "2nd cloud layer height"));Layer_2nd.addAttribute(newAttribute("units","meter")); Layer_2nd.addAttribute(newAttribute("_FillValue",-9999)); break;case9: VariableLayer_3th= ncfile.addVariable(null,"Layer_3th",DataType.INT,"time");Layer_3th.addAttribute(newAttribute("long_name", "3th cloud layer height"));Layer_3th.addAttribute(newAttribute("units","meter")); Layer_3th.addAttribute(newAttribute("_FillValue",-9999)); break;case10: VariableLayer_4th= ncfile.addVariable(null,"Layer_4th",DataType.INT,"time");Layer_4th.addAttribute(newAttribute("long_name", "4th cloud layer height"));Layer_4th.addAttribute(newAttribute("units","meter")); Layer_4th.addAttribute(newAttribute("_FillValue",-9999)); break;case11: VariableLayer_5th= ncfile.addVariable(null,"Layer_5th",DataType.INT,"time");Layer_5th.addAttribute(newAttribute("long_name", "5th cloud layer height"));Layer_5th.addAttribute(newAttribute("units","meter")); Layer_5th.addAttribute(newAttribute("_FillValue",-9999)); break; case12: VariableCBN = ncfile.addVariable(null,"CBN",DataType.INT,"time"); CBN.addAttribute(newAttribute("long_name","cloud base number")); CBN.addAttribute(newAttribute("units", "number")); CBN.addAttribute(newAttribute("no significant backscatter",0)); CBN.addAttribute(newAttribute("obscuration but no cloud",4)); CBN.addAttribute(newAttribute("transparent obscuration",5)); CBN.addAttribute(newAttribute("_FillValue",-9)); break; } } ncfile.create(); try{ ArrayDouble.D1 timeData = newArrayDouble.D1(countLinesResult); ArrayInt.D1 cloudL1Data = newArrayInt.D1(countLinesResult); Indexima = cloudL1Data.getIndex(); ArrayInt.D1 cloudL2Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cloudL3Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cloudL4Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cloudL5Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cLayer1Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cLayer2Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cLayer3Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cLayer4Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 cLayer5Data = newArrayInt.D1(countLinesResult); ArrayInt.D1 CBNData= newArrayInt.D1(countLinesResult); for(inttimeIdx = 0; timeIdx < countLinesResult;timeIdx++){ for(intk=0; k<Values.size(); k++){ // System.out.println(Values.get(k)); if(Values.get(k)==1){ timeData.setDouble(timeIdx,tdate.get(timeIdx));} if(Values.get(k)==2){ cloudL1Data.setInt(ima.set(timeIdx), cloud_layer_1st.get(timeIdx));} if(Values.get(k)==3){ cloudL2Data.setInt(ima.set(timeIdx), cloud_layer_2nd.get(timeIdx));} if(Values.get(k)==4){ cloudL3Data.setInt(ima.set(timeIdx), cloud_layer_3th.get(timeIdx));} if(Values.get(k)==5){ cloudL4Data.setInt(ima.set(timeIdx), cloud_layer_4th.get(timeIdx));} if(Values.get(k)==6){ cloudL5Data.setInt(ima.set(timeIdx), cloud_layer_5th.get(timeIdx));} if(Values.get(k)==7){cLayer1Data.setInt(ima.set(timeIdx), LayerC_1st.get(timeIdx));}if(Values.get(k)==8){ cLayer2Data.setInt(ima.set(timeIdx), LayerC_2nd.get(timeIdx));} if(Values.get(k)==9){ cLayer3Data.setInt(ima.set(timeIdx), LayerC_3th.get(timeIdx));} if(Values.get(k)==10){ cLayer4Data.setInt(ima.set(timeIdx), LayerC_4th.get(timeIdx));} if(Values.get(k)==11){ cLayer5Data.setInt(ima.set(timeIdx), LayerC_5th.get(timeIdx));} if(Values.get(k)==12){ CBNData.setInt(ima.set(timeIdx),nbcb.get(timeIdx));} } } // write the data into netCDF file for(intk=0; k<Values.size(); k++){ if(Values.get(k)==1){ ncfile.write(time, timeData);} if(Values.get(k)==2){ ncfile.write(cloud_1st, cloudL1Data);} if(Values.get(k)==3){ ncfile.write(cloud_2nd, cloudL2Data);} if(Values.get(k)==4){ ncfile.write(cloud_3th, cloudL3Data);} if(Values.get(k)==5){ ncfile.write(cloud_4th, cloudL4Data);} if(Values.get(k)==6){ ncfile.write(cloud_5th, cloudL5Data);} if(Values.get(k)==7){ ncfile.write(Layer_1st, cLayer1Data);} if(Values.get(k)==8){ ncfile.write(Layer_2nd, cLayer2Data);} if(Values.get(k)==9){ ncfile.write(Layer_3th, cLayer3Data);} if(Values.get(k)==10){ ncfile.write(Layer_4th, cLayer4Data);} if(Values.get(k)==11){ ncfile.write(Layer_5th, cLayer5Data);} if(Values.get(k)==12){ ncfile.write(CBN,CBNData);} } }catch(Exceptione){ e.printStackTrace();} finally{ try{ ncfile.close(); }catch(IOExceptionex){ ex.printStackTrace(); } }But after I run the code I get java.lang.NullPointerException at ucar.nc2.NetcdfFileWriter.write(NetcdfFileWriter.java:939). I tried to solve it with define global variables size e.g public static Variable time = new Variable(); but it is not possible. Could you please informe me how I should fix this issue?Thank you for your help in adavnce! Best regards, Beata _______________________________________________ NOTE: All exchanges posted to Unidata maintained email lists are recorded in the Unidata inquiry tracking system and made publicly available through the web. Users who post to any of the lists we maintain are reminded to remove any personal information that they do not want to be made public. netcdf-java mailing list netcdf-java@xxxxxxxxxxxxxxxx For list information or to unsubscribe, visit: http://www.unidata.ucar.edu/mailing_lists/
netcdf-java
archives: