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 4 of 4

Thread: NOOB: Array Help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default NOOB: Array Help

    After I ran DriverExamDemo, I got an error message in the command box:

    HTML Code:
    Exception in thread "main" java.lang.NullPointerException
            at DriverExam.<init>(DriverExam.java:16)
            at DriverExamDemo.main(DriverExamDemo.java:18)
    Press any key to continue . . .
    I don't know what I am doing wrong here.

    public class DriverExamDemo
    {
    	public static void main(String[] args)
    	{
    		String[] answersGiven1 = {"B","D","B","A","C",
    								"A","B","B","C","D",
    								"B","C","C","A","D",
    								"C","C","C","D","A"};
     
    		String[] answersGiven2 = {"B","D","B","A","D",
    								"A","B","B","C","D",
    								"B","C","C","A","D",
    								"E","C","C","D","A"};
     
    		DriverExam myAttempt1 = new DriverExam(answersGiven1);
    		DriverExam myAttempt2 = new DriverExam(answersGiven2);
     
    		displayResults(myAttempt1,"Attempt 1:");
    		displayResults(myAttempt2,"Attempt 2:");
    	}
     
    	public static void displayResults(DriverExam attempt, String title)
    	{
    		System.out.println("\n" + title);
    		System.out.println("Passed? " + attempt.passed());
    		System.out.println("# correct " + attempt.totalCorrect());
    		System.out.println("# missed " + attempt.totalIncorrect());
     
    		System.out.print("Questions missed: ");
     
    		for (int missedQuestion : attempt.questionsMissed())
    			System.out.printf("%2d ", missedQuestion);
     
    		System.out.println("");
    	}
    }

    public class DriverExam
    {
    	private String[] correctAnswers = {"B","D","B","A","C",
    								"A","B","B","C","D",
    								"B","C","C","A","D",
    								"C","C","C","D","A"};
     
    	private String[] studentAnswers;
     
     
    	public DriverExam(String[] newAnswers)
    	{
    		for(int i = 0; i < correctAnswers.length; i++)
    		studentAnswers[i] = newAnswers[i];
    	}
     
    	public boolean passed()
    	{
    		return (totalCorrect()>15);
    	}
     
    	public int totalCorrect()
    	{
    		int correctCount = 0;
    		for (int i = 0; i < correctAnswers.length; i++)
    		{
    			if(correctAnswers[i].equals(studentAnswers[i]))
    			correctCount++;
    		}
    		return correctCount;
    	}
     
    	public int totalIncorrect()
    	{
    		int totalMissed = 0;
    		totalMissed = correctAnswers.length - totalCorrect();
    		return totalMissed;
    	}
     
    	public int[] questionsMissed()
    	{
    		int[] missedQuestion = new int [totalIncorrect()];
     
    		return missedQuestion;
    	}
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: NOOB: Array Help

    private String[] studentAnswers;
    All that does it declare a variable called studentAnswers. When do you actually create the array?
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    DEAF (October 30th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: NOOB: Array Help

    Oh, my bad.
    private String[] studentAnswers = {"B","D","B","A","D",
    								"A","B","B","C","D",
    								"B","C","C","A","D",
    								"E","C","C","D","A"};

    I finally got it work. But the output is unexpected:

    Attempt 1:
    Passed? true
    # correct 16
    # missed 4
    Questions missed: 0 0 0 0

    Attempt 2:
    Passed? false
    # correct 14
    # missed 6
    Questions missed: 0 0 0 0 0 0
    Press any key to continue . . .

    The output is supposed to be like this:

    Attempt 1:
    Passed? true
    # correct 16
    # missed 4
    Questions missed: 3 8 13 18

    Attempt 2:
    Passed? false
    # correct 14
    # missed 6
    Questions missed: 3 5 8 13 16 18
    Press any key to continue . . .


    Any idea? I guess there's something wrong with the questionsMissed() part, but I don't see what I should fix.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: NOOB: Array Help

    Then you need to debug it. Place a bunch of print statements all over the place to display the values of various variables and make sure the output is what you expect.
    Improving the world one idiot at a time!

Similar Threads

  1. Noob question: Why .class?
    By Shaybay92 in forum Java Theory & Questions
    Replies: 10
    Last Post: August 22nd, 2011, 04:56 AM
  2. probably a simple noob problem
    By Unaffiliated in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 23rd, 2011, 12:25 AM
  3. Programming noob here, need help!
    By artenuga54 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2010, 04:04 PM
  4. helloworld noob
    By LostFury2012 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: July 14th, 2010, 06:29 AM
  5. Need Help with my hmwk! Java noob!
    By ravij in forum Loops & Control Statements
    Replies: 4
    Last Post: October 7th, 2009, 01:02 AM