Home arrow static arrow Java Programming [Archive] - property file
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - property file
This topic has 4 replies on 1 page.

Posts:53
Registered: 7/12/04
property file  
Aug 4, 2004 9:07 AM



 
I want to read properties from a flat file and I have the following code. It is throwing NullPointerException
can any one please tell me why?

Properties p = new Properties();		InputStream is =			ClassLoader.getSystemClassLoader().getResourceAsStream(				"c:\\test\\prop.txt"); 		try {			System.out.print("\nsssssssssssssssss\n");			p.load(is);		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		} 		if (p == null) {			System.out.println("unable to get properties");		} else {			Enumeration keys = p.keys(); 			while (keys.hasMoreElements()) {				System.out.println((String) keys.nextElement());			}		}
 

Posts:37,103
Registered: 3/30/99
Re: property file  
Aug 4, 2004 9:09 AM (reply 1 of 4)



 
If you're using getResourceAsStream, it has to be relative to the classpath (or whatever the equivalent is for the classloader you're using). If you have the full path to the file (C:\whatever) then use java.io.FileInputStream.
 

Posts:37,103
Registered: 3/30/99
Re: property file  
Aug 4, 2004 9:10 AM (reply 2 of 4)



 
This time the error was easy to spot, but in general, just saying it's giving you an exception isn't enough--tell us WHERE the exception is being thrown. The stacktrace will tell you the line number.
 

Posts:31,095
Registered: 4/30/99
Re: property file  
Aug 4, 2004 9:26 AM (reply 3 of 4)



 
And by the way,
if (p == null)
is a waste of code. You have already assigned p a non-null value in your first line of code, so "p==null" can never be true. Testing
if (is == null)
earlier in your code could be useful, though.
 

Posts:53
Registered: 7/12/04
Re: property file  
Aug 4, 2004 9:27 AM (reply 4 of 4)



 
I used FileInputStream, it works like a charm.
thank you.
 
This topic has 4 replies on 1 page.