Java array getters and setters.. help?

JoLLyRoGer

Diamond Member
Aug 24, 2000
4,153
4
81
Yes, this is homework. No I'm not asking you to do it for me.
But I'm stuck and need a nudge in the right direction.


So how do I explain what's happening and keep it short and sweet....thinking........

I have a program where I am trying to use getter method to reference (in a sense, copy) values stored in a two dimensional array from another class (well call it Class2 - which holds the original array). When I call the getter from within the main method (lets just say MainClass for simplicity), it works great and I can interact with the values from the array in "Class2".

However, when I try to call the getter method from any other class (let's just say "Class3" for example), all the values from the array return as null. Curiously enough, the new Array is the same "size" as the array I'm calling so both [row] and [column] information are getting passed in but all corresponding values are 'null'

So basically I wind up with an empty array anywhere except MainClass.

The getter method as defined in Class2 (pseudo code):

<code>

public String[ ][ ] getArray()
{
return someArray;
}
</code>

---------------------------------------
Here is how I am calling the getter in MainClass (pseudo code):

<code>


// Call the constructor for class 2
Class2 class2Stuff = new Class2();

//Copy the array from Class2
String[ ][ ] newArray = class2Stuff.getArray();

//Print some stuff from newArray
System.out.println(newArray[0][0] +" "+newArray[0][1]);

</code>

Output:
value@0,0 value@0,1


---------------------------------------
I call the getter the exact same way in Class3 and maybe change the names a little bit(pseudo code):

<code>
// Call the constructor for Class2
Class2 class2StuffForClass3 = new Class2();

//Copy the array from Class2
String[ ][ ] newClass3Array = class2StuffForClass3.getArray();

//Print some stuff from newClass3Array
System.out.println(newClass3Array[0][0] +" "+newClass3Array[0][1])
</code>

----------------------------------------

Then I call the constructor for Class3 inside the MainClass
<code>
//Call the constructor for Class3
Class3 class3Stuff = new Class3();

</code>

Output:
null null


WTF!? I have tested other variable types such as int for a primitive, and String for an object...using a similar methodology to what I have just outlined and those getters return the appropriate data irregardless of what class calls the getter method. So why are arrays returning 'null' anywhere "except" the MainClass?

Also bear in mind that all arrays an variables inside of Class2 are declared 'private' and declared before the start of any method declarations. Which it shouldn't matter.. private values can be accessed by any method within the class and I'm not having trouble exporting data types other than arrays.
 
Last edited:

JoLLyRoGer

Diamond Member
Aug 24, 2000
4,153
4
81
I'll just chime in with my own fix - took me all morning to crack this egg...

apparently in order to call the getter from any class other than the one containing the main method......
first the variable containing the value being referenced (the one's in Class2 in my case) must be declared at static.

adding this to the declaration make the whole world spin around.
 

purbeast0

No Lifer
Sep 13, 2001
53,021
5,898
126
Why don't you just post your actual code? This should be very easy to see why but you have some 1/2 pseudo code in there which makes it hard, plus it's not formatted properly.

Do you know what static actually does? If you don't really know what it is for you probably shouldn't be using it.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |