NullPointerException when trying to read a file from resources folder - java

I would like to read a property file, like this:
Properties props = new Properties();
props.load(MajorBot.class.getResourceAsStream("application.properties"));
But when I am trying to do this I get an error:
Exception in thread "main" java.lang.NullPointerException: inStream parameter is null
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at java.base/java.util.Properties.load(Properties.java:406)
at majorbot.MajorBot.main(MajorBot.java:13)
My app was created using IntelliJ, new Gradle/Java project. After creation, there was a resources directory allready there. I have created application.properties there, but I cannod read this file.
Is there a way to fix this?

getResourceAsStream will try to find your file in a subfolder of resources that matches your current class’s package. If you want to access a file that is not in a subfolder, specify it as e.g. “/application.properties”.

Related

XML not exist when it does

I am new to Intellij Idea and trying to follow some tutorials. I tried to load an XML into "new ClassPathXmlApplicationContext()". However, there is an error which stated FileNotFoundException.
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [vehicle_beans.xml]; nested exception is java.io.FileNotFoundException:
class path resource [vehicle_beans.xml] cannot be opened because it does not exist
This is my structure:
Here is my code:
XML File location is problem here. As you are using Maven for project, create another folder at level of java named "resources" and put your XML file inside it.
src/main/resources
Resources folder should contain all the config related files to make them available for application to consume. Otherwise you have to pass the full file path to your file reader.
please try this FileSystemXmlApplicationContext instead of ClassPathXmlApplicationContext
private static final ApplicationContext ac =
new FileSystemXmlApplicationContext(
"src/main/java/org/example/vehicle_beans.xml"
);
hope it will work for you

How to load property file located inside jar file

How to load property files placed in resource folder of an executable jar file. Here my app itself is a jar and it executes on it own. It need to find this property file (placed within itself under resource folder) at runtime depending on the path mentioned in the code. I have used below two methods but it didn't help me. Point here is, both these options are working fine when i execute in eclipse, but doesn't work when I pack it into an executable jar. It throws NullPointerException. Only problem I see here is that jar is not able to pick the property files with given path. Any help would be appreciated.
Method 1: Using Apache Commons Configuration
URL propFileURL = XYZ.class.getClassLoader().getResource("/config.properties");
Configuration propertyConfiguration = null;
propertyConfiguration = new PropertiesConfiguration(propFileURL);
In above case I'm getting ConfigurationException. Class is not able to find file mentioned in given path.
Method 2: Using getResourceAsStream. I know that getResource doesn't work if we are to load files from network on in any other location.
InputStream is =XYZ.class.getClassLoader().getResourceAsStream("/config.properties");
Properties prop = new Properties();
prop.load(is);
In this case, I'm getting nullPointerException.
Let me know if you need more details.
jar content Heirarchy
Properties file - /java-file-io-application/src/main/resources/config.properties
XYZ class - /java-file-io-application/src/main/java/org/bc/xyz/iplugin/utilities/XYZ.java
Looks like you might be building your jar incorrectly. Files from 'src/main/resources' would be expected at the root of the jar file. If your jar file contains the 'src/main/resources' directory, something's off with your build.

how to access a file in the root of the android project, java path

I have a configuration folder in the root of my android project and a file in side this project i need to access from a class deep inside the project. the directory structure is like this:
myProject/config/config.properties
myProject/src/com/testProject/util/Configuration.java
form the Config.java I need to read config.properties
I have tries with the following code:
InputStream stream = Configuration.class.getResourceAsStream("../../../../config/config.properties");
but the stream variable is null meaning the pat is wrong.
Can some one please send me the correct relative path to the config properties file searching from the Config.java file. Thanks
It is tricky accessing the a resource from the project since after the .apk is build the local file system on the phone is accessed and not the project files. My solution is putting the files that I need to access in the project/assets file and then they can be acesed ussin the getAssets() method. So the path is like this:
myProject/assets/config.properties
myProject/src/com/testProject/util/Configuration.java
and the code inside Configuration.java is:
AssetManager manager = context.getAssets();
stream = manager.open("config/config.properties");
So the important thing is that only files inside the assets folder can be accessed, and also you need a context to call the getAssets() method.

null pointer exception reading properties file

I really don't know what else to do. I normally put my properties files in my source folder (I use Netbeans, by the way) and when I read it, everything works fine.
Today, I created a new properties file in the source folder and tried to read it and I kept getting a null pointer exception, connoting the nonexistence of the file in the source folder. Yet, there was another properties file in said source folder and I was able to read that one fine. I deleted both properties files and started afresh, but this time I can't seem to read anything.
My codes are fine:
Properties pp = new Properties();
pp.load(getClass().getResourceAsStream("/errors.properties"));
Then I did the clean and build for my project, and now one of the classes can read the errors.properties file (same code, nothing changed), but I'm also now getting a java.lang.NoClassDefFoundError for my second class when I try running it.
WTH!?
Try this:
Properties pp = new Properties();
InputStream is = getClass().getClassLoader().getResourceAsStream("errors.properties");
pp.load(is);
Btw. this is not a bug in NetBeans.
If you use getClass().getResourceAsStream("errors.properties") your error.properties has to be where you class is.
Note: i used InputStream is only because otherwise the line gets too long. Of course you can put both lines into one.

load file within a jar

I need to package a configuration file within a jar. the configuration file is under the root of the jar file.
However I got the following error:
Caused by: java.lang.IllegalArgumentException: URI is not hierarchical
at java.io.File.(Unknown Source)
File url = new File(MyClass.class.getClassLoader().getResource("my.conf").toURI());
You should use getResourceAsStream() instead. If the file is embedded in your JAR the URI is most likely bundle:// URI
InputStream is = this.getClass().getResourceAsStream("my.conf");
Why do you need a file? IF you need to read the config use
Class.getResourceAsStream("/my.conf");
This will need only to be the file in the one folder with the root of your package( the same as in the root of the jar)
The file should be in the same package as the MyClass. I just realized you are creating a File object. Instead try using getResourceAsStream(). This is the right way if you want to read the contents from a classpath resource. Here is the example.

Categories