Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't handle properties file

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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"));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading