The moose likes Java in General and the fly likes Unable to load resource Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Unable to load resource" Watch "Unable to load resource" New topic
Author

Unable to load resource

Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 69

//DBconnection.java

private static Connection con = null;
private static Properties databaseProperties = new Properties();

static{
try {
databaseProperties.load(DBConnection.class.getClassLoader().getResourceAsStream("dbInfo.properties"));
Class.forName("com.sybase.jdbc3.jdbc.SybDriver").newInstance();
Properties dbProps = new Properties();
dbProps.put("user", databaseProperties.getProperty("user"));
dbProps.put("password", databaseProperties.getProperty("password"));
String dbURL="jdbc:sybase:Tds:"+databaseProperties.getProperty("server")+":"+databaseProperties.getProperty("port");
con = DriverManager.getConnection(dbURL, dbProps);
System.out.println("dbconnection = "+con);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
--------------------------
while running The above web applicatoin it gives:

java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at monitor.util.DBConnection.<clinit>(DBConnection.java:30)


here line 30 is: databaseProperties.load(DBConnection.class.getClassLoader().getResourceAsStream("dbInfo.properties"));

Any suggestion how to avoid the above exception ?
The dbInfo.properties file is alreay present at the location where this DBconnection.java file is available.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 23247

Well, at runtime, the dbInfo.properties file needs to be where the DBConnection.class file is, not the .java file. If you're building and running this from an IDE, it can be tricky making sure all your bits are in the right place when you run the program. If you're packing this code into a JAR file, then the properties file needs to go into that JAR as well.


[Jess in Action][AskingGoodQuestions]
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Posts: 69
Thank you Ernest.
Actually had kept in folder where I had the DBConnection.class file. but while I moved to the out of that folder and kept inside Class folder (that is 1 level up), it works fine now
 Filename img.bmp [Disk] Download
 Description screen-shot
 Filesize 99 Kbytes
 Downloaded:  15 time(s)

Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 17259

In your code you're using dbInfo.properties, while the file is called vectorDB.properties.

Also, the properties file is not located in the same folder (or a subfolder) as the class file, so you should try using an absolute path: /vectorDB.properties.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
 
subject: Unable to load resource
 
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture