NullPointerException: inStream parameter is null问题的解决与待完善

NullPointerException: inStream parameter is null问题的解决与待完善

java.lang.ExceptionInInitializerError
	at itcast.Jedis.JedisTest.test08(JedisTest.java:111)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.NullPointerException: inStream parameter is null
	at java.base/java.util.Objects.requireNonNull(Objects.java:246)
	at java.base/java.util.Properties.load(Properties.java:403)
	at itcast.Utils.DruidUtils.<clinit>(DruidUtils.java:20)
	... 23 more

问题解决

  • property文件位置问题
    应处于src目录下

  • 在撰写JedisUtils工具类时

//会出现NullPointerException: inStream parameter is null 去掉类加载器后成功
//InputStream is = JedisUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
InputStream is = JedisUtils.class.getResourceAsStream("jedis.properties");

撰写test验证成功,去除getClassLoader

存在问题

同时 撰写DruidUtils 数据库调用工具类时 需要加上getClassLoader
否则会出现如上图1异常

//需要加上getClassLoader()
InputStream is = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");

在后面Maven中为了解决配置IDEA里的jdk要与你本地下载的jdk一致时调试程序如下述博客java:错误不支持发行版本5

再次进行上述问题调试后发现test中也要加入类加载器
类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象

  • test代码如下
	@Test
    public void test07(){
     
        Jedis jedis = JedisUtils.getJedis();
        jedis.set("password","123");
        String username = jedis.get("username");
        System.out.println(username);
    }

	@Test
    public void test08() throws SQLException {
     
        Connection connection = DruidUtils.getConnection();
        //成功
        System.out.println(connection);
    }

验证成功! 但仍然不知道该问题出现原因,拟学习JVM虚拟机之后回顾
参考Java获取不同路径下的配置文件

你可能感兴趣的:(Java,java,exception,property)