Determine size of array at right time
privatestaticint total;
static String pAsString;
static String[] pArray =new String[total];
For an array that has to be of size total, I am getting nullpointerexceptions as total when initialized is zero and Im adding more than zero Strings to the array.
Is there a way (considering the array is an attribute), that I can get total in at the right time i.e. run the method which updates total, and then set the size of the array.
Cheers
[675 byte] By [
John4938a] at [2007-11-26 18:01:29]

Or have a static init method, like
private static int total;
static String pAsString;
static String[] pArray;
//
static void init(int tot) {
total = tot;
pArray = new String[total];
}
And, as corlettk suggests, if you explain what you want to accomplish you may get better suggestions.
I did try that. I tried to assign a number to total although total has to equal:
listOfJobs.getLength()
Which is in a method obviously.
I need to set array values once size has been determined in method (main).
Its a difficult one to explain.
Ill try that idea of yours.
Cheers
Message was edited by:
John4938
Well ive tried your idea and now the array is of the correct size at the right time which is where I was before I made the array an attribute.
This is probably the best way infact although now I have a logic error I think i.e. only the last value found is added to the array for some reason:
Cheers
This is a shortened version of what I am trying to do:
private static int totalJobs;
static String priorityAsString;
static String[] priorityArray;
...
public static void main(String[] args)
{
...
for(int s = 0; s < listOfJobs.getLength(); s++)
{
Node firstJobNode = listOfJobs.item(s);
if(firstJobNode.getNodeType() == Node.ELEMENT_NODE)
{
Element firstJobElement = (Element)firstJobNode;
NodeList priorityList = firstJobElement.getElementsByTagName("priority");
Element priorityElement = (Element)priorityList.item(0);
NodeList textPriorityList = priorityElement.getChildNodes();
priorityAsString = ((Node)textPriorityList.item(0)).getNodeValue().trim();
System.out.println("Priority: " + priorityAsString);
priorityArray = new String[totalJobs];
priorityArray[s] = priorityAsString;
}
}
for(int a = 0; a < totalJobs; a++)
{
System.out.println(priorityArray[a]);
}
}
> This is probably the best way infact although now I
> have a logic error I think i.e. only the last value
> found is added to the array for some reason:
>
>priorityArray = new String[totalJobs];
The above line should be placed outside the s for-loop otherwise a new empty priorityArray will be allocated in each iteration of the loop.