ONE ARRAYLIST IN MANY CLASSES QUESTION

J

justineee

Question..

I have an arraylist named accountNameList in the Journal.class, then I
have another class named Ledger.class.. Can I still read the values of
the arraylist in the Journal.class and do the same things I can do
with the ArrayList? like list.get(0), list.size() and other methods
that I made in other classes? Or display the values of the ArrayList
in a TextArea in the Ledger.class?

Thanks
Justine
 
L

Lew

justineee said:
I have an arraylist [sic] named accountNameList in the Journal.class, then I

Do you mean an ArrayList, as in java.util.ArrayList?
have another class named Ledger.class.. Can I still read the values of

You don't need to tell us the file name, i.e., the portion that is
".class". The class name does not include the file suffix, so
actually the name of the 'Ledger' class is "Ledger". (Strictly
speaking, even that is incorrect without the package part of the
name.)

The fact that you have that bytecode stored in a file named
"Ledger.class" is tangential and not necessary. Regardless of where
the bytecode is stored, once it's loaded into the JVM such details
vanish.
the arraylist [sic] in the Journal.class and do the same things I can do
with the ArrayList? like list.get(0), list.size() and other methods
that I made in other classes? Or display the values of the ArrayList
in a TextArea in the Ledger.class?

Actions don't happen in classes, /per se/, they happen in methods.
Pass the 'List' as a method argument.
 
J

justineee

justineee said:
I have an arraylist [sic] named accountNameList in the Journal.class, then I

Do you mean an ArrayList, as in java.util.ArrayList?
have another class named Ledger.class.. Can I still read the values of

You don't need to tell us the file name, i.e., the portion that is
".class".  The class name does not include the file suffix, so
actually the name of the 'Ledger' class is "Ledger".  (Strictly
speaking, even that is incorrect without the package part of the
name.)

The fact that you have that bytecode stored in a file named
"Ledger.class" is tangential and not necessary.  Regardless of where
the bytecode is stored, once it's loaded into the JVM such details
vanish.
the arraylist [sic] in the Journal.class and do the same things I can do
with the ArrayList? like list.get(0), list.size() and other methods
that I made in other classes? Or display the values of the ArrayList
in a TextArea in the Ledger.class?

Actions don't happen in classes, /per se/, they happen in methods.
Pass the 'List' as a method argument.

Sorry for the typo errors.
How will I pass the 'List' as a method argument and use it in the
Ledger as it was in the Journal?

public ArrayList passArrayList ()
{
}

????
 
M

Mark Space

justineee said:
public ArrayList passArrayList ()
{
}


That's (almost) one way of doing it. It would be helpful if you posted
more code though, there's not a lot here to go on. The problem with
what you are trying to do is it's bad programming principles.

public class Journal {
public ArraryList accountNameList;
//...
}

Now anyone can use "accountNameList." I hope you see though that
allowing access by anyone to your internal variables is a bad idea.

public class Ledger {
public static void main( String... args ) {
Journal j = new Journal();
System.out.println( j.accountNameList );
}
}

This should work fine, baring any typoes made by me.

However as I said it's not good practice.

So for your code snippet (repeated here):
> public ArrayList passArrayList ()
> {
> }

Try something like this:

public class Journal {
public ArraryList accountNameList;
//...
public ArrayList getArrayList() { // changed name
return accountNameList; // or accountNameList.clone();
}

But this is little better than just allowing public access to your
internal variable. The best way to do this is just to re-implement the
methods you need in terms of the underlying ArrayList

public class Journal {
public ArraryList accountNameList;
//...
public boolean add( Object o ) {
return accountNameList.add( o );
}
public Object get( int i ) {
return accountNameList.get(i);
}
public int size() {
return accountNameList.size();
}
}

Stuff like that is much better encapsulated than actually allowing
access to an internal variable.
 
L

Lew

That's (almost) one way of doing it.  It would be helpful if you posted
more code though, there's not a lot here to go on.  The problem with
what you are trying to do is it's bad programming principles.

public class Journal {
   public ArraryList accountNameList;
   //...

}

Now anyone can use "accountNameList."  I hope you see though that
allowing access by anyone to your internal variables is a bad idea.

public class Ledger {
   public static void main( String... args ) {
     Journal j = new Journal();
     System.out.println( j.accountNameList );
   }

}

This should work fine, baring any typoes made by me.

However as I said it's not good practice.

So for your code snippet (repeated here):

 > public ArrayList passArrayList ()
 > {
 > }

Try something like this:

public class Journal {
   public ArraryList accountNameList;
   //...
   public ArrayList getArrayList() {  // changed name
     return accountNameList;  // or accountNameList.clone();

}

But this is little better than just allowing public access to your
internal variable.  The best way to do this is just to re-implement the
methods you need in terms of the underlying ArrayList

public class Journal {
   public ArraryList accountNameList;
   //...
   public boolean add( Object o ) {
     return accountNameList.add( o );
   }
   public Object get( int i ) {
     return accountNameList.get(i);
   }
   public int size() {
     return accountNameList.size();
   }

}

Stuff like that is much better encapsulated than actually allowing
access to an internal variable.


The original question was how to operate on the list from another
class.

public class Ledger
{
public void operateOn( List<?> stuff )
{
// do whatever you want to 'stuff'
}
}

justineeee: I suggest that you review the tutorials at java.sun.com.
Passing variables around between methods is a very fundamental
technique.
 
J

justineee

I tried putting this to the ledger class

public ArrayList<AccountingClass> getList()
{
Journalizing j = new Journalizing();
return j.accountNameList;
}

No errors or whatever.

However, when I tried the program.. I put some values in the ArrayList
(accountNameList) then hit the Back button (went to homepageGui)
then hit the Ledger button (went to ledgerGUI) then I tested if it
works with this..

if (e.getSource()==goButton)
{
textBox.setText(""+getList().get(0));
}

after I hit the goButton NullPointerException error comes up.. I think
that when I go out of the Journalizing GUI the values of the ArrayList
disappear that's why I can't use it to the Ledger Gui?

Help. Thanks.
 
L

Lew

justineee said:
I tried putting this to the ledger class

public ArrayList<AccountingClass> getList()
{
Journalizing j = new Journalizing();
return j.accountNameList;
}

No errors or whatever.

However, there is no clue here as to how the 'Journalizing#accountNameList' is
initialized. Absent an SSCCE, I can only guess that it isn't, ergo
'accountNameList' is 'null'.

You should not be returning 'accountNameList' directly anyway. Either use a
getter (accessor) method, or turn the control around and have 'Journalizing'
call a method in another class, perhaps a setter (mutator) that sets the list
in an instance of that other class.
However, when I tried the program.. I put some values in the ArrayList
(accountNameList) then hit the Back button (went to homepageGui)

How are you putting values in that list? All you show is creating a brand new
instance of 'Journalizing' and returning its presumably 'public' variable.

SSCCE. SSCCE. SSCCE.
then hit the Ledger button (went to ledgerGUI) then I tested if it
works with this..

if (e.getSource()==goButton)
{
textBox.setText(""+getList().get(0));
}

If, as I guessed, you don't put values into the list inside the constructor of
'Journalizing', which you shouldn't do anyway, then 'accountNameList' is
'null' when you return it from 'getList()'. Hence the NPE.
after I hit the goButton NullPointerException error comes up.. I think
that when I go out of the Journalizing GUI the values of the ArrayList
disappear that's why I can't use it to the Ledger Gui?

No, they never "appear" in the first place. You haven't shown us how the
constructor sets those values.

<http://sscce.org/>
<http://sscce.org/>
<http://sscce.org/>
 
J

justineee

Here is how I put the values inside the accountNameList

String getDebit = inputDebit.getText();

if ((!(getDebit.equals(""))) && e.getSource()==debitButton)
{
addAccountNameDebit(getDebit);
}

public void addDebitAmount(String word, int amount,
ArrayList<AccountingClass> list)
{
int ctr = 0;
boolean found = false;

while (ctr < list.size() && !found)
{
if (list.get(ctr).getAccountName().equalsIgnoreCase(word))
{
found = true;
list.get(ctr).addDebitAmount(amount);
}
ctr++;
}
}

I hope I showed the right code now. Thanks.
 
L

Lew

Here is how I put the values inside the accountNameList

String getDebit = inputDebit.getText();

if ((!(getDebit.equals(""))) && e.getSource()==debitButton)
      {
         addAccountNameDebit(getDebit);
      }

public void addDebitAmount(String word, int amount,
ArrayList<AccountingClass> list)
   {
      int ctr = 0;
      boolean found = false;

      while (ctr < list.size() && !found)
      {
         if (list.get(ctr).getAccountName().equalsIgnoreCase(word))
         {
            found = true;
            list.get(ctr).addDebitAmount(amount);
         }
         ctr++;
      }
   }

I hope I showed the right code now. Thanks.

You have not.

You don't show the definition for 'inputDebit'. You don't show the
definition for 'e'. You don't show the definition for
'addAccountNameDebit()'. You don't show the definition for
'AccountingClass'. (BTW, don't include "Class" in a class name.) You
don't show the definition for 'addDebitAmount(int amount)' in
'AccountingClass'. What you do show is incomplete and does not
compile. Most egregiously, you don't show the one thing I asked for
explicitly, where in the constructor of 'Journalizing' the value of
'accountNameList' is set to a non-'null' value.

You need to provide an SSCCE. That is explained fully in the link
provided in triplicate above, and repeated here. You have
consistently omitted the code where the problem resides.

Again, the problem is that you create a new instance of 'Journalizing'
leaving the value of 'accountNameList' as 'null', then try to
dereference the 'null' pointer.

<http://sscce.org/>
<http://sscce.org/>
<http://sscce.org/>
 
R

Roedy Green

I have an arraylist named accountNameList in the Journal.class, then I
have another class named Ledger.class.. Can I still read the values of
the arraylist in the Journal.class and do the same things I can do
with the ArrayList? like list.get(0), list.size() and other methods
that I made in other classes? Or display the values of the ArrayList
in a TextArea in the Ledger.class?

You can make the reference to it have package scope, then other
classes in the same package can access it.

You can create a access method in the class to give the refe9rence to
other classes.

You can provide a facade, exposes just some of the arrayList methods
to the outside,

you can pass the reference around as a parameter to whomever needs it.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Climate change is no longer a doomsday prophecy, it’s a reality."
~ Astrid Heiberg president of the International Federation of Red Cross and Red Crescent Societies
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top