The Artima Developer Community
Sponsored Link

Java Answers Forum
Issues Initialzing and Accessing Integer Array

1 reply on 1 page. Most recent reply: Apr 7, 2005 1:33 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Issues Initialzing and Accessing Integer Array Posted: Apr 7, 2005 12:33 AM
Reply to this message Reply
Advertisement
Coding Style: NetBeans IDE 4.0 Beta2 (Java)

Given an Integer array (global/static) defined within my Class scope, I want to be able to use the integer array from within any function in the class itself.
However I seem to be having issues accessing the array, what I do is I Initiallize (define the size) the array 1st thing in my main, then later in another function (StartConf) I want to use the array.
My goal is to increment a value in the array when this function is run (for a specific index).


public class Main {

public static int[] confArray;

public static void main(String[] args) {
// Each Indexed Integer corresponds to the amount of users in that channel/topic
int confArray[] = new int[10];
// I need to INITIALIZE the array = 0 here, currently I can only do it manually confArray[1]=0, etc...
.. DO SOME STUFF ..
StartConfs();
}

public static void StartConfs() {
confArray[0] = 0; // THIS FAILS, nullexceptionpointer
confArray[0]++; // THEN OF COURSE THIS FAILS
}
[Code]

So I have 2 major issues:
1- I need to Initialize my int[] = 0 (so I am sure my counters start at 0)
2- I need to be able to access it from inside the StartConf function

Currently if I debug through the code I can see that in my main the int[] is created (still not 100% on the best way to initialize the entire thing = 0)
However my StartConf function sees it as a NULL int[] (as if it was not created) and generates errors like "nullpointerexception" because the int[] is null (but I initialized it in my main)
I am trying to use this as a "global variable", am I doing something wrong?
Any help would be appreciated, Thanks,


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Issues Initialzing and Accessing Integer Array Posted: Apr 7, 2005 1:33 AM
Reply to this message Reply
Your problem is that you use 2 different arrays with the same name.


public class Main {
  public static int[] confArray;
 
  public static void main(String[] args) {
    int confArray[] = new int[10]; //this variable overrides  the variable in the class
 
    //.. DO SOME STUFF ..
    StartConfs();
  }
 
public static void StartConfs() {
  confArray[0]++; //confArray was never initialized.
}

Change the line in the main method to:
confArray = new int[10];

All numerical variables in an array are 0 by default.
You could allways do this:
for (int i = 0; i < confArray; i++)
  confArray[i] = 0;

or (starting with Java 1.5):
for (int num : confArray)
  num = 0;
I'm nut sure if it will work, since int is a primitive type and I don't know if num would only be a copy of the array content.

Flat View: This topic has 1 reply on 1 page
Topic: Why Object class has wait(), notify() & notifyall() method Previous Topic   Next Topic Topic: Problem Creating Executable JAR

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use