Pass ArrayList to another Class

17,887

Solution 1

An instance of ClassA needs to be created in the second class. I would recommend creating it within the main method so ClassA is not a dependency for the second class.

DecimalFormat f = new DecimalFormat("#0.00");
public ArrayList<String> listContent; 
DefaultListModel<String> ListModelNew = new DefaultListModel<String>();

public static void main(String args[]) {
ClassA pMain = new ClassA();
listContent = new ArrayList<String>(pMain.collCollect);
for (int i = 0; i < listContent.size(); i++){
        ListModelNew.add(i, listContent.get(i));
    } 
}

This can be further refactored:

DecimalFormat f = new DecimalFormat("#0.00");
DefaultListModel<String> ListModelNew = new DefaultListModel<String>();

public static void main(String args[]) {
ClassA pMain = new ClassA();
for (int i = 0; i < pMain.collCollect.size(); i++){
        ListModelNew.add(i, pMain.collCollect.get(i));
    } 
}

Solution 2

ClassA pMain; // here you not initialized the object.

pMain.collCollect // here you are getting the NullpointerException

public ArrayList<String> listContent = new ArrayList<String>(pMain.collCollect);
                                                                    ^__see here   

change

ClassA pMain;

to

ClassA pMain = new ClassA ();

Solution 3

Your ClassA pMain is not instantiated. You need to create the actual instance of ClassA like this:

ClassA pMain = new ClassA();

Since you're trying to get a public property of that class (collCollect), it would be sane to have that one exist too, either via constructor in ClassA or by calling some method on the newly created instance of ClassA.

Share:
17,887
ZeroGS
Author by

ZeroGS

Startet coding small php scripts and html pages back in 1999. I'm a complete self-educated person when it comes to Computer Science and Coding. My repertoire in programing languages ranges from PHP &amp; PERL, to JAVA and C# (My favourite since a few Years) to C++ and a bit of C. I've worked in the Software Industry for 10 Years now and i've worked for Company's in Germany and Switzerland. Did some self-employed project-based Software for Company's in the UK, US, Canada, Germany, Switzerland, Austria and Australia. I surely don't know everything but like one once said, there are many ways to get to rome ;)

Updated on July 28, 2022

Comments

  • ZeroGS
    ZeroGS almost 2 years

    I'm having some Trouble with an ArrayList. All i need to do is to pass a filled ArrayList to another Class so i can use the Values of the ArrayList there. Here's a snippet from the first Class:

    public ArrayList<String> collCollect = new ArrayList<String>();
    for (int i = 0; i < ListModel.size(); i++) {
            collCollect.add(ListModel.get(i).toString());
        }
        System.out.println(collCollect);
    

    Till this Part everything is going quite well (i stripped the rest of the Code!)

    Now comes the tricky Part! This is the Second Class:

    ClassA pMain;
    DecimalFormat f = new DecimalFormat("#0.00");
    public ArrayList<String> listContent = new ArrayList<String>(pMain.collCollect);
    DefaultListModel<String> ListModelNew = new DefaultListModel<String>();
    
    public static void main(String args[]) {
    for (int i = 0; i < listContent.size(); i++){
            ListModelNew.add(i, listContent.get(i));
        } 
    }
    

    Everytime the ClassB is loaded i get a NullPointerException from the Line where the reference to the Array in pMain is made.

    Any help would be appriciated...i'm unable to get the Values from ClassA ArrayList to ClassB -.-