5 Replies - 195 Views - Last Post: 05 May 2019 - 05:17 AM Rate Topic: -----

#1 overwhelmed_student   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Objects.requireNonNull(File)

Posted 05 May 2019 - 02:46 AM

Hey guys,
I want to read a .csv file and I used Objects.requireNonNull and then went up and down the path until I reached the file. However, while the file is indeed there and I checked my path, the method throws a NullPointerException. Also, this method was only my template. I am supposed to use it to get the file so that I can work on that file. It is supposed to work. Any ideas why it isn't?

return new File(Objects.requireNonNull(
                MyClass.class.getClassLoader()
                        .getResource("name_of_the_file.csv")).getFile()
        );



Is This A Good Question/Topic? 0
  • +

Replies To: Objects.requireNonNull(File)

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3631
  • View blog
  • Posts: 16,676
  • Joined: 20-September 08

Re: Objects.requireNonNull(File)

Posted 05 May 2019 - 03:52 AM

It needs to be in the correct location of the classpath for that to work. I'd probably put it at the package root and then do

.getResource("/name_of_the_file.csv")


Better still, make a location at the package root for all your resources and place it there:

.getResource("/resources/name_of_the_file.csv")

This post has been edited by g00se: 05 May 2019 - 03:55 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 overwhelmed_student   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Objects.requireNonNull(File)

Posted 05 May 2019 - 04:21 AM

View Postg00se, on 05 May 2019 - 03:52 AM, said:

Better still, make a location at the package root for all your resources and place it there:

.getResource("/resources/name_of_the_file.csv")


It is there. I tried a few things and now it finds the file. However, there are three files in my folder "resources" and when I change the name to access a different file, it immediately doesn't find it again. I will try to add "/resources/" and see if it helps. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3631
  • View blog
  • Posts: 16,676
  • Joined: 20-September 08

Re: Objects.requireNonNull(File)

Posted 05 May 2019 - 04:39 AM

Resources needs to be at the root. e.g. if you have package a.b.c, /resources needs to have the same parent directory as a
Was This Post Helpful? 0
  • +
  • -

#5 overwhelmed_student   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 71
  • Joined: 10-May 18

Re: Objects.requireNonNull(File)

Posted 05 May 2019 - 04:56 AM

View Postg00se, on 05 May 2019 - 04:39 AM, said:

Resources needs to be at the root. e.g. if you have package a.b.c, /resources needs to have the same parent directory as a


It doesn't work. So when I have:
	return new File(Objects.requireNonNull(
	                MyClass.class.getClassLoader()
	                        .getResource("name_of_the_file.csv")).getFile()

	        );


my program finds name_of_the_file.
But when I change it to:

	return new File(Objects.requireNonNull(
	                MyClass.class.getClassLoader()
	                        .getResource("name_of_other_file.csv")).getFile()
	        );



name_of_other_file can't be found.
And the most confusing part is: I wanted to go back to the file "name_of_the_file" because my program was able to find it before, but once I change the name, it doesn't work, too. My only option is to press ctrl+z until I go back to how it was before. Why?? Why can I not change the name of the file to a different file? It is in the same directory, same folder, same path. What is happening here?
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3631
  • View blog
  • Posts: 16,676
  • Joined: 20-September 08

Re: Objects.requireNonNull(File)

Posted 05 May 2019 - 05:17 AM

Your path is completely different to mine - i'm surprised you haven't noticed ...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1