0

why am i getting a null return with this code at the bottom? You are a + year

i'm getting a null return when running this code: /** * The Student class represents a student in a student administration system. * It holds the student details relevant in our context. * * @author Michael Kƶlling and David Barnes * @version 2016.02.29 */ public class Lab3 { // the student's full name private String name; // the student ID private String id; // the amount of credits for study taken so far private int credits; // the year of the student private String year; /** * Return the year of the student. */ public String Year() { if((credits >= 0) && (credits <= 30)){ year = "Freshman"; } else if((credits >= 31) && (credits <= 60)){ year = "Sophomore"; } else if((credits >= 61) && (credits <= 90)){ year = "Junior"; } else if(91 <= credits){ year = "Senior"; } return year; } /** * Create a new student with a given name and ID number. */ public Lab3(String fullName, String studentID) { name = fullName; id = studentID; credits = 0; } /** * Return the full name of this student. */ public String getName() { return name; } /** * Set a new name for this student. */ public void changeName(String replacementName) { name = replacementName; } /** * Return the student ID of this student. */ public String getStudentID() { return id; } /** * Add some credit points to the student's accumulated credits. */ public void addCredits(int additionalPoints) { credits += additionalPoints; } /** * Return the number of credit points this student has accumulated. */ public int getC

9th Feb 2018, 2:10 AM
Todd Caputo
Todd Caputo - avatar
7 Answers
+ 2
when i changed the operators it wouldnt display the other years according to the amount of credits i entered. when i changed it back to the && it worked. but it prints out the following on the first println You are a null. After i enter the credits and then print i get the null return. only when i select the getYear method and then print will it display the correct year.
9th Feb 2018, 2:26 AM
Todd Caputo
Todd Caputo - avatar
+ 1
instead of using && use || operator then it will work.
9th Feb 2018, 2:12 AM
Mohd Zaki
Mohd Zaki - avatar
+ 1
it only prints the credit year when running the setYear/getYear method first... please help
9th Feb 2018, 2:16 AM
Todd Caputo
Todd Caputo - avatar
+ 1
/** * Return the login name of this student. The login name is a combination * of the first four characters of the student's name and the first three * characters of the student's ID number. */ public String getLoginName() { return name.substring(0,4) + id.substring(0,3); } /** * Print the student's name and ID number to the output terminal. */ public void print() { System.out.println(name + ", student ID: " + id + ", credits: " + credits); System.out.println("You are a " + year + "."); } } this is the rest of the code
9th Feb 2018, 2:27 AM
Todd Caputo
Todd Caputo - avatar
0
Please share your code from the code playground instead of pasting it in here. It will make it much easier to assist you. You can then use the insert button at the bottom of the screen (on Android at least) to insert a link to your code.
9th Feb 2018, 3:26 AM
Jesse Bayliss
Jesse Bayliss - avatar
0
here is the source code i posted in the playground. its coded using BLUEJ. https://code.sololearn.com/cmAJIpYZoCr6
9th Feb 2018, 3:44 AM
Todd Caputo
Todd Caputo - avatar
0
You have to initialize the "year" variable. https://code.sololearn.com/cqXGVO3hHcRD/?ref=app
9th Feb 2018, 3:55 AM
Jesse Bayliss
Jesse Bayliss - avatar