-3

DITP 3113 class

package com.ditp3113;

import java.util.Scanner;//Scanner class takes input
public class Student {
    
    
    /////Decalartion of the fields
    
    private String studName;
    private String idNo;
    public static double[] marks =new double[3];//Stores marks of three tests// static to ensure this double can be used universally
    
    
    
    public Student(String studName,String idNo)
    {  ///Constructor that accepts student's name and id as arguments
        this.idNo=idNo;
        this.studName=studName;
        
        
    }
    
    
    public static void main(String []args) {
        
     model model[] =new model[3];
        
        Scanner input=new Scanner(System.in);
        
        
        Student OmarObject=new Student("Omar","B0123345");
        Student HanaObject=new Student("Hamza","B06564788");
        
        System.out.println("Please enter 3 marks for Omar:");
        
        
        for (int i=0; i<3;i++) {
        
            marks[i]=input.nextDouble();
            
           System.out.println();
                
        }
    
         
    model[0].setMarks(marks);
        
    
    System.out.println("Please enter 3 marks for Hana");
        
        
        for (int j=0; j<3;j++) {//Stores 3 marks for 3 subjects
            
            marks[j]=input.nextDouble();
            
            System.out.println();
        
            
        }
        
        model[1].setMarks(marks);
      
        double[] OmarMarks =model[0].getMarks();
        for (int i = 0; i <OmarMarks.length; i++) {
            System.out.println(OmarMarks[i]);
            
         
        } 
        
        double[] HannaMarks =model[1].getMarks();
        for (int j = 0; j <HannaMarks.length; j++) {
            System.out.println(HannaMarks[j]);
        } 
        
        
    }

}

Model class(model.java)

package com.ditp3113;

public class model {
    
    private double[] marks;
    
    

    
    public void setMarks(double[] marks) {
        
        this.marks=marks;
    }
    
    public double[] getMarks() {
        
        return marks;
    }

    
    
    
}

When i run , the following error comes:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.ditp3113.model.setMarks(double[])" because "model[0]" is null
    at com.ditp3113.Student.main(Student.java:46)

Can you please assist me how to fix this problem?

  • 3
    Hint: where do you believe you're ever creating an instance of the `model` class? (As an aside, I'd also *strongly* advise you to start following Java naming conventions ASAP.) – Jon Skeet Nov 16 '21 at 18:59
  • I was trying to create an array of objects for model class. Model[0] stores details of student1, model[1] stores details of student 2....like this ..... – Habeeb E Sadeed Nov 16 '21 at 19:04

1 Answers1

0

When doing model = new model[3];, you create an array with a size of 3. But, it's empty.

So, when you do model[0], this will return null.

To fix this, you should set value, such as model[0] = myModel;. In your case, model[0] = new model(); should works, but only for the first item (the second and third will always be null).

More informations about NPE for Java, it's here

Elikill58
  • 4,050
  • 24
  • 23
  • 45