[Logo] JavaRanch » JavaRanch Saloon
  Search | FAQ | Recent Topics | Hot Topics
Register / Login


Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
Author

Unable to load resource

Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Messages: 61


//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
Sheriff

Joined: Jul 08, 2003
Messages: 21098

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][My Art][AskingGoodQuestions]
Ramesh Kumar Swarnkar
Ranch Hand

Joined: Sep 15, 2003
Messages: 61

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:  14 time(s)

Rob Prime
Bartender

Joined: Oct 27, 2005
Messages: 8832

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
 
 
 
Reply Bookmark it! Watch this topic JavaRanch » Forums » Java » Java in General
 
RSS feed
 
New topic
JProfiler
Get rid of your performance problems and memory leaks!

.