3

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 -.-

3
  • 2
    You need to initialize pMain. ClassA pMail = new ClassA(); Commented Nov 25, 2013 at 12:37
  • Very strange code! Could you send the complete code? Commented Nov 25, 2013 at 12:37
  • can you paste the stack trace? Commented Nov 25, 2013 at 12:39

7 Answers 7

3

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 ();
Sign up to request clarification or add additional context in comments.

Comments

2

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));
    } 
}

1 Comment

I forgot to initialize the ClassA -.- Ok but now nothing is added to the ListModelNew?!?! The StandardOutPrint shows me "[]"?!? I'm getting more and more confused
1

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.

Comments

0

Well you just declare the variable pMain but never assign it anything. Thus it will be initialized with null and give you a NullPointerException when you try to access it.

You must either declare it like ClassA pMain = new ClassA() or assign a value elsewhere. If you assign the value elsewhere be sure to remove the direct initialization of listContent

Comments

0

Instead of having the instantiator at the top of your class:

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

Override the constructor for your second class, and pass it in there:

public ClassB(ArrayList<String> pMain) {
  this.listContent = pMain.collCollect;
}

Comments

0

@Ben, You can keep array initialization code in collCollectInit() of ClassA. In second class create ClassA object as ClassA pMain=new ClassA(); pMain.collCollectInit(); then use pMain.collCollect if collCollect is a public or protected reference in ClassA. Please use getter and setter method to access class variable.

Comments

0

Ok i must have been blind not to initialize the ClassA in ClassB...that is fixed now. But the ListModelNew doesn't have any content it is out printed as "[]" so i think the ArrayList in ClassA is not passing the Values properly to ArrayList listContent in ClassB

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.