?
Solved

Java arrays through classes (getter and setter)

Posted on 2009-05-06
3
Medium Priority
?
1,008 Views
Last Modified: 2013-11-23
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
Comment
Question by:jeffiepoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24317926
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
 
LVL 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 400 total points
ID: 24317952

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

Open in new window

0
 
LVL 3

Accepted Solution

by:
hazgoduk earned 1600 total points
ID: 24337730
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question