I am using Java to handle properties file. I am trying to reach and read data from myconfig.properties file but it gives me error that says: cannot take null parameter, but the parameter is not null
Please help me to read data from properties file using java:
here is my code:
Properties p = new Properties();
p.load(getClass().getResourceAsStream("myconfig.properties"));
String s = p.getProperty("Name");
System.out.println(s);
>Solution :
Use file path (file path of property file) as a String and FileInputStream to handle this file path. Then load it to the load method of Properties class.
String filepathOfYourConfiFile ="C:\\Users\\resha\\IdeaProjects\\FrameWorkProject\\src\\main\\resources\\myconfig.properties";
FileInputStream read = new FileInputStream(filepathOfYourConfiFile);
Properties p = new Properties();
p.load(read);
String s = p.getProperty("Name");
System.out.println(s);
Hope it will work
Update:
The above solution will work until you make a Jar file. If you will make a Jar file it will not work as the Rob Spoor mentioned in the comment
Other way is adding the / before the property file name
p.load(getClass().getResourceAsStream("/myconfig.properties"));