1

This question looks like very similar to: Concatenating null strings in Java

But my issue is some different.

I want to build an absolute path to a file:

String path = properties.get("path"); // returns /home/myuser/relativepath/ , ends with bar /
String file = currentFile; // currentFile values "file.txt"
String result = path + file; // this results in /home/myuser/relativepath/nullfile.txt

Why is there than 'null' text? That's the reason my application does not work now.

I have review it in Windows and Linux. In Windows it works perfectly. In Linux, I have this issue.

I uploaded properties file and then, edited with vi command. Maybe is this the problem? Shouldn't I use this way to generate an absolute path, and use File.Separator property in Java?

EDIT: I have post my final right answer with detailed steps. I hope it would be useful.

Community
  • 1
  • 1
yaki_nuka
  • 724
  • 4
  • 26
  • 44
  • my suggestion is for you to check the docs.oracle.com/javase/tutorial/essential/io/pathOps.html – Leo Mar 04 '14 at 00:56
  • Please show how you build the value of `result`. – rgettman Mar 04 '14 at 00:57
  • Your code won't compile: it'll give an error on that third line, "variable result might not have been initialized." Can you edit your question to include a full, _compilable_ code snippet that reproduces the problem? (You may even find that in putting that snippet together, you figure out the answer.) – yshavit Mar 04 '14 at 01:41
  • `String result = path + file;` **not** `String result = path + result;` see my anwser – Zied R. Mar 04 '14 at 01:45
  • @Ziprox09 Your answer is already below; you don't need to post it here, too. But your code wouldn't result in the string including "null" (assuming `path` and `file` are what the OP says they are), so the OP's problem is obviously something different. Without knowing what the actual problem is, it's a bit difficult to help them solve it (or upvote an answer). In other words, your answer fixes a mistake the OP clearly doesn't have, because such a mistake would cause a compile-time error, not a runtime error as described. – yshavit Mar 04 '14 at 02:10
  • @Ziprox09, @yshavit, @rgettman, @leo, thank you all and excuse me. I had a writing error. My issue is that `String result = path + file;` results in `/home/myuser/relativepath/nullfile.txt`. How can I fix it? I'll try to concatenate with File.Separator and checking variable initializations. – yaki_nuka Mar 04 '14 at 06:49
  • @yaki_nuka Have you confirmed that `path` and `file` are exactly what you think they are? I would do two things: First, look at each of those variables in a debugger _right_ before the `String result =...` line (alternatively, print them out using `System.out.println`). Maybe `path` ends in "...null" or `file` starts with "null..."? If those look right, you can triple-check by printing out their bytes: `System.out.println(Arrays.toString(path.getBytes())` (and similarly for `file`). If you update your question with that output, it'll let us see the exact bytes that are in your string. – yshavit Mar 04 '14 at 19:00
  • @yshavit, I'm testing a solution based on all sugestions given here: direct creation and edition with Linux vi (not uploading from Windows) and check variables initialization, characters at starting and ending. I hope I'm posting the right solution soon. Thank you – yaki_nuka Mar 06 '14 at 12:34
  • Finally, I have found a solution. I'll explain it now. – yaki_nuka Mar 07 '14 at 11:03

4 Answers4

1

My bet (though I have not seen Java behave this way) is there's a null-ish character (such as a carriage return) of some sort in your properties file which Windows handles at the OS level so Java/Properties doesn't see it.

As a first pass, try printing the length and last few characters of your path string, e.g.:

for(int i = Math.max(0, path.length()-5); i < path.length(); i++) {
  System.out.print(path.charAt(i)+":"+((int)path.charAt(i))+" ");
}
System.out.println(path.length());

Willing to bet the last character, on Linux, is not what you'd expect. The right fix would then be to clean up your properties file so that it's compatible on both OSes.

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • thank you and excuse me. I had a writing error. My issue is that `String result = path + file;` results in `/home/myuser/relativepath/nullfile.txt`. How can I fix it? I'll try to concatenate with File.Separator, checking variable initializations and full create and edit config file in Linux. – yaki_nuka Mar 04 '14 at 06:51
  • Did you try my suggestion? – dimo414 Mar 04 '14 at 15:44
  • I'm testing a solution based on all sugestions given here: direct creation and edition with Linux vi (not uploading from Windows) and check variables initialization, characters at starting and ending. I hope I'm posting the right solution soon. Thank you – yaki_nuka Mar 06 '14 at 12:36
  • Finally, I have found a solution. I'll explain it now – yaki_nuka Mar 07 '14 at 11:04
1

Well, the complete and detailed steps to fix my issue are these (maybe any of them could not be necessary, but I prefer to write them all):

  1. Create config file in Linux with vi, emacs, ... (not upload file from Windows).
  2. Edit file with vi, emacs... At the end of each path, do not include directory separator character ( / ).
  3. Check variables before contatenate them. Make sure they don't have any space and other unexpected character.
  4. Concatenate variables with:

    String result = path + File.separator + file;

I hope this would be useful. Thank you all for your suggestions. Regards

yaki_nuka
  • 724
  • 4
  • 26
  • 44
0

What do you expect?
Yo´re doing a String result = path + result;
int a = 1 + a would be similar... don´t use a variable to init itself.
(That can´t be your code in the first place, if you´re getting this output.)

deviantfan
  • 11,268
  • 3
  • 32
  • 49
0

result is path+file :

    String path = properties.get("path");
    String file = currentFile; 
    String result = path + file; 
                            ^ 
                       change here

the result is: /home/myuser/relativepath/file.txt

Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • thank you and excuse me. I had a writing error. My issue is that `String result = path + file;` results in `/home/myuser/relativepath/nullfile.txt`. How can I fix it? I'll try to concatenate with File.Separator and checking variable initializations. – yaki_nuka Mar 04 '14 at 06:50