I have assigned value to an array in one class but array shows null in another class, why? (Fraction of code is provided)

Like n Share :
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);
    }
}

On displaying array of objects all_mappers[i].size it shows null while size of all_mappers[i] has been defined by calling the constructor of mapper class. I want to display the size of each mapper through engine class and define size of each mapper in graph class.

Rahat Ali :
        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);
    }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20878&siteId=1