gpt4 book ai didi

java - 如何解决 HibernateTemplate 的 find(String query) 方法抛出的 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:45:26 27 4
gpt4 key购买 nike

如何解决这个 NullPointerException ?一点背景知识,我正在开发一个基于 Spring 的项目,并在 DAO 层使用 HibernateTemlate 来完成所有数据库相关操作。

这是我从抛出 NullPointerException 的测试类中提取的代码片段。


try {
List<Object[]> list = (List<Object[]>) ht.find("select uomId,uomModel from in.nit.model.Uom");
System.out.println(list);
}catch(NullPointerException e) {
e.printStackTrace();
}

这是控制台上异常的堆栈跟踪。

```
java.lang.NullPointerException
at in.nit.dao.impl.UomDaoImplTest.test(UomDaoImplTest.java:28)
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:567)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
```

Here is my model class

```
@Entity
@Table(name="uomtab")
public class Uom {

@Id @GeneratedValue @Column(name="umid") private Integer uomId;
@Column(name="utype") private String uomType;
@Column(name="umodel") private String uomModel;
@Column(name="udesc") private String uomDesc;

public Uom() {
super();
}

public Uom(Integer uomId) {
this.uomId = uomId;
}

public Integer getUomId() {
return uomId;
}
public void setUomId(Integer uomId) {
this.uomId = uomId;
}
public String getUomType() {
return uomType;
}
public void setUomType(String uomType) {
this.uomType = uomType;
}
public String getUomModel() {
return uomModel;
}
public void setUomModel(String uomModel) {
this.uomModel = uomModel;
}
public String getUomDesc() {
return uomDesc;
}
public void setUomDesc(String uomDesc) {
this.uomDesc = uomDesc;
}
@Override
public String toString() {
return "Uom [uomId=" + uomId + ", uomType=" + uomType + ", uomModel=" + uomModel + ",
uomDesc=" + uomDesc + "]";
}
}

```

此外,尽管 find(String Query) 方法已被弃用,但我在项目的其他 dao 类中广泛使用了它,它工作顺利且良好。

此异常导致我的代码崩溃的可能原因是什么?

最佳答案

  1. 你不应该捕获 NullPointerException
  2. 代码中的第 28 行是什么?
  3. 您尝试过调试线路吗?如果是 ht.find 行,唯一有意义的是您的 HibernateTemplate 为 null 并且尚未正确设置。

关于java - 如何解决 HibernateTemplate 的 find(String query) 方法抛出的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60873714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com