skip to Main Content

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

2

Answers


  1. In your current implementation, you attempt to assign values to givenName and familyName by directly setting the parameters name and famName to these fields. However, this does not work because you are reassigning the parameters instead of setting the class fields.

    To fix this, you should assign the constructor parameters to the class fields using this keyword, which refers to the current instance of the class. Here is the corrected version of your class:

    public Student(String name, String famName, String maj) {
        this.givenName = name;      // Correct assignment
        this.familyName = famName;  // Correct assignment
        this.major = maj;           // Assign major to the class field
    
        this.userName = createUserName();
    }
    
    public void setMajor(String maj) {
            this.major = maj;  // Correct assignment
        }
    
    Login or Signup to reply.
  2. When you create an Object from Student class then givenName and famiyName all variable will be initialize as a null object. Inside createUserName when you will call substring method on null object then Obviously it will return Null PointerException.

    If you initialize variable in following ways You will not get NullPointerException

    this.givenName = name;      
    this.familyName = famName;  
    this.major = maj;         
    this.userName = createUserName();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search