Why I got null ? This is the result I got it Welcome to the gradebook : null The
ID: 3643108 • Letter: W
Question
Why I got null ?This is the result I got it
Welcome to the gradebook : null
The instructor is : null
I should got
Welcome to the gradebook : Math
The instructor is : John
++++++ First class +++++++
package javaapplication55;
public class GradeBook {
public String courseName ;
public String instuctorName;
public GradeBook(String course , String instructor){
course = courseName ;
instructor = instuctorName;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String name) {
name = courseName;
}
public String getInstuctorName() {
return instuctorName;
}
public void setInstuctorName(String name) {
name = instuctorName;
}
public void displayMessage(){
System.out.println("Welcome to the gradebook : " + getCourseName());
System.out.println("The instructor is : " + getInstuctorName());
}
}
+++++++ second class +++++
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication55;
public class GradeBookTest {
public static void main(String[] args) {
GradeBook gradebook1 = new GradeBook("Math","John");
gradebook1.displayMessage();
}
}
Explanation / Answer
Hi, you are getting a null because there is nothing tells your methods what you want them to return, null means there is nothing to return so instead of returning no output it returns null, I fixed the program for you, please try it now :) /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication55; public class GradeBookTest { public static void main(String[] args) { GradeBook gradebook1 = new GradeBook("Null","Null"); gradebook1.displayMessage(); } } package javaapplication55; public class GradeBook { public String courseName ; public String instuctorName; public GradeBook(String course , String instructor){ course = courseName ; instructor = instuctorName; } public String getCourseName() { return "Math"; } public void setCourseName(String name) { name = courseName; } public String getInstuctorName() { return "John"; } public void setInstuctorName(String name) { name = instuctorName; } public void displayMessage(){ System.out.println("Welcome to the gradebook : " + getCourseName()); System.out.println("The instructor is : " + getInstuctorName()); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.