• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Global Array not working

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my posts never seen to get answered! lol, is there anyone online that can help???
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, i dont know how to thank you! that has sorted it and it is producing results im happy. thank you so much! x
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ==.
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Embla:

You should program against interfaces rather than implementations:

John.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic