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){}
}
}
}
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){}
}
}
}