• 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 is null after its been filled

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Hopefully this is just something simple that I'm missing...

I've got a JComboBox that another one depends on to fill its choices.
I'm filling the latter by checking the 9th character in a part number and if the 9th character is the same as the one selected (in the first combo box) it grabs them from an array containing all of them and then puts only the ones that are the same into another String array.

My problem is that after I've populated an ArrayList in the method checkCompatability it then puts that in a global String array.

Whenever I try to use that string array it is null even after being populated.

I hope I've explained that clearly but hopefully you can see from the below code.



Thank in advance everyone
This is doing my head in
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which array is null? You have several. Where have you got a "global" array; "global" is a term usually associated with C and similar languages rather than object-oriented programming?
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Both tempListPrice and tempListColour are null.

I say they are global because they are defined at the of top the class and should be available for use by any method within that class, unless I am mistaken.

forkList.addItem in the code from my previous post is adding the different Strings to the comboBox. While this is happening, is there any reason that actionPerformed should be called?
Because I've been playing around with it and this...



which is in actionPerformed, keeps coming up from within the loop in checkCompatability().
forkList is the JComboBox that I am trying to populate.

I think the problem is that whenever I try to do something within the IF statement above using either arrays (tempListPrice or tempListColour), because during the loop its going in there for some reason it would be null so thats why its returning a null pointer exception.

I hope you understand what I mean. I'm not sure I do. heh.

I really don't understand why it goes in actionPerformed...

Thanks again
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't global; that is a field. Where are the two arrays instantiated? Show us the code for your constructor.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the code for the constructor

This is how I'm creating the arrays though
This is before the constructor, still in the class



 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done it!
There really wasn't a problem filling the array at all.

I didn't realise that adding something to a JComboBox counts as an event and calls the actionEvent stuff so I have managed to sort it.

Thanks for your help.

Also, i'm going to read up on the fields that you said about before. I though they were global variables like C++ but thanks for pointing it out.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not creating the arrays at all. You are only declaring a variable. Unless you put that variable to the left of an assignment operator, and something real on the right, those fields will remain null for ever.
 
Andy Powell
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the bottom of my first post I have this,


does that not create the arrays?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That might, but it is likely that is after you are trying to use the arrays.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic