Martin
Martin

Reputation: 277

Why is the object that I created staying at null?

I've written a simulation in Java, but hit some problems when running. The custom classes I created to simulate aspects of the model seem to never get initialized. For example,

public class Queue {
public static int front = -1;
public static int length = 0;
public static int capacity = 0;
public static int roadqueue[];
public static int position[];
public static boolean full = false;

public static void push(int pushValue) {

and then the functions that can be run from the class.

In my main project, I declare the class as follows:

public class myProject {

public static Queue queue[] = new Queue[33];

and then in the main function, I call a function first thing that is supposed to initialize values in this class:

public static void main(String[] args) {
initQueue();

with

 public static void initQueue() {
queue[0].length = 6000;
queue[14].length = 6000;
queue[1].length = 20;
queue[13].length = 20;
queue[3].length = 10;
queue[11].length = 10;
queue[4].length = 360;
queue[12].length = 360;
queue[5].length = 260;
queue[10].length = 260;
queue[6].length = 20;
queue[9].length = 400;
queue[15].length = 460;
queue[22].length = 460;
queue[16].length = 260;
queue[21].length = 260;
queue[17].length = 100;
queue[26].length = 100;
queue[18].length = 160;
queue[20].length = 140;
queue[23].length = 240;
queue[25].length = 140;
queue[27].length = 80;
queue[32].length = 80;
queue[28].length = 480;
queue[31].length = 140;

for (int i = 0; i < 33; i++) {
    if (i == 3 || i == 12)
    queue[i].capacity = 40;
    else
    queue[i].capacity = queue[i].length / 10;
    queue[i].position = new int[queue[i].capacity];
    queue[i].roadqueue = new int[queue[i].capacity];
}
}

However, when I run the project and insert breakpoints, the for loops start iterating, but Netbeans informs me that, for instance at

queue[0].length = 6000;

"length references a null object", and in the variable explorer all the values in queue[] remain null throughout the program. Any idea what I'm doing wrong?

Upvotes: 0

Views: 430

Answers (5)

T.J. Crowder
T.J. Crowder

Reputation: 1074475

This line:

public static Queue queue[] = new Queue[33];

...just creates the array. You still have to create the entries in the array, e.g.:

queue[0] = new Queue();

This distinction makes particular sense if you think in terms of why you have an array in the first place: Each of the objects in it may have different state. So using an abstract example:

// Create a Person array
Person[] people = new Person[3];

// Add Joe, Mohammed, and Maria to it
people[0] = new Person("Joe");
people[1] = new Person("Mohammed");
people[2] = new Person("Maria");

(I've used explicit indexes there, but typically you'd loop through and use the loop variable.)

But note the very important thing Jon Skeet picked up on in the comments: Your Queue class's members are all static, so there's only one one length (for instance) for the entire class. Because of that, and because of a quirk of Java, this line:

queue[0].length = 6000;

actually does this:

Queue.length = 6000;

You want to make those members instance members (remove the static from them), initialize them to relevant values in a constructor, and initialize the entries in the array (the queue[x] = new Queue(); above).

Upvotes: 2

async
async

Reputation: 1537

All elements of an array are automatically initialized to their default value (in your case, null). You have an array of 33 null Queues.

As others suggested, you need to assign an instance of Queue to your elements before using them

queue[i] = new Queue();
//now you can use queue[i]

LE: Btw, I doubt you want those fields in Queue to be static. You're creating an array of Queues that all have the same fields. You may want to remove the static keyword from those fields (this may include also removing static from some of your Queue methods).

Upvotes: 4

lbremen
lbremen

Reputation: 53

i think ur missing, taht u only got onr Queue object. U need to have one on every position of the Array.

like

queue[1] = new Queue();

Upvotes: 0

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

Queue queue[] = new Queue[33]; Your array is an Queue type and it is an Object. The moment you initialize your array. All element will assign to it's default value, in this case it is null since default value of an Object is null.

Upvotes: 0

Tim B
Tim B

Reputation: 41188

When you create an Array in Java it allocates the array itself - but everything inside the Array is null. Essentially you create a street with plots for lots of houses but don't build any houses.

You need to go through building houses in each of the plots before you can do anything with the houses.

So loop through the queue, creating your Queue objects.

for (int i=0;i<queue.length;i++) {
    queue[i] = new Queue();
}

Upvotes: 1

Related Questions