0

I have a problem or a question regarding Java and or Eclipse. I have a programm that does the following:

  • open up a UI where the user can search for a folder
  • if the folder is picked the absolut path is getting passed to the main
  • there are few folders in that chosen path
  • there are functions that read from the data and create a new excel with the read data
  • The path that I am getting is correct!

Gernerally the programm works... But it only works if:

The chosen Path from the User is a folder that is inside the project folder where I run the programm from.

OR

The Names of the files that are getting read are specified inside the project folder. What I mean with that is: if the file test.vcc is inside the package then I could open it from the desktop if there is a file that is also named test.vcc


So it seems like Java or Eclipse cant open up paths that are not inside the project or named in the project - is that true?

Running the programm gives me NullPointerExceptions for every File that is not especially named or inside the project.

Sincerly Faded


Update

Okay so I have noticed something that I am doing wrong definetly!

I was replacing the whole FILEPATH that the User choses with ".\" which obviouslly means that it will not care about the filepath and replace it with .\ instead of really taking the path.

String filePathS = filePath.replace(FILEPATH, ".\\");

When I am using this it works because it can find the files in the project Folder. So after seeing this I just took the real Path that the user choosed.

Its basically this:

String filePathSX = filePath.replace("/", "\\"); 

or this

String filePathSX = filePath.replace("\\", "/");

which either gives me a Path like this: C:\Users\me\dev\foldername\part\test.txt or like this C:/Users/me/dev/foldername/part/test.txt

I can check that in the destination the file test.txt does indeed exist and the path is correct. Still when it tries to do something with that file it gives me a NullPointerExeption.

IF I would leave the replace function for the whole path with .\, then it would take the test.txt file that is inside the package and it would run without any problems.

So I think that I am somehow accsessing the file in the wrong way. How should the path look like on windows if the user chooses something? c/user/... or c\user...

Because both ways do not work for me.

yesIamFaded
  • 1,970
  • 2
  • 20
  • 45
  • 1
    No, it is not true. I have lots of Java programs running in Eclipse that can open files all over the place. Show us a [mre] – greg-449 Apr 13 '21 at 12:40
  • We can't help without seeing exactly how you are trying to read the file, anything else is just a guess. Please edit your question to show us exactly how you are attempting to read the file into your application, and how you are trying to open it or create an excel file from it using your chosen excel/spreadsheet library. Also you can check to see if the file exists as shown here: [how do I check if a file exists in java](https://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – sorifiend Apr 13 '21 at 13:09
  • @sorifiend the Thing is that the file exists and it gets found but it will only not show a NullPointerException if the file is inside the project package - its just weird - i will try to fix it maybe there is a logical error somewhere – yesIamFaded Apr 13 '21 at 13:30
  • Oh, are you trying to load an internal resource that is packaged into the application? That is a completely different issue and a trap for new players because it will often work in eclipse, but once you compile and run your code it will fail because the resource or inner file gets packaged into the jar file, and need to use a resource loader for that not an absolute file path. – sorifiend Apr 13 '21 at 13:33
  • See here: https://stackoverflow.com/q/14089146 – sorifiend Apr 13 '21 at 13:37
  • @sorifiend the thing is that it should not depend on internal resources - it also does not work when I build a .jar, it only works if the files that get read are also inisde the package, but i dont want that behavior i just want to take the files like they are and it kills me that everything looks actually fine because he is finding the files but when he tries to read them I get the NpExp. If the filenames are inisde the package tho then i can open them up and it doesnt care where the file is located – yesIamFaded Apr 13 '21 at 13:45
  • Please edit your question to show the code you are using, I really can't help more without seeing it. – sorifiend Apr 13 '21 at 14:16
  • Your question will remain closed until you edit it to include the full stack trace, and until you show us which line of your code causes the error. The paths are not the issue, you will get a FIleNotFound exception if the path/file is invalid, but you are getting a null pointer error which is a completely different issue caused by something else. Show us more code. – sorifiend Apr 14 '21 at 08:41
  • Note that both `c/user/` and `c\user` are wrong, it needs to have the colon `:` like so `C:\\Users` or `C:/Users`, note also that a backslash "\" is a string escape character, so when using it in a string in Java you need to use "\\" instead. This is a valid path `C:\\Users\\sorifiend\\Documents\\test.txt` and this is valid path `C:/Users/sorifiend/Documents/test.txt` – sorifiend Apr 14 '21 at 08:42
  • Unfortunatly I cant show much more code :/ So if my Path looks like this C:/Users/me/dev/part/test.txt - it should be correct right? The weird thing is that I think that the Handler that writes the excel works! At least if the file is inside the project folder it would write everything like it should. Its just not working if the filepath is really chosen by the user even if the path is 100% correct. – yesIamFaded Apr 14 '21 at 08:52
  • I feel like this is my problem: https://stackoverflow.com/questions/16570523/getresourceasstream-returns-null - I know need to just fix it for my case or at least try to :D – yesIamFaded Apr 14 '21 at 09:18

2 Answers2

1

So it seems like Java or Eclipse cant open up paths that are not inside the project or named in the project - is that true?

Yes and no. But mostly no, that is not true.

Some OSes (most notably, Mac OS) have a security policy framework in place that denies any file access to all applications unless you explicitly allow it. If you're on a mac, that sounds like it could be the culprit.

Other than that, though - the answer is no: That is incorrect.

You'd have to ask a new question and provide a bunch of details if it's not the Mac OS thing: Show the code, show the result of printing the path, and show the actual file structure on disk, for example.

NullPointerException sounds even weirder, so maybe your code is just problematic.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • I am on Windows but it also does not work on linux.... its really weird tbh and explaining everything would take like 2 days :D anyways I might get back here – yesIamFaded Apr 13 '21 at 12:38
  • It sounds like the OP may be attempting to load a file into a library that deals with excel files, but he may not be initialising the excel data object correctly before attempting to open it. – sorifiend Apr 13 '21 at 13:12
0

This is not true...

File f = new File("C:\\path\\to\\your\\file");
// perform some checks here to make sure Desktop can actually open it
Desktop desktop = Desktop.getDesktop();
desktop.open(f);

EDIT: if you are getting NPEs, your path contains no file, try creating a file with a certain name at that location, and then opening it in your file explorer to see where it actually went

Delected
  • 41
  • 4