Stupid null pointer exception

T

Tony McGrath

This here is some code I have been writing as part of my final year
project in Comp Sci. It is a traffic simulator and this is a test
class to try and run a text based version of a simle straiht road.
However when i run it i only seem to get a null pointer exception the
whole time. Can anyone help. The arrray should contain objects of type
Bollox, which in this case contains informationon each cell. ie does
it contain a car or not and if so whats its speed.

Much appreciated cheers.


public class Bollox {

Cells road1[] = new Cells[200];
Cells road2[] = new Cells[200];

private class Cells{

public void Cells(){
Cells Cell = new Cells();
}
boolean isEmpty;
boolean isCar;
int velocity = 0;
int maxVelocity = 0;

public void setVelocity(int vel){
velocity = vel;
}

public void setEmpty(){
isEmpty = true;
}

public void setNotEmpty(){
isEmpty = false;
}

public void setIsCar(){
isCar = true;
}

public void setIsNotCar(){
isCar = false;
}

public boolean isEmpty(){
return isEmpty = true;
}

public boolean isCar(){
return isCar = true;
}

public int getVelocity(){
return velocity;
}

public void velocity(int i){
int x = 1;
int temp = 0;

if(isCar()){
maxVelocity = 12;
}else{
maxVelocity = 10;
}

do{
if(road1[i+x].isEmpty() && temp < maxVelocity){
temp++;
road2[i+x].setEmpty();
x++;
}else{
if(temp < velocity){
velocity = temp;
break;
}else{
int ran = (int)(Math.random()*1);
if(ran == 0 && velocity + 2 <= maxVelocity){
velocity = velocity + 2;
isEmpty = false;
road2[i+x] = road1[i+x];
break;
}else if(velocity + 1 <= maxVelocity){
velocity++;
isEmpty = false;
road2[i+x] = road1[i+x];
break;
}else{
velocity = velocity;
road2[i+x] = road1[i+x];
isEmpty = false;
break;
}
}
}
}
while (road1[i+x].isEmpty());
}

public void addVehicle(){
road1[0].setNotEmpty();
int ran = (int)(Math.random()*8);
if(ran == 3){
road1[0].setIsNotCar();
road1[0].setVelocity(7);

}else{
road1[0].setIsCar();
road1[0].setVelocity(9);
}
}

public void printOut(){
System.out.println("");
for(int i = 0; i < 200; i++){
if(road1.isEmpty()){
System.out.print(" ");
}else{
if(road1.isCar()){
System.out.print(".");
}else{
System.out.print("#");
}
}
}
}

public void swap(int i){
road1 = road2;
}
}

void run(){
for(int i = 0; i < 200; i++){
Cells cell1 = road1;
Cells cell2 = road2;
cell1.swap(i);
}
}

//void swap(){
//swap();
//}
}

class MainC extends Thread{
public static void main(String[] args){
Bollox b = new Bollox();
boolean go = true;
while(go){
b.run();
try{Thread.sleep(100);}
catch(InterruptedException e){}
}
}
}
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

Tony McGrath wrote:
<snip>

There is probably very much else wrong with this code, but this is what
I found on a cursory look:
public class Bollox {

Cells road1[] = new Cells[200];
Cells road2[] = new Cells[200];

You do know that this only allocates the arrays to hold the Cell
objects, not the objects in the array slots? To allocate the Cells
objects you must do:

for (int i = 0; i < road.length; i++)
road = new Cells();
private class Cells{

public void Cells(){
Cells Cell = new Cells();
}

What is the above supposed to do? This will result in an infinite loop.
public boolean isEmpty(){
return isEmpty = true;
}

This does not do what the name implies it does. This will always set
isEmpty to true and return true.
class MainC extends Thread{
public static void main(String[] args){
Bollox b = new Bollox();
boolean go = true;
while(go){
b.run();
try{Thread.sleep(100);}
catch(InterruptedException e){}

What is this supposed to do?
 
T

Thomas Schodt

Tony said:
Cells road1[] = new Cells[200];
Cells road2[] = new Cells[200];

You allocate two arrays that hold references to Cells objects
(initially all these references are null).

You did not create the 400 Cells objects
that you presumably want the arrays to reference


Also, there are problems in your isEmpty() and isCar() methods.
 
X

xarax

Daniel Sjöblom said:
Tony McGrath wrote:
<snip>

There is probably very much else wrong with this code, but this is what
I found on a cursory look:
public class Bollox {

Cells road1[] = new Cells[200];
Cells road2[] = new Cells[200];

You do know that this only allocates the arrays to hold the Cell
objects, not the objects in the array slots? To allocate the Cells
objects you must do:

for (int i = 0; i < road.length; i++)
road = new Cells();
private class Cells{

public void Cells(){
Cells Cell = new Cells();
}

What is the above supposed to do? This will result in an infinite loop.


Look more closely at the method. It is not a constructor.
It is just wasting time and space by creating an instance
that is never used.

The OP should review the guidelines for naming
packages, classes, methods, and fields.

This does not do what the name implies it does. This will always set
isEmpty to true and return true.

The OP is confusing "=" with "==".

Probably better to just "return isEmpty;", but I
cannot see where "isEmpty" is correctly assigned.

class MainC extends Thread{
public static void main(String[] args){
Bollox b = new Bollox();
boolean go = true;
while(go){
b.run();
try{Thread.sleep(100);}
catch(InterruptedException e){}

What is this supposed to do?

I have no clue.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top