-1

I have sourcehandler.java class which has the code

public class SourceHandler {
  String PrpPath = null;
  Properties prop = null;

 public Properties loadConfigProperties() {

try {

    System.out.println("Propertiess " +PrpPath );
    InputStream in =new FileInputStream(new File(PrpPath ));
    prop.load(in);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
return prop;
}

and main method in a different class,

    public static void main(String[] args) throws ParserConfigurationException,
                                              Exception {
    try {

        SourceHandler conf = new SourceHandler();

        conf.setProperties("C:/config.properties");

        Properties p = conf.loadConfigProperties();

        System.out.println("Done");

    } catch (DOMException dome) {
        // TODO: Add catch code
        dome.printStackTrace();
     }

Now, if i run the code , it shows null pointer exception at line , prop.load(in);

stack trace:

java.lang.NullPointerException at DecryptEncryptSource.SourceHandler.loadConfigProperties(SourceHandler.java:98) at DecryptEncryptSource.SourceHandler.updateCofigDestn(SourceHandler.java:151) at DecryptEncryptSource.MainClass.main(MainClass.java:27)

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Jeelan
  • 11
  • 5
  • Could you add the exact path `String` you are using ? – Arnaud Mar 01 '17 at 13:35
  • yes Berger, that is the exact path " C:\config.properties'' passed from main method i could print also thru sop.. – Jeelan Mar 01 '17 at 13:37
  • Show us how you pass it from the `main` method. That is crucial. – Erwin Bolwidt Mar 01 '17 at 13:38
  • I Have a variable 'Properties' in Source handler class and using accessor setter method i am setting the value of path of proprties file from public static void main(String[] args) throws try { SourceHandler conf = new SourceHandler(); conf.setProperties(" C:\\config.properties"); conf.updateCofigDestn(); System.out.println("Done"); } catch (DOMException dome) { // TODO: Add catch code dome.printStackTrace(); } } – Jeelan Mar 01 '17 at 13:49
  • Did you copy and paste the `main`-code in your comment? Your path starts with a blank, that would match the error-message, since absolute paths should start with a drive-letter, not a blank. – piet.t Mar 01 '17 at 14:07
  • @piet the above is main code , i had made changes , and it starts with drive-letter now , its java.lang.NullPointerException at line prop.load(in); please suggest any ideas – Jeelan Mar 01 '17 at 14:19

1 Answers1

0

First of all,

InputStream in =new FileInputStream(new File(Properties));

should better read

InputStream in =new FileInputStream(new File(propertyFileName));

to avoid any ambiguity; and then:

  • Are you sure that there is really a file named C:\config.properties
  • Probably you need either escaping: C:\\config.properties; or you try C:/config.properties

Regarding the update; you have this line:

Properties prop = null;

and further down:

prop.load(in);

And you are surprised that you get a NPE? Really? Hint: look into your code and create that Property object using the file path; instead of just calling a method on a null object.

And the real answer is read this here over and over again.

(and for those who wonder why I didn't close out as duplicate ... I can't any more, because I already close-requested on another reason )

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I have the file in that path C:\\config.properties , now at line , prop.load(in); it throws java.lang.NullPointerException – Jeelan Mar 01 '17 at 14:00
  • You are welcome. In case you are "happy" .. please consider accepting my answer. Now, or when you are sure that you will be around tomorrow; tomorrow would be fine, too ;-) – GhostCat Mar 01 '17 at 15:30