-2

I'm new to Java and I'm implementing a class (CenterTable) that contains a nested class (CenterData). Inside the enclosing class, I want to create an array of type CenterData. The code can be seen below:

public class CenterTable {

public class CenterData {
        public int userId; 
        public double distance; 
        public double elevation; 
        public int point_00; 
        public int point_01; 
        public int point_10; 
        public int point_11; 

        public CenterData() {       
            userId = 0; 
            distance = 0; 
            elevation = 0; 
            point_00 = 0; 
            point_01 = 0; 
            point_10 = 0; 
            point_11 = 0; 
        }
    }   // end of CenterData class

public static CenterData[] centers = new CenterData[7064];  
public static double centerMaxDistance = 0; 
}

Whenever I try to access or set an element of the array centers:

 CenterTable.centers[1].beam_user = 1; 
 System.out.println(CenterTable.centers[1].beam_user); 

I get an error: Exception in thread "main" java.lang.NullPointerException

If I move the class CenterData out of CenterTable and into it's own java class, I don't get an issue like that. I'm kind of stuck at this point, if any one has any tips/hints that would be great.

Thanks in advanced!

Robadob
  • 5,319
  • 2
  • 23
  • 32
  • `centers` contains 7064 elements, all of them `null` http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Brian Roach Aug 08 '13 at 20:44

3 Answers3

2

You are getting NullPointerException because you are trying to access beam_user on center[1] but it refers to null as of now.

  • When an array is created, all the values are default values.
  • The default value for a reference type is null

you need to create object first

CentreTable.centers[1] = new CentreData();

For creating all objects,

for(int i = 0; i<= centers.length ; i++){

    centers[i] = new CenterData();
}

After your array elements are referring to actual objects, you can do as follows

centers[1].beam_user = 1;    
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • Can you explain to me why if I move the nested CenterData class out of CenterTable into its own java class, I don't get an issue like that? – user2665755 Aug 08 '13 at 20:40
1

You've created an array of CenterData objects, but that's just a bunch of slots that you can then fill CenterDatas into. You have to actually create them, either all at once (using a for loop) or as needed (by checking whether centers[i] == null and creating a new one if necessary).

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Can you explain to me why if I were to put CenterData into its own java class instead of having it as a nested class within CenterTable, that issue doesn't occur? – user2665755 Aug 08 '13 at 20:45
  • That issue still will occur and has nothing to do with the nested class. The issue is that creating an array just creates slots for holding references to a bunch of objects, it doesn't create objects to go inside the array, and until you assign objects to the slots, they're `null`. – chrylis -cautiouslyoptimistic- Aug 08 '13 at 20:52
  • Ah understood. I just tried playing around with it some more. It seems like if I change the fields in CenterData to static, I don't need to create an instance for each array element. Is there a reason for that? – user2665755 Aug 08 '13 at 21:05
  • If you do that, then the fields belong to the class `CenterData` itself, not to the instances (copies) of the class. You'll only ever have exactly one copy of those variables, not seven thousand of them. You need to read through a basic Java tutorial on classes and members. – chrylis -cautiouslyoptimistic- Aug 08 '13 at 21:13
  • Thanks so much for your quick replies! You've cleared up a lot for me! I've been reading through Oracle's java tutorial for a couple of hours, but haven't had too much time to sit down and read thoroughly through it as I have extremely limited amount of time to get this code working for work. But I def. will sit down and learn it when I get more time. Thank you!! – user2665755 Aug 08 '13 at 21:18
0
public static CenterData[] centers = new CenterData[7064];  

All the elements are null. You must put CenterData instances in the array.

rocketboy
  • 9,573
  • 2
  • 34
  • 36