0

I have two files TestingArrayList.java and ArrayListClass. I am trying to access ArrayList object created in ArrayListClass from TestingArrayList class. Even though the list is populated in ArrayListClass. size() method on ArrayList object from TestingArrayList is returning zero.

TestingArrayList.java

class TestingArrayList {

    public static void main(String[] args) {
        System.out.println(ArrayListClass.justList.size());
    }

}

ArrayListClass.java

import java.util.*;

public class ArrayListClass {

    public static ArrayList<String> justList = new ArrayList<String>();

    public static void main(String[] args) {
        justList.add("I am working ");
        while(true);
    }
}

How can I access ArrayList efficiently from another class? Am I handling the ArrayList appropriately or is there something wrong with this ?

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
saikumarm
  • 1,565
  • 1
  • 15
  • 30

3 Answers3

2

If you start two main methods, then you are starting two separate virtual machines. Both VMs have their own objects and static variables.

If you want to run both main methods parallel, you probably want something like this:

public static void main(String[] args) {
    new Thread() { run() { 
        ArrayListClass.main(null);
    }}.start();

    new Thread() { run() { 
        TestingArrayList.main(null);
    }}.start();
}

Only start this main

Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
0

You need to initialize the list in order for it to actually be diferent from null

public static void main(String args[]) {
    System.out.println(ArrayListClass.justList.size()); // ArrayListClass has nothing initilized in it.
}

Now you need to initliaze a static list! (Static Initialization Blocks) for that append in your ArrayListClass ann initlaisation

static{
        System.out.println("Init list here"); // this one is called once only.
    }

Read more about blocks: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html and Static Initializers And Static Methods In Java

Community
  • 1
  • 1
Bogdan M.
  • 2,161
  • 6
  • 31
  • 53
  • This is actually a `snippet` from a project I am working on, There I cannot use `static blocks` – saikumarm Nov 07 '14 at 09:27
  • There I am populated the List when certain object gets created. I am accessing this list in a different class which is in different file. Static blocks work just once and before the main begins, I think. – saikumarm Nov 07 '14 at 09:30
  • Well yes then you can do what you want with the list, you question was about the null pointer is this answere not fising null pointer? Have even glimpsed the links i posted? I recommand you for what you need to create a class using singlethon pattern and use that if you wanna use that object over the application. but for your requirements this resources should help if – Bogdan M. Nov 07 '14 at 09:34
0

It is probably because you are running main method from TestingArrayList.java, so justList is not populated. Change main method inArrayListClass.java to be a constructor and then call it in the TestingArrayList.java main method before you are calling for justList.size().

damian_pol
  • 37
  • 1
  • 8
  • 1
    `justList` is `static`, if you want to populate it in the constructor, it's a bad idea to keep it `static`, because every time you create a new instance of the object, you will add new entries into the list. – Florent Bayle Nov 07 '14 at 09:28
  • I know. This could be useful when you count how many instances of particular class has been created. But what I understood from @sai_kumar question is that he wants to populate it ArrayListClass.java. Better way to do it is to create another public method in that class that will populate this static list. But here I cannot see any usage of what he is doing, so it can be done in constructor. – damian_pol Nov 07 '14 at 09:37