剛開始使用mevan出現的Null Pointer Exception問題
阿新 • • 發佈:2018-12-21
這個是測試時候,這個部分報錯的情況,然後對照著上面指示的位置一個個檢查也沒有發現到底哪裡錯了。
public class EMSUtils { private static BasicDataSource ds; static { try{ //進行初始化 ds=new BasicDataSource(); Properties p=new Properties(); InputStream in=EMSUtils.class.getClassLoader().getResourceAsStream("dbconfig.properties"); p.load(in); ds.setUrl(p.getProperty("url")); ds.setUsername(p.getProperty("username")); ds.setPassword(p.getProperty("password")); ds.setDriverClassName(p.getProperty("className")); ds.setMaxWait(Long.parseLong(p.getProperty("maxWait"))); ds.setMaxActive(Integer.parseInt(p.getProperty("maxActive"))); ds.setInitialSize(Integer.parseInt(p.getProperty("initSize"))); }catch(Exception e){ System.out.println("配置檔案載入失敗..."); e.printStackTrace(); } } //獲取資料庫連線 public static BasicDataSource getDataSource(){ return ds; }
這是寫的EMSUtils檔案這部分的內容。後來檢查之後,注意到maven和之前使用Dynamic Web Project佈局區別挺大的。之前習慣配置檔案,也就是這裡的dbconfig.properties放在utils裡面EMSUtils檔案旁邊。
後來把dbconfig.properties放到了resource位置,才解決了這個問題。
然後test,程式執行一切正常。