Click to See Complete Forum and Search --> : Java- returning arrays from function


Neuromancer
10-16-2000, 12:08 AM
I have an assignment which I need to complete in a few hours from now. Although the actual logic employed in the program seems very clear to me, I find myself paralyzed by my lack of knowledge of Java (I just changed to this advanced course frm much simpler ones).

There is a function FileReader in another class which accepts a filename, opens the file, reads a set of integers stored in it as elements of an array, and then returns the array back to the main function (in the main class).

My querstion is- How do I read the returned array into an array in the main function itself?

I tried:-

int[] a;
a= objectname.FileReader("10.txt");

But I get a null-pointer error.

Any suggestions?

------------------
ICQ UIN: 42884520
Posting messages for the betterment of Humanity..

klamath
10-16-2000, 12:47 AM
The following (ugly, hackish) code works fine:

public class Test {
private static int[] foo;

public static void main(String[] args) {
foo = getArray();
for (int i = 0; i <= foo.length; ++i) {
System.out.println(i);
}
}

public static int[] getArray() {
int[] array;
if (Math.random() > 0.5) {
array = new int[] {1,2,3,4,5};
} else {
array = new int[] {1,2,3,4,5,6,7};
}
return array;
}
}


If this is how you've written the code, maybe the problem is somewhere else. Are you sure FileReader() is really returning an array properly?

------------------
- Klamath
Get my GnuPG Key Here (http://klamath.dyndns.org/mykey.asc)