Danial Kosarifa Danial Kosarifa - 2 months ago 6
Java Question

Although I've defined value for my object attributes but they all become null ?Why?

I have an

ArrayList
called
building
which stores the instances of a super class called
Residence
. (At this point I've defined only one of the sub classes called
Manager
). What I am trying to do is to read from a text file if the position is
m
, (short for manager) create an object called "mng" type manager and store it in
building
. I have no problem when I read the file, but once I insert the values in my constructor, the values become null.

here is my residence class:

public class Residence {
String posision;
String username;
String pass;
String accNo;
String type;
String no;
public Residence(String p, String u, String pas, String acc, String type, String no) {
this.posision = p;
this.username = u;
this.pass = pas;
this.accNo = acc;
this.type = type;
this.no = no;
}

public Residence() {}
}


manager class:

public class Manager extends Residence {
public String posision;
public String username;
public String pass;
public String accNo;
public String type;
public String no;

public Manager() {
super();
}

public Manager(String p, String u, String pas, String acc, String type, String no) {
super(p , u , pas, acc, type , no);
}

public String getUsername() {
return username;
}
}


and here is the main:

public static void main(String[] args) {
File file = new File("E:\\guaz\\eclipse_javafx\\workspace\\dkflozaTesting\\src\\dkflozaTesting\\danial.txt");
ArrayList<Residence> building = new ArrayList<Residence>();
String posision = "";
String username = "";
String pass = "";
String accNo = "";
String type = "";
String no = "";
try {
Scanner scan = new Scanner(file);
while(scan.hasNext()) {
posision = scan.next();
username = scan.next();
pass = scan.next();
accNo = scan.next();
type = scan.next();
no = scan.next();
System.out.println(posision + " " + username + " " + pass + " " + type + " " + no);
if (posision.compareToIgnoreCase("m") == 0) {
System.out.println("in if : " + posision + " " + username + " " + pass + " " + type + " " + no);
Manager mng = new Manager(posision, username, pass, accNo, type, no);
System.out.println("username : " + mng.getUsername());
building.add(mng);
}
}

System.out.print("\n \n");
//System.out.print(list.size());
//System.out.print(list.get(0).getUsername());
System.out.print("\n \n");
} catch(Exception e) {
System.out.println(e.getLocalizedMessage());
}
}


And the output of the main as follows:

t dkfloza pass pre 1
in if : t dkfloza pass pre 1
username : null
m danial pass1 post 2


Worth mentioning that the size of the apartment changes to 1 but the value of all attributes are null.

Answer

You have to remove the variables from Manager class if you want to initialize them.

public class Manager extends Residence {
   public Manager() {
      super();
  }

  public Manager(String p, String u, String pas, String acc, String type,    String no) {
      super(p , u , pas, acc, type , no);
  }

  public String getUsername() {
      return username;
  }
}

Since you have been overriding the variables, you have to reinitialize them if you want to assign the values. But there is not point of overriding the same variables if you are inheriting them. (Ps. the variables should usually set as private and use getters and setters)

Comments