My code for creating a student id is unable to find the family name and i am unsure what part of the code is making it unable to see the family name that it is given when creating a new Student object
this is the error code i am receiving :
java.lang.NullPointerException: Cannot invoke “String.substring(int, int)” because “this.familyName” is null
this is the object class code :
public class Student{
private String givenName;
private String familyName;
private String userName;
private String major;
public static String institutionEmailAddress = "@student.otago.ac.nz";
public Student(String name, String famName, String maj){
name = givenName;
famName = familyName;
userName = createUserName();
}
private String createUserName(){
String s1, s2;
s1 = familyName.substring(0, 3);
s2 = givenName.substring(0, 2);
int firstN = s1.indexOf(1);
int secondN = s2.indexOf(1);
int Num = (firstN * secondN);
String Num2 = String.valueOf(Num);
String finalNum = Num2.substring(0, -3);
String finalUserName = (s1 + s2 + finalNum);
return finalUserName.toLowerCase();
}
public String getEmailAddress(){
return (userName + institutionEmailAddress);
}
public void setMajor(String maj){
maj = major;
}
public String toString(){
return "meow";
}
}
I have tried to change how the variable is initialized, calling it in different ways and using other methods to try and get it to recognize the family name but have hit a wall
Flether is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.