I can''t understand this error
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...
Code java:
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 :)
Re: I can''t understand this error
When posting code, make sure you use the CODE tags. And make sure you boil your code down to the smallest form possible, an SSCCE (that's a link, click it). Point out any line numbers referenced in the Exception's stack trace so we don't have to count them ourselves.
Re: I can''t understand this error
Please use [highlight=java]your java code here[*/highlight] while posting your code.
Remove the * from [*/highlight] while posting the code actually.
That would help us understand it better.
Goldest
Re: I can''t understand this error
See: Common Java Mistakes: Forgetting to initialize a variable
Step through your code in debug mode to find out which variable didn't get initialized (or didn't get initialized to be non-null).
Re: I can''t understand this error
I've never debugged my code before and on eclipse it looks rather confusing
Re: I can''t understand this error
Hmmmm..how about this:
Code java:
public class Student {
private String studentName;
public Student(String studentName)
{
this.studentName = studentName;
Setname(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());
}
}
Code java:
public class Group
{
private String studentGroup;
public Group(String studentGroup)
{
this.studentGroup = studentGroup;
Setgroup(studentGroup);
}
public void Setgroup(String name){
studentGroup=name;
}
public String getGroup() {
return studentGroup;
}
public void groupsaying(){
System.out.printf(" from %s", getGroup());
}
}
Also, it'd be best if you had Student and Group in two separate java files.
Re: I can''t understand this error
Quote:
Originally Posted by
javapenguin
Hmmmm..how about this:
Code java:
public class Student {
private String studentName;
public Student(String studentName)
{
this.studentName = studentName;
Setname(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());
}
}
Code java:
public class Group
{
private String studentGroup;
public Group(String studentGroup)
{
this.studentGroup = studentGroup;
Setgroup(studentGroup);
}
public void Setgroup(String name){
studentGroup=name;
}
public String getGroup() {
return studentGroup;
}
public void groupsaying(){
System.out.printf(" from %s", getGroup());
}
}
Also, it'd be best if you had Student and Group in two separate java files.
cheers I'll give that a go, and they are separate files :)
Re: I can''t understand this error
you declared quiz to be 9 elements
Code :
SimpleQuestion[] quiz = new SimpleQuestion[9];
but you only have 3 quizzes that have values. That's why you have the error. either initialize your quiz array to have values, or declare just enough size for your quiz
Re: I can''t understand this error
Gracious :) Error is now gone to never never land :)
But I also stumbled upon another problem, the Mutliplequestion
Code java:
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;
doesn't seem to work, well it runs but even if you input the correct answer it returns false, and I've taken out the if statement but that doesn't seem to make any difference.
class code
Code java:
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));
}
}
}