Search code examples
javagetnullsetreturn

Even I set a value with a set method, it returns null from a get method


This is my main code

public class PSugangSincheong {

    public void run(VUserInfo vUserInfo) {  
        Campus campus = new Campus();
        College college = new College();
        Department department = new Department();
        Lecture lecture = new Lecture();
        CampusPath campusPath = new CampusPath();
        CollegePath collegePath = new CollegePath();
        DepartmentPath departmentPath = new DepartmentPath();
        
        try {
//          System.out.println(vUserInfo.getName() + "welcome.\n");
            int input = 0;
            String path = System.getProperty("user.dir") + "\\data\\root.txt";
            Scanner sc = new Scanner(System.in);
            
            campus.show(path);
            input = sc.nextInt();
            campus.run(input, path);
            path = campusPath.getCampuspath();
            System.out.println(path);
            
            college.show(path);
//          input = sc.nextInt();
//          college.run(input, path);
//          path = collegePath.getCollegepath();
            
//          department.show(path);
//          input = sc.nextInt();
//          department.run(input, path);
//          path = departmentPath.getDepartmentpath();
            
//          lecture.show(path);
//          input = sc.nextInt();
//          lecture.run(input, path);
            
            
            
            
            
            
        
        } catch (FileNotFoundException e) {

            e.printStackTrace();
        }
        
    }

}

and this is my campus class

public class Campus {
    String newPath = "";
    CampusPath campusPath = new CampusPath();
    
    ArrayList<String> campusNum = new ArrayList<>();
    ArrayList<String> campusName = new ArrayList<>();
    ArrayList<String> campus= new ArrayList<>();
    
    public void show(String path) throws FileNotFoundException {
        Scanner campusFile = new Scanner(new File(path));
        
        System.out.println("input a number.");
        while(campusFile.hasNext()) {
            String line = campusFile.nextLine();
            System.out.println(line);
            String[] tokens = line.split(" ");
            campusNum.add(tokens[0]);
            campusName.add(tokens[1]);
            campus.add(tokens[2]);
        }
        campusFile.close();
    }
    
    public void run(int input, String path) {
        while(true) {
            if(input <= Integer.parseInt(campusNum.get(campusNum.size()-1)) && input >= Integer.parseInt(campusNum.get(0))) {
                for(int i = 0; i < campusNum.size(); i++) {
                    if(input == Integer.parseInt(campusNum.get(i))) {
                        System.out.println(campusName.get(i) + "campus\n");
                        newPath = campus.get(i);
                        path = System.getProperty("user.dir") + "\\data\\"+ newPath +".txt";
                        campusPath.setCampuspath(path);
                    }
                }           
                break;
            }else {
                System.out.println("wrong number.");
            }
        }
    }
    
    

}

and this is my campus path code

public class CampusPath {
    private String path;
    
    public void setCampuspath(String path) {
        this.path = path;
    }

    public String getCampuspath() {
        return path;
    }

}

the problem is when I do path = campusPath.getCampuspath();, it returns a null data not a path. could any one help me. It's a bit long code.. I'm sorry about that...

I expected it to set the path and return the path. It sets the path correctly but returns null. when I set it and get it immediately it works but when I do it like the code above it doesn't work. I tried debugging and a lot but don't know why it changes into null.


Solution

  • It looks like the "CampusPath" instantiated in your run() method never has his "path" attribute set.

    You are setting a brand new instance of "CampusPath" in the "Campus" instance, the two "CampusPath" objects are two seperate references and setting one of the two does not mutate the other.