Summary: Can't figure out why I'm getting this Null Pointer Exception error.
I am new to Processing and to programming in general. I am trying to model a concept in nonlinearity in statistics. The concept is vicious cycles in success and failure, where one's chance for success in a win or lose event are higher if they were successful in the last event. The math is simple; one's chances for winning in the next event are 75% if they won in the last event, otherwise it is 25%.
I've built this into a class so I can visualize it. Basically, an ellipse moves at a constant speed from left to right, incrementing vertically up 5 pixels for each success and down for each failure. I want to make this work for one object, then add multiple objects to see what it looks like.
The logic is simple. I created two arrays of 100 outcomes, one with 75 wins ( 1's) and 25 losses (0's) to reflect the probability of winning after a win, and the other for the probability of winning after a lose. Then depending on whether the last event was a win or lose, I pick the appropriate array and randomly select an outcome to determine the result of the current event.
In trying to reflect this is basic vector movement logic, I am getting a Null Pointer Exception error, with the player.update() method in draw being highlighted.
Code:Player player;
void Setup() {
size(500,500);
background(50);
player = new Player();
}
void draw() {
noStroke();
player.update();
player.checkEdges();
player.display();
}
class Player {
PVector location;
PVector velocity;
Player() {
location = new PVector(0,height/2);
velocity = new PVector(1,0);
}
void update() {
/*create two sets of outcomes, one if the last event was a win, one for it was a loss*/
int[] winProbOutcomes = new int[100];
int[] loseProbOutcomes = new int[100];
for (int i = 0; i < 100; i++) {
if(i<=75) {
winProbOutcomes[i] = 1;
}else{
winProbOutcomes[i]=0;
}
}
for (int j = 0; j< 100; j++) {
if (j<=25) {
loseProbOutcomes[j] = 1;
}else{
loseProbOutcomes[j] = 0;
}
}
//start from a random outcome for first event
float lastEvent = random(2);
//get an outcome from the set based on lastEvent
int outcome;
if(lastEvent == 1) {
outcome = winProbOutcomes[int(random(100))];
}else{
outcome = loseProbOutcomes[int(random(100))];
}
//convert to vertical increment
int increment;
if (outcome == 1) {
increment = 5;
}else{
increment = -5;
}
//set velocity.y to increment
velocity.y = increment;
location.add(velocity);
}
void display() {
stroke(0);
fill(175);
ellipse(location.x,location.y,8,8);
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
}
}