The code works fine, but at the end of the quiz, I get an error for some unknown reason and any help pinpointing why I have this error would be much appreciated.
The code...
public class Student { private String studentName; public void Setname(String name){ studentName=name; } public String getName() { return studentName; } public void saying(){ System.out.printf("Your test starts now, you have 5 minutes, you may begin %s", getName()); } } public class Group { private String studentGroup; public void Setgroup(String name){ studentGroup=name; } public String getGroup() { return studentGroup; } public void groupsaying(){ System.out.printf(" from %s", getGroup()); } } // basic question public class SimpleQuestion { private String text; private String answer; public SimpleQuestion(String questionText) { text = questionText; answer = ""; } // sets the answer public void setAnswer(String correctResponce) { answer = correctResponce; } // checks given responce public boolean checkAnswer(String responce) { return responce.equals(answer); } // displays the question public void display() { System.out.println(text); } } import java.util.ArrayList; // Multiple choice question class public class MultipleQuestion extends SimpleQuestion { private ArrayList<String> choices; public MultipleQuestion(String questionText) { super(questionText); choices = new ArrayList<String>(); } // correct answer public void addChoice (String choice, boolean correct) { choices.add(choice); if (correct) { //Convert choice.size to string String choiceString = "" + choices.size(); setAnswer(choiceString); } } public void display() { //displays the text for the question super.display(); // Display the answer choices for (int i = 0; i < choices.size(); i++) { int choiceNumber = i + 1; System.out.println(choiceNumber + ": " +choices.get(i)); } } } import java.util.Scanner; class quizDemon{ public static void main(String[] args) { int count = 0; Scanner input = new Scanner(System.in); Scanner inputgroup = new Scanner(System.in); Student studentObject = new Student(); Group groupObject = new Group(); System.out.println("\nEnter your name here : "); String studentnamesave = input.nextLine(); System.out.println("\nEnter your group here : "); String groupnamesave = inputgroup.nextLine(); studentObject.Setname(studentnamesave); groupObject.Setgroup(groupnamesave); studentObject.saying(); groupObject.groupsaying(); SimpleQuestion[] quiz = new SimpleQuestion[9]; quiz[0] = new SimpleQuestion ("\nWho sang Stairway to Heaven"); quiz[0].setAnswer("Led Zepplin"); MultipleQuestion question = new MultipleQuestion( "Which of the following are Car manufactures"); question.addChoice("Saab", true); if(true){ ++count; } question.addChoice("R8", false); question.addChoice("Accord",false); question.addChoice("Weetos", false); question.addChoice("FF10", false); quiz[1] = question; MultipleQuestion question2 = new MultipleQuestion( "Which one of the following is a programming language"); question2.addChoice("Diamond", false); question2.addChoice("C++", true); quiz[2] = question2; Scanner in = new Scanner(System.in); for (SimpleQuestion q : quiz) { q.display(); System.out.print("Please insert your answer here: "); String responce = in.nextLine(); System.out.println(q.checkAnswer(responce)); System.out.println("Your current score is"); System.out.println(count); } } }
//
The error message
Exception in thread "main" java.lang.NullPointerException
at quizDemon.main(QuizTest.java:54)
Thanks again