here is the error message:

Exception in thread "main" java.lang.NullPointerException
at DriverClass.DriverExam.questionsMissed(DriverExam.java:48)
at Chapter7ALabDemo.main(Chapter7ALabDemo.java:33)

program is to read a answer key txt and a student's answers txt, then the number of questions would be entered to correspond with the txt file.

and here is my code in two files:

import java.util.Scanner;
import java.io.*;
import DriverClass.DriverExam;

	public class Chapter7ALabDemo
	{
	    public static void main (String [] args) throws IOException
	    {
	         File file;
	         Scanner readKey;
	         Scanner readAnswers;
	         String str;
	         int numQuestions;
	         DriverExam student;
	         int [] missedQuestions;

	         Scanner keyboard = new Scanner(System.in);

	         System.out.println("Enter the name of the file that is the test key ");
	         str = keyboard.nextLine();
	         file = new File(str);
	         readKey = new Scanner(file);

	         System.out.println("Enter the name of the file with the student answers.");
	         str = keyboard.nextLine();
	         file = new File(str);
	         readAnswers = new Scanner(file);

	         System.out.println("How many test questions are there?");
	         numQuestions = keyboard.nextInt();

	         student = new DriverExam(readKey, readAnswers, numQuestions);
	         missedQuestions = student.questionsMissed();

	         System.out.println(student);
	         if (student.passed())
	              System.out.println("The student passed.");
	         else
	              System.out.println("The student did not pass.");

	         System.out.println("The student got " + student.totalCorrect() + " answers correct.");
	         System.out.println("The student got " + student.totalIncorrect() + " answers incorrect.");
	         System.out.println("The following questions were missed by the student: ");
	         student.printMissed(missedQuestions);
	     }
	}
package DriverClass;

import java.util.Scanner;

public class DriverExam 
{
		private char[] key;
		private char[] answers;
		
//constructor that instantiates the arrays to a given size and stores data read from
//		a file into the answer key array and stores data read from another file into
//		the student answer array
		
		public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
		{
			
		}
		
// “grades the test“ and returns the total number of correct answers
		
		public int totalCorrect()
		{
		int correct = 0;
		  for (int i = 0; i < key.length; i++)
		  {
			  if (key[i] == answers[i])
				  correct++;
		  }
		 return correct;
		}
		
//that returns the total number of incorrect answers		
		
		public int totalIncorrect()
		{
		    int tmissed = 0;
		    tmissed = key.length - totalCorrect();
		    return tmissed;
		}
	
//returns true if the student passed the exam and false if the student failed the exam
//the student needs to get 75% of the questions correct in order to pass	
		
		public boolean passed ()
		{
			 double percentage = 0.75;
			 for(int i = 0; i < key.length ;i++)
			 {
				 if (totalCorrect() > percentage * answers[i])
					 return true;
			 }
			 return false;
		}
		 
//instantiates an integer array that contains the question numbers missed by the student.
//the number of elements in the array should be the number of questions missed.	
		
		public int[] questionsMissed()
		{       
		int size = key.length - totalCorrect();
		int[] missed = {};
		if (size < 1)
		  return missed;
		  	else
		  missed = new int [size];

			int pos = 0;
			for (int i = 0; i < key.length; i++)
			{
			  if (key[i] != answers[i])
			  {
				  missed[pos] = (i +1);
				  pos = pos + 1;
			  }
			}
		  return missed; 
		 }
	
//displays the contents of the array instantiated in questionsMissed
		
		public void printMissed(int[] missedQuestions)
		{
			 System.out.println(questionsMissed());
		}

//displays the correct answers and the student answers stored in the arrays referenced
//by the class fields — the output should be labelled
		
		public String toString()
		{
		String str = "The correct answers are: " + key.toString()  + " and the student answers were: " + answers.toString() ;	
			 return str; 
		}
}

Initialize

private char[] key;
 private char[] answers;

I have intiialized, but it still gives same error, which is directed at my questionsMissed method, specifically at:

int size = key.length - totalCorrect();


it also points to that in my mine app:

missedQuestions = student.questionsMissed();


???

>> I have intiialized

No, you have not, unless there's new code. I'm search for something that looks like "key = new char[...]" and not seeing it.

>> I have intiialized

No, you have not, unless there's new code. I'm search for something that looks like "key = new char[...]" and not seeing it.

public DriverExam(char [] k1, char [] a1)
		{
		k1 = key;
		a1 = answers;
		k1 = new char [key.length];
		a1 = new char [answers.length];
		}

sorry, forgot to show what I added. Same error exists regardless of this...
same error, pointing to what i specified in previous response

public DriverExam(char [] k1, char [] a1)
		{
		k1 = key;
		a1 = answers;
		k1 = new char [key.length];
		a1 = new char [answers.length];
		}

Lines 3 and 4 are overridden by lines 5 and 6 so you can delete them as far as I can tell. Not sure where you are calling this from either, but regardless, I think the memory allocated on lines 5 and 6 would go out of scope immediately and is therefore unusable. The above isn't like C:

void foo(char** k)
{
    *k = malloc(10); // pointer to memory isn't lost
}

Regardless, none of your code initializes key or answers as far as I can tell. Perhaps this?

public DriverExam(char [] k1, char [] a1)
		{
		key = new char [k1.length];
		answers = new char [a1.length];
		}

Without seeing the call to the constructor, it's hard to see where/how this is used.

uuups :-)

void foo(char... k)

i guess I'm not understanding this... so I think my constructor is not working, but I don't understand what I'm missing or supposed to do to fix this error...

I keep making changes and nothing works.

public class DriverExam 
{
		private char key;
		private char answers;
		
		public DriverExam(int k1, int a1)
		{
		
		}
		
//constructor that instantiates the arrays to a given size and stores data read from
//		a file into the answer key array and stores data read from another file into
//		the student answer array
		
		public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
		{
			
		}

I think you were probably right the first time. You want arrays. Don't forget the brackets.

private char key[];
private char answers[];

public DriverExam(int k1[], int a1[])

As for how, assuming k[] and a[] already hold the keys and answers you can either do this

public DriverExam(int k1[], int a1[])
{
    // shallow copy
    key = k1;
    answers = a1;
}

or this

public DriverExam(int k1[], int a1[])
{
    // deep copy
    int numQuestions = k1.length; // assume a1[] is same size
    key = new char[numQuestions];
    answers = new char[numQuestions];
    for(int i = 0; i < numQuestions; i++)
    {
        key[i] = k1[i];
        answers[i] = a1[i];
    }
}

When in doubt or if you don't know the difference between challow and deep copying, use the deep.

I am still getting the same error message:

Exception in thread "main" java.lang.NullPointerException
at DriverClass.DriverExam.totalCorrect(DriverExam.java:39)
at Chapter7ALabDemo.main(Chapter7ALabDemo.java:32)

the line: at DriverClass.DriverExam.totalCorrect(DriverExam.java:39)

directs me to my totalCorrect method, but do not know why? and in my main app, it is pointing at the line: missedQuestions = student.questionsMissed();


I tried both hard and shallow copies, and neither changed the error I am getting. Something else has to be going on here...

here is the code again for my main app and the other class file w/ methods.

import java.util.Scanner;
import java.io.*;
import DriverClass.DriverExam;

	public class Chapter7ALabDemo
	{
	    public static void main (String [] args) throws IOException
	    {
	         File file;
	         Scanner readKey;
	         Scanner readAnswers;
	         String str;
	         int numQuestions;
	         DriverExam student;
	         int [] missedQuestions;

	         Scanner keyboard = new Scanner(System.in);

	         System.out.println("Enter the name of the file that is the test key ");
	         str = keyboard.nextLine();
	         file = new File(str);
	         readKey = new Scanner(file);

	         System.out.println("Enter the name of the file with the student answers.");
	         str = keyboard.nextLine();
	         file = new File(str);
	         readAnswers = new Scanner(file);

	         System.out.println("How many test questions are there?");
	         numQuestions = keyboard.nextInt();

	         student = new DriverExam(readKey, readAnswers, numQuestions);
	         missedQuestions = student.questionsMissed();

	         System.out.println(student);
	         if (student.passed())
	              System.out.println("The student passed.");
	         else
	              System.out.println("The student did not pass.");

	         System.out.println("The student got " + student.totalCorrect() + " answers correct.");
	         System.out.println("The student got " + student.totalIncorrect() + " answers incorrect.");
	         System.out.println("The following questions were missed by the student: ");
	         student.printMissed(missedQuestions);
	     }
	}
package DriverClass;
import java.util.Scanner;

public class DriverExam 
{
		private char key [];
		private char answers [];
	
		  
		public DriverExam(char k1[], char a1[])
		{
		      int numQuestions = k1.length; // assume a1[] is same size
		   
		      key = new char[numQuestions];
		      answers = new char[numQuestions];
		   
		      for(int i = 0; i < numQuestions; i++)
		      {		   
		      key[i] = (char) k1[i];
		      answers[i] = (char) a1[i];
		      }
		}
	
//constructor that instantiates the arrays to a given size and stores data read from
//		a file into the answer key array and stores data read from another file into
//		the student answer array
		
		public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
		{
			
		}
		
// “grades the test“ and returns the total number of correct answers
		
		public int totalCorrect()
		{
		int correct = 0;
		  for (int i = 0; i < key.length; i++)
		  {
			  if (key [i] == answers [i])
				  correct++;
		  }
		 return correct;
		}
		
//that returns the total number of incorrect answers		
		
		public int totalIncorrect()
		{
		    int totalMissed = 0;
		    totalMissed = key.length - totalCorrect();
		    return totalMissed;
		}
	
//returns true if the student passed the exam and false if the student failed the exam
//the student needs to get 75% of the questions correct in order to pass	
		
		public boolean passed ()
		{
			 double percentage = 0.75;
			 for(int i = 0; i < key.length ;i++)
			 {
				 if (totalCorrect() > (percentage * answers [i]))
					 return true;
			 }
			 return false;
		}
		 
//instantiates an integer array that contains the question numbers missed by the student.
//the number of elements in the array should be the number of questions missed.	
		
		public int [] questionsMissed()
		{ 	
		int size = key.length - totalCorrect();
		int [] missed = {};
		if (size < 1)
		  return missed;
		  	else
		  missed = new int [size];

			int pos = 0;
			for (int i = 0; i < key.length; i++)
			{
			  if (key [i] != answers [i])
			  {
				  missed [pos] = (i + 1);
				  pos = pos + 1;
			  }
			}
		  return missed; 
		 }
	
//displays the contents of the array instantiated in questionsMissed
		
		public void printMissed(int [] missedQuestions)
		{
			 System.out.println(questionsMissed());
		}

//displays the correct answers and the student answers stored in the arrays referenced
//by the class fields — the output should be labelled
		
		public String toString()
		{
		String str = "The correct answers are: " + key.toString()  +
		" and the student answers were: " + answers.toString() ;	
			 return str; 
		}
}

and when you run the program, I am using these txt files for the input files, and the number of questions is 20.

key1.txt:

B
D
A
A
C
A
B
A
C
D
B
C
D
A
C
C
C
B
D
A

ans1.txt

B
D
A
A
C
D
B
A
C
D
B
C
D
A
C
A
C
B
D
A

Notice which constructor you are calling...

student = new DriverExam(readKey, readAnswers, numQuestions);
public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
{
			
}

It has yet to be written! You wrote one, but the one you wrote is the one that never gets called.

If you want to allocate the memory,

public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
{		   
    key = new char[numQuestions];
    answers = new char[numQuestions];

    // read data from scanners, fill in arrays.		
}

That allocates the memory. You'll need to write the part that reads the data.

this is what I was thinking would read the data from array, but that was wrong, giving an error, how should it be, in psuedo code if needed, as I am confused as to how to direct data?

public DriverExam(Scanner readKey, Scanner readAnswers, int numQuestions)
		{
			key = new char[numQuestions];
			answers = new char[numQuestions];
			
			for (int i = 0; i < numQuestions; i++)
			{
				key [i] = key[numQuestions];
				answers [i] = answers[numQuestions];
			}
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.