-1

I have a method to return a string of the folder path. CSVFile method calls another method ConfigFilePath to extract the value from a config file and return it back to be used for another condition.

public CSVFile(ManagedElement_T[] meInfo) {
    String path = null;
    ConfigFilePath(path);
    system.out.print("CSV "+path);
}

public String ConfigFilePath(String path) {
    final String directory = System.getProperty("user.dir");
    final String filename = "config.properties";
    final File configFile = new File(directory+"/"+filename);

    final Properties prop = new Properties();
    InputStream input = null;

    try {
        input = new FileInputStream(configFile);
        prop.load(input);
        path = prop.getProperty("diretory_csv");
        System.out.println("PATH " + path);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
             // Close the file
    }

    File checkPath = new File(path.toString());
    System.out.println("CHECK " + checkPath);
    if (checkPath.exists()) {
        System.out.println("Directory already exists ...");
    } else {
           //mkdir if it does not exist
    }


    return path;
}

Problem is, it doesn't return any var path? just says null when I print it but inside the ConfigFilePath method it seems its getting the right values based from the prints in eclipse.

System.out.println("PATH " + path); = C:/opt/CORBA/input
System.out.println("CHECK " + checkPath); = C:\opt\CORBA\input
System.out.print("CSV "+path); = null
user3736748
  • 185
  • 2
  • 3
  • 15
  • possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – Oleg Estekhin Jun 17 '14 at 08:17

4 Answers4

6

The method ConfigFilePath is returning a value. You have to assign that returned value to the variable path:

path = ConfigFilePath(path);

Note: Try to follow Java naming conventions. Use mixedCase for methods/variables and use CamelCase for classes/interfaces

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
2

Java is actually pass by value not pass by reference.

so when you initialized it here:

path = prop.getProperty("diretory_csv");

path wont actually reference that value but get destroyed when out of scope.

solution:

Get the returned value of the ConfigFilePath method

path = ConfigFilePath(path);
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

ConfigFilePath is returning a value. You should get the result from it and put it in the path:

public CSVFile(ManagedElement_T[] meInfo) {
    String path = ConfigFilePath(path);
    system.out.print("CSV "+path);
}
Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
0

The problem you face is that java is passing method-parameters by value, not by reference. It can be confusing, since when using object types the passed value is actually an object-reference.

Let's have a look at some lines of your code:

public String ConfigFilePath(String path)

Here your method gets passed a reference to some String and keeps it in a local variable called path.

path = prop.getProperty("diretory_csv");

Now you're changing the local variable path to point to a new String-object.

return path;

This is passing out the reference as a return-value. ConfigFilePath's local path-variable goes out of scope.

ConfigFilePath(path);

As the other answers said: in this line you're calling the method passing a reference to a String that is referenced by CSVFile's local path-variable. But since passing is by value this path-variable will not be changed by the method-call. Your result is passed via the return-value - and you are ignoring that instead of assigning it to your path-variable.

So to sum it up:

  • ConfigFilePath() doesn't need a parameter since it's not using the value that is passed in
  • You have to assign the returned value to some variable in your calling method for it to be of any use.
piet.t
  • 11,718
  • 21
  • 43
  • 52