Question [] questions; //array of objects for the question class
Screens sc; // object of the screen class
Answers ans;
final int stateFirst=0; //cannot be changed
final int stateGameOver=1;
final int stateq1=2;
final int stateq2=3;
final int stateq3=4;
final int stateq4=5;
final int stateq5=6;
int currentState=stateFirst;//setting the current state to statefirst so that the game starts on the first screen
int score=0;
PImage [] left;
PImage [] right;
void setup() {
size(600, 600);
left=new PImage[5]; //creating arrays of images
left[0]=loadImage("first.jpeg");
left[1]=loadImage("second.jpg");
left[2]=loadImage("third.jpg");
left[3]=loadImage("fourth.jpg");
left[4]=loadImage("fifth.jpg");
right=new PImage[5];
right[0]=loadImage("firstb.jpg");
right[1]=loadImage("secondb.jpg");
right[2]=loadImage("thirdb.jpg");
right[3]=loadImage("fourthb.jpg");
right[4]=loadImage("fifthb.jpg");
for (int i=0; i<5; i++) {
image(left[i], 50, 200, 250, 400);
image(right[i], 300, 200, 250, 400);
}
ans=new Answers(left, right);
sc=new Screens();
String [] text=loadStrings("questions.txt");//load the file
questions=new Question[text.length];
for (int i=0; i<questions.length; i++) {
String [] q=split(text[i], "?"); //split the text file whenever we hit a question mark, getting the questions by using the indices
questions[i]=new Question(q);
}
}
void draw() {
sc.showState(); //calling the show state method
for (int i=0; i<questions.length; i++) { //calling the showQuestionState() inside a for loop that runs as many times as the length of the file
showQuestionState();
}
}
void keyPressed() { //if spacebar is pressed, we move to the question screen
if (key==' ') {
currentState=stateq1;
}
}
void mousePressed() { //using mouse pressed event handler so that when we press the mouse on the questions screen inside the specific answer region, it switches to the next question.
if (mouseX>=50 && mouseX<=550 && mouseY>=200 && mouseY<=600) {
if (currentState==stateq1) {
currentState=stateq2;
} else if (currentState==stateq2) {
currentState=stateq3;
} else if (currentState==stateq3) {
currentState=stateq4;
} else if (currentState==stateq4) {
currentState=stateq5;
} else if (currentState==stateq5) {
currentState=stateGameOver;
}
}
}
void showQuestionState() { //function used to show the respective questions in order depending on what the state is
ans.answerMechanism();
if (currentState==stateq1) {
questions[0].showQuestion();
}
if (currentState==stateq2) {
questions[1].showQuestion();
}
if (currentState==stateq3) {
questions[2].showQuestion();
}
if (currentState==stateq4) {
questions[3].showQuestion();
}
if (currentState==stateq5) {
questions[4].showQuestion();
}
}
class Answers {
PImage []answer1;
PImage[] answer2;
Answers(PImage [] a1, PImage [] a2) {
answer1=a1;
answer2=a2;
}
void answerMechanism() {
if (currentState==stateq1 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
score++;
} else if (currentState==stateq2 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
score++;
} else if (currentState==stateq3 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
score++;
} else if (currentState==stateq4 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
score++;
} else if (currentState==stateq5 && mouseX>=50 && mouseX<=300 && mouseY>=200 && mouseY<=600) {
score++;
}
}
void showAnswerStructure(){
for(int i=0;i<answer1.length;i++){
answer1[i].resize(250,400);
}
for(int i=0;i<answer2.length;i++){
answer2[i].resize(250,400);
}
}
}
Hello everyone,
I ran into a problem with my project. Right after I added the images to the setup, my code won’t run and the NullPointerException error appears. Any idea what might have caused it? The code was running fine before adding the images.
Thanks in advance!