I am new to java and have been working on a project. The project involves two files: One with student ID numbers and their answers for 10 different questions.
The second file contains a bunch of ID numbers from the first file.
The second file has id numbers for a different school.
The task is go through each line in the first file and check if the id number is the same as in the second file, if it is, then move on to the next line, and if it is not, then calculate the student score based on his answer.

I know how to calculate the total.

The question is how to check each id number and see if it is the same. I am getting null pointer exception.

My code is as follows:

public static int findWHSAvg() throws IOException
    {
      readData();
      int count = 0;
      int temp = 0;
      int sum = 1;
      int average = 0;
      String[] ECHSid = new String[50];
      String[] CitizenshipID = new String[50];
      int[] stdt;
      int[] answer = answerKey();
      while (inScan2.hasNextLine())
      {
        String id1 = inScan2.next();
        ECHSid[temp] = id1;
        temp++;
        String nextLine = inScan2.nextLine();
      }

      String aa = inScan.nextLine();
      while (inScan.hasNextLine())
      {
        String id = inScan.next();
        CitizenshipID[count] = id;
        count++;

//        if (!(isMatch(ECHSid[count], CitizenshipID[count])))
//        {
//          stdt = studentAns(new int[11]);
//          for (int cnt = 0; cnt <= 10; cnt++)
//          {
//            if (answer[cnt] == stdt[cnt])
//            {
//              sum++;
//            }
//          }
//        }

        String nextLine = inScan.nextLine();
      }

      for (int a = 0; a <= count; a++)
      {
        boolean match = false;

        for(int b = 0; b <= temp; b++)
        {
          if(isMatch(CitizenshipID[a], ECHSid[b]))
          {
            match = true;
          }
        }
        //if not found
        if(!match)
        {
//          stdt = studentAns(new int[11]);
//          for (int cnt = 0; cnt <= 10; cnt++)
//          {
//            if (answer[cnt] == stdt[cnt])
//            {
//              sum++;
//            }
//          }
//          average = (int)(((sum/11.0)*100.0)+0.5);
          readData();
          while (inScan.hasNextLine())
          {
            String id = inScan.next();
            if (isMatch(id,CitizenshipID[a]))
            {
              stdt = studentAns(new int[11]);
              for (int cnt = 0; cnt <= 10; cnt++)
              {
                if (answer[cnt] == stdt[cnt])
                {
                  sum++;
                }
              }
              average += (int)(((sum/11.0)*100.0)+0.5);
            }
            /*String nextLine = inScan.nextLine();*/
          }
        }
      }
      return average;

    }

ECHSid is an array that has all the ids from the second file and CitizenshipID is an array that has all the ids from the first file.

Please help me, I get the NullPointerException and it says this for the line in this method:

at Project1.findAvg(Project1.java:338)

which is this line:
if(isMatch(CitizenshipID[a], ECHSid))

Please help me.

Edited 5 Years Ago by saurabh2007: n/a

If you don't know which of the two variables is null, add a print statement before the if statement to print out the values of each so you can see which is null. Also print out the values of a and b.

You'll have to figure out which variable you're accessing in that method is null and why. The code that you provided is not enough to answer that question. Put in some debug println() statements to check the values of those array elements prior to that method call to isMatch().

Edit: bleh sorry, cross-posted with Norm. :)

Edited 5 Years Ago by Ezzaral: n/a

You'll have to figure out which variable you're accessing in that method is null and why. The code that you provided is not enough to answer that question. Put in some debug println() statements to check the values of those array elements prior to that method call to isMatch().

Edit: bleh sorry, cross-posted with Norm. :)

I did print out each array and was able to see the null entries in each array. The problem is, how do i then accomplish the task of comparing and checking if an id number from the first array is in the second array or not?

Too impatient to wait longer than 4 minutes for an answer on a forum? Perhaps you need to pay a tutor.

I just wrote that because i have a deadline for this project and why is why i need help on it. I would have payed someone to teach me but I can't do that right now. If you can help, then please do. I appreciate it.

Why did you get the NullPointerException?
Your code should know when an element in the array will be null and not try to look at it. There is a bug there.

I can't seem to find the bug. It should work but I don't know why its seeing a null entry. All that its supposed to do is check if the entry in the first file is also present in the second file, if it is, then do nothing and if it isn't, then continue to calculate grades, but it doesn't compare the two arrays to check if id is same or not.

Copy and paste here the output when you run the program with the printlns in it.
The output should give a clue to the problem.

This article has been dead for over six months. Start a new discussion instead.