class.getClassLoader().getResourceAsStream报错java.lang.NullPointerException

这个问题我是在写JDBC工具类里遇到的,在静态代码块中报错。

Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));

经过网上多次搜索,这里总结一下问题原因和解决办法:

原因:源根设置的问题

解决办法:

先看一下我的druid.properties文件在哪里:

然后点开项目结构 

 然后看到红框的内容:

 我们发现,druid.properties只在src目录下,但是不在上面源文件夹,测试、资源文件夹的目录下,所以我们JVM是读取不到资源的,这很奇怪,但是这应该和getClassLoader().getResourceAsStream的实现有关系。

所以要么我们把druid.properties文件放到源根文件夹下面,要么设置一下。

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
160个项目开发实例,聊天项目,小游戏适和新手学习(项目采用GBK编码)。 import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.*; import javax.swing.*; class Gobang extends JFrame implements Runnable, ActionListener { final static int Player=1; final static int AI =-1; ClassLoader cl = this.getClass().getClassLoader(); Toolkit tk = Toolkit.getDefaultToolkit(); int length=14, game_state, winner, check, step; int grid[][] = new int[length][length]; int locX, locY /* 囱竚 */, count /* 硈囱计 */, x, y /* 既竚 */, displace_x=0, displace_y=0 /* 簿秖 */, direction; ArrayList steps = new ArrayList(); /* 癘魁囱˙ */ JPopupMenu control_menu = new JPopupMenu(); /* 龄匡虫 */ JMenuItem[] command = new JMenuItem[4]; String[] command_str={"囱", "郎", "弄郎", "秨"}; int[][] dir = { {-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1} }; boolean[] dir2 = new boolean[8]; boolean turn; String message; final JDialog dialog = new JDialog(this, "叫匡", true); Font font=new Font("new_font", Font.BOLD, 20); Grid grids[][] = new Grid[length][length]; Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; JPanel boardPanel, bigpanel; JRadioButton[] choice = new JRadioButton[2]; final static int Start =0; final static int Select =1; final static int Playing =2; final static int End =3; final static int nil=-1; /* 礚よ */ final static int oblique_1 =0; /* オ */ final static int oblique_2 =1; /* オ */ final static int horizontal =2; /* 绢 */ final static int vertical=3; /*  */ Gobang() { super("き囱"); boardPanel = new JPanel(); boardPanel.setLayout(new GridLayout(length, length, 0, 0)); boardPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); for(int i=0; i<length; i++) for(int j=0; j<length; j++) {
package druidJDBCUtils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DruidJDBCUtils { //定义成员变量 private static DataSource ds; //静态代码块加载配置文件 static { try { Properties prop = new Properties(); InputStream is = DruidJDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"); prop.load(is); ds = DruidDataSourceFactory.createDataSource(prop); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } /** * 获取数据库连接对象 */ public static Connection getConnection() throws SQLException { return ds.getConnection(); } /** * 获取连接池方法 */ public static DataSource getDataSource(){ return ds; } /** * 关闭资源方法 * close()查询sql方法 */ public static void close(ResultSet resultSet, Statement statement, Connection connection) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } /** * 关闭资源方法 * close()增删改sql方法 */ public static void close(Statement statement, Connection connection) {

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值