0

public class Student {

int[] mark ;
char[] grade;
public int[] getMark() {
    return mark;
}
public void setMark(int[] mark) {
    this.mark = mark;
}
public char[] getGrade() {
    return grade;
}
public void setGrade(char[] grade) {
    this.grade = grade;
}


public Student(int[] mark){
    this.mark=mark;
    char[] grade=new char[mark.length];
}
public void findGrade(){
    for(int i=0;i<grade.length;i++){
        if(mark[i]>=92){
            grade[i]='E';      
        }
        if(mark[i]>=85&&mark[i]<92)
            grade[i]='A';
        if(mark[i]>=70&&mark[i]<85)
            grade[i]='B';
        if(mark[i]>=65&&mark[i]<70)
            grade[i]='C';
        if(mark[i]<65)
            grade[i]='D';
    }
    
}

} public class Tester {

public static void main(String[] args) {
    int[] marks = { 79, 87, 97, 65, 78, 99, 66 };
    Student student = new Student(marks);
    System.out.println("Grades corresponding to the marks are : ");

    student.findGrade();
    
    char[] grades = student.getGrade();
    for (int index = 0; index < grades.length; index++) {
        System.out.print(grades[index] + " ");
    }
}

}

I dont know what is the erorr, it throws null pointer exception erorr, please explain me! it is a array program to find the corresponding grades based on marks!!

jarvis
  • 29
  • 7
  • 2
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Jan 19 '21 at 18:01
  • The exception should contain the line number where it occurred. What line is it? – Mark Meuer Jan 19 '21 at 18:05
  • In findGrade(), the for-loop should be limited by mark.length rather than grade.length. – NomadMaker Jan 19 '21 at 19:32

2 Answers2

2

You are declaring local grade array in the constructor and assigning pointer to new array to it. But the global variable grade stays null.

Just replace

char[] grade=new char[mark.length];

line with

 this.grade=new char[mark.length];

and it will work.

1

You have initialized the wrong 'grade' variable. Check your constructor:

char[] grade=new char[mark.length];

The grade you declared is a local variable, not the global variable in your class. To fix it, just use

grade=new char[mark.length];

instead.