0

So I have this private inner class as follows:

private class Exam{
   private String course;
   private double score;

   public Exam(String course, double score){
       this.course = course;
       this.score = score;
   }


   public String toString(){
       return String.format("%-25s: %1.2f", course, score);
   }
}

I have created an instance of this as an array

private Exam[] preliminaryExams = new Exam[6];

I did not create this class, it is part of a java learning tutorial thing, and I am having difficulty populating the array itself. What I've been trying to do was give values to the score and course variables like so:

preliminaryExams[0].score = some double;
preliminaryExams[0].course = some string;

This compiles fine, but when I try to actually execute some code using these classes and methods I get

Exception in thread "main" java.lang.NullPointerException

On the first line where I am trying to assign these values to those variables. I'm kind of stuck at this point and any help would be appreciated.

2
  • can u post stacktrace
    – Ankit
    Jul 26, 2013 at 20:27
  • How did that compile? score is private Jul 26, 2013 at 20:28

4 Answers 4

5

After you have created the array of Exam, each entry in the array still points to null. You need to create instances of Exam, and assing them in your array, before you can start settings attributes on your Exam instances.

for (int i = 0; i < 6; i++) {
    preliminaryExams[i] = new Exam (someCourse, someScore);
}
2
  • 1
    Don't forget the parameters in new Exam.
    – amb110395
    Jul 26, 2013 at 20:29
  • Thank you. I knew I was making some dumb mistake and repeatedly over looking it. Jul 26, 2013 at 20:33
2

After you created your array still each of those Exam elements points to null. So you will have to explicitly instantiate each of them as well. you could do the following.

for (Exam exam : preliminaryExams){
exam=new Exam(someString,someDouble);
}
1
  • 1
    Upthumbs for using a foreach loop Jul 26, 2013 at 20:36
0

You instantiate an array of Exam but every element contained is still null, so you must explicitly instantiate them.

Since you have an Exam constructor I don't see why you don't use it:

private Exam[] preliminaryExams = new Exam[6];

preliminaryExams[0] = new Exam(some course, some double);
0

seems you haven't initialized array elements, the below line is setting the score field of an array element, not the element itself

preliminaryExams[0].score = some double;

make sure you have initialized the elements as

preliminaryExams[0] = new Exam(some course, some double);//call appropriate constructor here

before setting the score variable (any field/method of preliminaryExams in general)

1
  • new preliminaryExams() is wrong, preliminaryExams contains instances of Exam. I have upvoted to counter the downvote.
    – Laf
    Jul 26, 2013 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.