*
The moose likes Beginning Java and the fly likes Global array is null after its been filled Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Global array is null after its been filled" Watch "Global array is null after its been filled" New topic
Author

Global array is null after its been filled

Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Apr 18, 2010
Posts: 20
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
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
That isn't global; that is a field. Where are the two arrays instantiated? Show us the code for your constructor.
Andy Powell
Greenhorn

Joined: Apr 18, 2010
Posts: 20
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

Joined: Apr 18, 2010
Posts: 20
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
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
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

Joined: Apr 18, 2010
Posts: 20
At the bottom of my first post I have this,


does that not create the arrays?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
That might, but it is likely that is after you are trying to use the arrays.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Global array is null after its been filled
 
Similar Threads
ArrayList of ArrayList<String>
problem adding Arrays to an ArrayList in a loop
ArrayList added in to a map behaves in strange manner
passing value from jcombobox to other classes?
array confusion