• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1026
  • Last Modified:

Java arrays through classes (getter and setter)

Ok, so I cannot figure out getter and setter methods for arrays. When I use the getter methods I always just get an array of nulls. If you can help me with this I will get you points right away, I just need help! (I attached the files so you gurus can figure this out fast?)

So I get a null pointer exception running this method:

      public void printList(){
            two newOne = new two();
            int[] toPrint = newOne.getList();
            int i = 0;
            while(i<toPrint.length){
                  System.out.print(toPrint[i]+", ");
                  i+=1;
            }
      }

If you notice, it calls the getList() function from the two class -- here is the getList function:

      public int[] getList(){
            
            return first.get();
      }

If you notice, this calls the get() function on first, first is a pointer to an instance of the three class. here is the three class:

public class three {

      private int[] list;
      
      public three(){
            int[] anArray;
            anArray = new int[3];
            anArray[0] = 1;
            anArray[1] = 2;
            anArray[2] = 3;
      }
      
      public three(int[] listie){
            list = listie;
      }
      public int[] get(){
            return list;
      }
      
}

I just cannot figure out how to successfully move arrays around through different classes. Any help would be greatly appreciated. I've attached all 4 files. This is just an expiriment cause I can't get this same thing working in a larger program I'm writing.
mainGuy.txt
one.txt
two.txt
three.txt
0
jeffiepoo
Asked:
jeffiepoo
  • 2
2 Solutions
 
Kevin CrossChief Technology OfficerCommented:
From what I can see here without reviewing the files, you have a constructor where you define a new int[] and set initial values; however, you never assign this to the private variable list, so when you return this from get() method it is NULL.
0
 
Kevin CrossChief Technology OfficerCommented:

public three(){
            list = new int[3];
            for (int i=0; i<list.length; i++) {
                list[i] = i+1;
            }
      }

Open in new window

0
 
hazgodukCommented:
You can avoid doing the code in the constructor by doing the following
public class two
{
    private int[] list = {1,2};
      
    public two()
    {
    }
      
    public two(int[] list)
    {
        this.list = list;
    }
 
    public int[] get()
    {
        return list;
    }
}
 
public class three
{
    private int[] list = {1,2,3};
      
    public three()
    {
    }
      
    public three(int[] list)
    {
        this.list = list;
    }
 
    public int[] get()
    {
        return list;
    }
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now