请查看以下MSSQL Server 2005代码。
属性文件
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://127.0.0.1:1433;databaseName=LibMgmtSys
user=sa
password=passwrod
连接文件
public class DBConnection {
static Properties dbproperties;
public static Connection getConnection() throws Exception {
Connection conn = null;
InputStream dbInputStream = null;
dbInputStream = DBConnection.class.getResourceAsStream("jdbc.properties");
try {
dbproperties.load(dbInputStream);
Class.forName(dbproperties.getProperty("driver"));
conn = DriverManager.getConnection(dbproperties.getProperty("url"),
dbproperties.getProperty("user"),
dbproperties.getProperty("password"));
} catch (Exception exp) {
System.out.println("error : " + exp);
}
return conn;
}
}
当我尝试执行dbproperties.load(dbInputStream)时,上面的代码给了我一个NullPointException。我做错了吗?
答案 0 :(得分:2)
您没有实例化dbproperties
,因此当您尝试取消引用它时,它会为空(dbproperties.load(dbInputStream);
)。将其更改为:
try {
dbproperties = new Properties();
dbproperties.load(dbInputStream);