"NullPointerException" Question (Array of Objects)

N

Newbie

My code is as below, however, as I'm new to java, I cant quite understand
why I'm getting a "NullPointerException" with the line marked inside the
asterisks.

Could someone tell me where I'm going wrong please?

thankyou.
----------------------------------
CODE
----------------------------------
public class MyProject
{
int r1, r2;
Tractor[] trackObj = new Tractor[50]; //create array of objects from the
external tractor class

public MyProject()
{
for (int i=0; i < 50; i++)
{
//**************************************************************************
********
trackObj.setP( Randomizer() ); //call setP(int) inside
tractor class
//**************************************************************************
********
System.out.println(i+" : "+trackObj); // println only testing
input results of setP() using the toString() method
}
}

public int Rand()
{
r1 = (int)(Math.random() * 100000);
r2 = r1 / 100;
return r2;
}
}
 
A

Anton Spaans

Tractor[] trackObj = new Tractor[50];

This statement creates an array that *can* hold 50 Tractor
object-references.When you do not initialize an array explicitlly, all the
elements in the array are undefined (null).

So, when i call trackObj[x] (for every 0<=x<50), null is returned.

Therefore trying to call the method trackObj.setP(...) will throw the
NullPointerExcption.

-- Anton.
 
N

Newbie

how do I initialize the array explicitly?

so as when I'm in the for loop I can put the random int's into the
trackObj[] array of references.....

your assistance is greatly appreciated......

thankyou
 
K

Kristian Bisgaard Lassen

Hi,

Try something like:

Tractor[] trackObj = new Tractor[50];
// and add the line
for (int i = 0; i < 50; i++) trackObj = new Tractor();
// assuming tractor takes no arguments otherwise you have to rewrite it a
// bit.

Best regards
Kristian

how do I initialize the array explicitly?

so as when I'm in the for loop I can put the random int's into the
trackObj[] array of references.....

your assistance is greatly appreciated......

thankyou


Anton Spaans said:
Tractor[] trackObj = new Tractor[50];

This statement creates an array that *can* hold 50 Tractor
object-references.When you do not initialize an array explicitlly, all the
elements in the array are undefined (null).

So, when i call trackObj[x] (for every 0<=x<50), null is returned.

Therefore trying to call the method trackObj.setP(...) will throw the
NullPointerExcption.

-- Anton.
 
N

Newbie

EXCELLENT...thankyou very much.... it works

Kristian Bisgaard Lassen said:
Hi,

Try something like:

Tractor[] trackObj = new Tractor[50];
// and add the line
for (int i = 0; i < 50; i++) trackObj = new Tractor();
// assuming tractor takes no arguments otherwise you have to rewrite it a
// bit.

Best regards
Kristian

how do I initialize the array explicitly?

so as when I'm in the for loop I can put the random int's into the
trackObj[] array of references.....

your assistance is greatly appreciated......

thankyou


Anton Spaans said:
Tractor[] trackObj = new Tractor[50];

This statement creates an array that *can* hold 50 Tractor
object-references.When you do not initialize an array explicitlly, all the
elements in the array are undefined (null).

So, when i call trackObj[x] (for every 0<=x<50), null is returned.

Therefore trying to call the method trackObj.setP(...) will throw the
NullPointerExcption.

-- Anton.

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top