Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将netcdf变量初始化为全局变量以避免java.lang.NullPointerException?_Java_Netcdf4 - Fatal编程技术网

如何将netcdf变量初始化为全局变量以避免java.lang.NullPointerException?

如何将netcdf变量初始化为全局变量以避免java.lang.NullPointerException?,java,netcdf4,Java,Netcdf4,我打算基于Java编写一个netCDF4文件。我有很多变量,我只想选择一些变量,并只将它们写入netcdf文件。我试图通过以下方式解决这个问题: public class JavaDB { public static Variable time; public static Variable cloud_1st; public static Variable cloud_2nd; public static Variable cloud_3th; pub

我打算基于Java编写一个netCDF4文件。我有很多变量,我只想选择一些变量,并只将它们写入netcdf文件。我试图通过以下方式解决这个问题:

public class JavaDB {

    public static Variable time;
    public static Variable cloud_1st;
    public static Variable cloud_2nd;
    public static Variable cloud_3th;
    public static Variable cloud_4th;
    public static Variable cloud_5th;
    public static Variable Layer_1st;
    public static Variable Layer_2nd;
    public static Variable Layer_3th;
    public static Variable Layer_4th;
    public static Variable Layer_5th;
    public static Variable CBN;

public static void getDataNC(String location, ArrayList<Integer> Values) throws Exception{



        String version = System.getProperty("java.version");
         NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, location, null);
        // add dimensions

        Dimension heightDim = ncfile.addDimension(null,"height",1539);
        Dimension timeDim = ncfile.addUnlimitedDimension("time");

        //define variables
        for(int k=0; k<Values.size(); k++){

            switch(Values.get(k)){


                case 1:  Variable time = ncfile.addVariable(null, "time", DataType.DOUBLE,"time");
                         time.addAttribute( new Attribute("units", "days since 1970-01-01"));  
                         break;
                case 2: Variable cloud_1st = ncfile.addVariable(null,"cloud_1st", DataType.INT,"time");
                        cloud_1st.addAttribute( new Attribute("long_name", "cloud amount in 1st layer1"));
                        cloud_1st.addAttribute( new Attribute("units", "octa"));
                        cloud_1st.addAttribute(new Attribute("_FillValue",-9999));
                        cloud_1st.addAttribute(new Attribute("Vertical Visibility",9));
                        cloud_1st.addAttribute(new Attribute("No enough data",99));
                        break;
                case 3: Variable cloud_2nd = ncfile.addVariable(null,"cloud_2nd", DataType.INT,"time");
                        cloud_2nd.addAttribute( new Attribute("long_name", "cloud amount in 2nd layer"));
                        cloud_2nd.addAttribute( new Attribute("units", "octa"));
                        cloud_2nd.addAttribute(new Attribute("_FillValue",-9999));
                        cloud_2nd.addAttribute(new Attribute("Vertical Visibility",9));
                        cloud_2nd.addAttribute(new Attribute("No enough data",99));
                        break;
                case 4: Variable cloud_3th = ncfile.addVariable(null,"cloud_3th", DataType.INT,"time");
                        cloud_3th.addAttribute( new Attribute("long_name", "cloud amount in 3th layer"));
                        cloud_3th.addAttribute( new Attribute("units", "octa"));
                        cloud_3th.addAttribute(new Attribute("_FillValue",-9999));
                        cloud_3th.addAttribute(new Attribute("Vertical Visibility",9));
                        cloud_3th.addAttribute(new Attribute("No enough data",99));
                        break;
                case 5: Variable cloud_4th = ncfile.addVariable(null, "cloud_4th", DataType.INT, "time");
                        cloud_4th.addAttribute( new Attribute("long_name", "cloud amount in 4th layer"));
                        cloud_4th.addAttribute( new Attribute("units", "octa"));
                        cloud_4th.addAttribute(new Attribute("_FillValue",-9999));
                        cloud_4th.addAttribute(new Attribute("Vertical Visibility", 9));
                        cloud_4th.addAttribute(new Attribute("No enough data",99));
                        break;
                case 6: Variable cloud_5th = ncfile.addVariable(null, "cloud_5th", DataType.INT, "time");
                        cloud_5th.addAttribute( new Attribute("long_name", "cloud amount in 5th layer"));
                        cloud_5th.addAttribute( new Attribute("units", "octa"));
                        cloud_5th.addAttribute(new Attribute("_FillValue",-9999));
                        cloud_5th.addAttribute(new Attribute("Vertical Visibility",9));
                        cloud_5th.addAttribute(new Attribute("no enough data",99)); 
                        break;
                case 7: Variable Layer_1st = ncfile.addVariable(null,"Layer_1st",DataType.INT,"time");
                        Layer_1st.addAttribute(new Attribute("long_name", "1st cloud layer height"));
                        Layer_1st.addAttribute(new Attribute("units","meter"));
                        Layer_1st.addAttribute(new Attribute("_FillValue",-9999));
                        break;
                case 8: Variable Layer_2nd = ncfile.addVariable(null,"Layer_2nd",DataType.INT,"time");
                        Layer_2nd.addAttribute(new Attribute("long_name", "2nd cloud layer height"));
                        Layer_2nd.addAttribute(new Attribute("units","meter"));
                        Layer_2nd.addAttribute(new Attribute("_FillValue",-9999));
                        break;
                case 9: Variable Layer_3th = ncfile.addVariable(null,"Layer_3th",DataType.INT,"time");
                        Layer_3th.addAttribute(new Attribute("long_name", "3th cloud layer height"));
                        Layer_3th.addAttribute(new Attribute("units","meter"));
                        Layer_3th.addAttribute(new Attribute("_FillValue",-9999));
                        break;
                case 10:    Variable Layer_4th = ncfile.addVariable(null,"Layer_4th",DataType.INT,"time");
                            Layer_4th.addAttribute(new Attribute("long_name", "4th cloud layer height"));
                            Layer_4th.addAttribute(new Attribute("units","meter"));
                            Layer_4th.addAttribute(new Attribute("_FillValue",-9999));
                            break;
                case 11:    Variable Layer_5th = ncfile.addVariable(null,"Layer_5th",DataType.INT,"time");
                            Layer_5th.addAttribute(new Attribute("long_name", "5th cloud layer height"));
                            Layer_5th.addAttribute(new Attribute("units","meter"));
                            Layer_5th.addAttribute(new Attribute("_FillValue",-9999));
                            break;

                case 12:    Variable CBN = ncfile.addVariable(null,"CBN",DataType.INT,"time");
                            CBN.addAttribute(new Attribute("long_name","cloud base number"));
                            CBN.addAttribute(new Attribute("units", "number"));
                            CBN.addAttribute(new Attribute("no significant backscatter",0));
                            CBN.addAttribute(new Attribute("obscuration but no cloud",4));
                            CBN.addAttribute(new Attribute("transparent obscuration",5));
                            CBN.addAttribute(new Attribute("_FillValue",-9));
                            break;
                }
}

ncfile.create();


        try{


            ArrayDouble.D1 timeData = new ArrayDouble.D1(countLinesResult);
            ArrayInt.D1 cloudL1Data = new ArrayInt.D1(countLinesResult);
            Index ima = cloudL1Data.getIndex();
            ArrayInt.D1 cloudL2Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cloudL3Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cloudL4Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cloudL5Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cLayer1Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cLayer2Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cLayer3Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cLayer4Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 cLayer5Data = new ArrayInt.D1(countLinesResult);
            ArrayInt.D1 CBNData = new ArrayInt.D1(countLinesResult);


  for (int timeIdx = 0; timeIdx < countLinesResult;timeIdx++){ 
                    for(int k=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(int k=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(Exception e){
            e.printStackTrace();}

    finally{
            try{

                ncfile.close();

            }catch(IOException ex){
                ex.printStackTrace();

            }

        }
公共类JavaDB{
公共静态可变时间;
公共静态变量cloud_1st;
公共静态变量cloud_2nd;
公共静态变量云_3th;
公共静态变量cloud_;
公共静态变量cloud_;
公共静态变量层_1st;
公共静态变量层_2nd;
公共静态变量层_3th;
公共静态变量层_4th;
公共静态变量层_5th;
公共静态变量CBN;
公共静态void getDataNC(字符串位置,ArrayList值)引发异常{
String version=System.getProperty(“java.version”);
NetcdfFileWriter ncfile=NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4,位置,null);
//添加维度
维度heightDim=ncfile.addDimension(null,“高度”,1539);
维度timeDim=ncfile.addUnlimitedDimension(“时间”);
//定义变量

对于(int k=0;k您将两次声明变量,一次声明为静态变量:

public static Variable time;
以及交换机本地范围内的其他:

Variable time = ncfile.addVariable(...);
因此,您正在本地范围内“丢失”初始化对象

通过将初始化对象分配给静态变量声明来替换本地声明:

time = ncfile.addVariable(...);
问候


Antonio

它的可能副本非常简单:您可以初始化它们。我们不知道您的
变量
类,所以不知道构造函数是什么样子,但可以尝试类似
新变量()
的方法。