Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I can''t understand this error

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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...

    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
    Last edited by ragingdemon; January 6th, 2011 at 04:05 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,534
    My Mood
    Hungover
    Thanks
    145
    Thanked 647 Times in 552 Posts

    Default 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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink 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
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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).

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can''t understand this error

    I've never debugged my code before and on eclipse it looks rather confusing

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: I can''t understand this error

    Hmmmm..how about this:
    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());
        }
    }

    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.
    Last edited by javapenguin; January 6th, 2011 at 06:10 PM.

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I can''t understand this error

    Quote Originally Posted by javapenguin View Post
    Hmmmm..how about this:
    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());
        }
    }

    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

  8. #8
    Member
    Join Date
    Dec 2010
    Posts
    46
    Thanks
    0
    Thanked 10 Times in 10 Posts

    Default Re: I can''t understand this error

    you declared quiz to be 9 elements
    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

  9. #9
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile 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

            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

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

Similar Threads

  1. 2 errors I can't understand
    By Brock in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 27th, 2010, 12:56 AM
  2. Grid Computing, need to understand and learn
    By madcrazyboys in forum The Cafe
    Replies: 1
    Last Post: December 21st, 2010, 11:32 PM
  3. Homework help, don't understand teach's hint, need some direction
    By crazed8s in forum Java Theory & Questions
    Replies: 2
    Last Post: December 8th, 2010, 04:56 PM
  4. Dont understand what to write.. :/
    By johnwalker in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 09:31 PM
  5. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 04:10 PM