Jack Trowbridge
Jack Trowbridge

Reputation: 3251

Loading .txt file within JAR

I've got a db.properties.txt file with my package com.noteu

Within the same package as the file I'm trying to load it in the constructor of a properties object within a Database class like as follows :

properties.load(Database.class.getResourceAsStream("db.properties.txt"));

However I get a java.lang.NullPointerException as follows :

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.noteu.Database.get(Database.java:24)
    at com.noteu.menus.Signin.checkSignedInStatus(Signin.java:52)
    at com.noteu.menus.Signin.<init>(Signin.java:253)
    at com.noteu.Main.main(Main.java:33)

Upvotes: 0

Views: 114

Answers (1)

atish shimpi
atish shimpi

Reputation: 5023

Change type of file to .properties, properties.load(); loads properties file not txt file as stream.

public void load(InputStream inStream) throws IOException

Reads a property list (key and element pairs) from the input byte stream.

Modified code :

properties.load(Database.class.getResourceAsStream("db.properties"));

To load .txt file instead of properties file you have to create FileInputStream instance.

FileInputStream fis new FileInputStream("myfile.txt");
props.load(fis)

If my solutions are not working please make sure that your path is correct.

Upvotes: 1

Related Questions