私は別のクラスであるクラスが、配列のショーはnullに配列に値を割り当て、なぜか?(コードの一部が提供されます)

同様のnシェア:
public class Graph extends Framework  {

    Graph graph;
    Mapper[] all_mappers=new Mapper[Framework.Mapper];
    public void createGraph(Graph graph)
    {
        for(int i=0;i<Framework.Mapper;i++)
        {
            all_mappers[i]=new Mapper((double)5);
            System.out.println("For mapper "+(i+1)+" following are parameters: ")
            System.out.println();

        }
    }
}

class Mapper {

    Mapper mp;
    public double size;
    Mapper(double size)
    {

        this.size=size;

    }
}

public class Engine {
    Engine en;

    public void send_mapper(Engine en)
    {
        for(int i=0;i<Framework.Mapper;i++)
        {
            Graph array=new Graph();
            System.out.println("This is the array:"+array.all_mappers[i]);
        }
    }
}

public class Framework {

    public static int Mapper;

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        //Loading config.properties file.
        Properties prop=new Properties();
        FileInputStream ip=new FileInputStream("config.properties");
        prop.load(ip);

        Mapper= Integer.parseInt(prop.getProperty("Mappers"));

        //Calling on function in Graph class
        Graph g=new Graph();
        g.createGraph(g);


        //Calling on function in Engine class
        Engine en=new Engine();
        en.send_mapper(en);
    }
}

オブジェクトの配列を表示する上でall_mappers[i].sizeの大きさがつつ、ヌルを示すall_mappers[i]マッパークラスのコンストラクタを呼び出すことにより、定義されています。私はエンジンクラスを介して各マッパーのサイズを表示し、グラフクラスで各マッパーのサイズを定義します。

快適なアリ:
        Basic points to be remember and follow the standard while writing the java code.

        1. Don't name any variable strating with Capital letter like you named Mapper as int. If you are going to declare static varibale name like MAPPER
        2. Don't extend the class unless it is very necessary or you are going to follow the OOPS concept i.e class has IS-A or HAS-A releationship. I did not get the point why you have extends Framework in class Graph Ex- **public class Graph extends Framework**
        3. If you are going to initialize the size you should always declare as int. I am not sure why you have written constructor like **Mapper(double size)**

        So above discussion was regarding basic standard to code in Java. Now I will come to your question why you are getting null as value because when you are calling send_mapper(en) by passing Engine object you are again creating Graph object which will initialize nothing as there is no constructor.

    Call below code from Framework class as below.

            Graph g=new Graph();
            g.createGraph(g);

            Engine en=new Engine();
            en.send_mapper(en,g);
And edit Engine class method as below

    public void send_mapper(Engine en,Graph graph)
    {

    for(int i=0;i<Framework.MAPPER;i++)
    {

     System.out.println("This is the array:"+graph.all_mappers[i].size);
    }
    }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=21097&siteId=1