aspose file tools*
The moose likes Beginning Java and the fly likes Global Array not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Global Array not working" Watch "Global Array not working" New topic
Author

Global Array not working

Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
Hey all, im having a little trouble. basically im reading an array of integers from a file and putting it into a linked list, then from the linked list im transfering it to an array using the .toArray() method of the arrayList methods. the array is filled with the data, but when i call another method to display the array it is coming up with a null pointer error. ive used System.out.println to test it and the array is of the correct size, and with the correct data in it in the method that reads the data into it, but outside of it the array is "empty" even though it is a global array?! it compiles absolutly fine but with a unsafe operation warning but that is not the issue. my code is below! much appriciated. Jeff.



~ Jeff Yan ~
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
my posts never seen to get answered! lol, is there anyone online that can help???
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Jeff Yan wrote:my posts never seen to get answered! lol, is there anyone online that can help???


It's because in displayArray() you're displaying a global int array that's declared like this,

int data[];

It has never beein intialized with an actual array object. If you do this,

int data[] = new int[0];

you'll get rid of the null pointer exception but there's will be nothing to print of course.
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
right i have done that, but why isnt the data inside the array from the readFile method? that is what im more confused about! if it is a global array then it should read the data no matter what method it is? could it be something to do with line 64? i need the data to be displayed after i have inputted it. that is why i have created the method.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Jeff Yan wrote:but why isnt the data inside the array from the readFile method?


It's because this local array called data you declare in readFile,

Object data[] = tempData .toArray();

is not the same as the global array called data.

You need to transfer information from the local data array to the global data array.
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
right, im with you so far, but how do i do that, would i use a for loop using data.length and then this.data[i] or something like that or is there a predefined method?!
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Jeff Yan wrote:right, im with you so far, but how do i do that, would i use a for loop using data.length and then this.data[i] or something like that or is there a predefined method?!


The most straightforward would be to use generics and change the global declarations to,

ArrayList<Integer> tempData = new ArrayList<Integer>();
Integer data[];

Then to transfer information from the ArrayList to the array you would do,

data = tempData.toArray(new Integer[0]);
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Or if you find the creation of the dummy Integer array revolting you can do this instead,

data = tempData.toArray(new Integer[tempData.size()]);
Jeff Yan
Ranch Hand

Joined: Nov 05, 2009
Posts: 42
wow, i dont know how to thank you! that has sorted it and it is producing results im happy. thank you so much! x
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Jeff Yan wrote:my posts never seen to get answered! lol, is there anyone online that can help???
Please! You got a reply in about half an hour. Have a look at this FAQ.

Please avoid very long lines in code; they make the posts harder to read. I had to insert some line breaks, so some line numbers in your posts will have changed.

Avoid writing == true and == false and similar. You can simply say if (boo) . . . or if (!boo) . . . You can occasionally get nasty errors if you write = instead of ==.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Embla:

You should program against interfaces rather than implementations:

John.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
John de Michele wrote:
You should program against interfaces rather than implementations:


Well, if you write code using List variables you must be careful to make sure that it will work equally well regardless of which actual object you plug in. Say you make heavy use of the random accesses available in List, then your code will work perfectly fine with an ArrayList but it will be a disaster with a LinkedList.

So in general you should write against an interface but not against any interface. You must select an interface at the right abstraction level. And sometimes it's actually better to use a concrete implementation if it's a perfect fit for the particular algoritm you're implementing.

But I agree that in this case there's probably no reason for not using List. It's just that I've seen so many cases when List has been used where it shouldn't so I'm a little reluctant to suggest it.

By the way why didn't you suggest the Collection interface rather than List?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Well, if you write code using List variables you must be careful to make sure that it will work equally well regardless of which actual object you plug in. Say you make heavy use of the random accesses available in List, then your code will work perfectly fine with an ArrayList but it will be a disaster with a LinkedList.


Really? I'm curious; what would be a case where using a LinkedList would be a disaster? And what do you mean by disaster: performance? The program actually generates an error? I've never encountered a case where one implementation would be the totally wrong choice, so I'm genuinely interested to know.

By the way why didn't you suggest the Collection interface rather than List?


Because you used ArrayList, nothing more.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global Array not working