Issues Initialzing and Accessing Integer Array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Issues Initialzing and Accessing Integer Array

  1. #1
    Join Date
    Feb 2005
    Posts
    24

    Question Issues Initialzing and Accessing Integer Array

    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).

    Code:
    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
        onfArray[0]++;	// THEN OF COURSE THIS FAILS
     }
    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,
    Last edited by Jamie; 04-07-2005 at 05:09 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Dublin, Ireland
    Posts
    63
    In your code you are declaring confArray as a public static variable, but are not instantiating/initialising it. In your main() method you are creating another array, with the same name but this time private. So when you try to access confArray from the StartConfs() method its throwing a NullPointerException, because the array you're accessing is in fact Null.

    Obviously I'm a VB developer, so this might not be 100%, but try this
    Code:
    public class Main {
    	public static int[] confArray;
    	public static void main(String[] args) {
    		confArray[] = new int[10];
    		StartConfs();
    	}
    	public static void StartConfs() {
    		confArray[0] = 0;
    		confArray[0]++;
    	}
    
    	// other stuff
    }
    VBForums.com & DevX.com Super Moderator
    Microsoft MVP : Visual Developer - Visual Basic
    My Website

  3. #3
    Join Date
    Feb 2005
    Posts
    24
    Well I have the line
    "int confArray[] = new int[10];"
    As one of the 1st lines in my MAIN (as seen in my [CODE] section).
    Is there really that much of a difference between
    int confArray[] = new int[10];
    and your suggestion
    confArray[] = new int[10];

  4. #4
    Join Date
    Aug 2004
    Location
    Dublin, Ireland
    Posts
    63
    Yes. By prefixing confArray[] with int you are declaring a new array called confArray[] but under a different scope. Remember how variables are declared in Java, the type followed by the name.
    VBForums.com & DevX.com Super Moderator
    Microsoft MVP : Visual Developer - Visual Basic
    My Website

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center