[英]hibernate throws NullPointerException when calling query.list method
I am trying to get some data from my database by running a query using hibernate. 我试图通过使用hibernate运行查询从我的数据库中获取一些数据。 The data is then stored in a List using query.list method.
然后使用query.list方法将数据存储在List中。 The problem is I am getting a nullPointerException when running the query.list method, which I presume is because the query does not return any data for listing.
问题是我在运行query.list方法时遇到nullPointerException,我认为这是因为查询没有返回任何列表数据。
This is the method that produces the excpetion 这是产生excpetion的方法
public static void getDataForMonth(int yearCount ,int monthCount){
try {
session.beginTransaction();
LocalDate date = LocalDate.of(yearCount, monthCount, 1);
Query query = session.createQuery("select t from Task t where month(t.date) = month(:date) and year(t.date) = year(:date)");
query.setParameter("date", dtf.format(date));
taskList = query.list();
session.getTransaction().commit();
} finally {
session.close();
}
}
and this is the log: 这是日志:
Exception in Application constructor
java.lang.reflect.InvocationTargetException
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:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)
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:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)
Caused by: java.lang.RuntimeException: Unable to construct Application instance: class tadej.Main
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:963)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:844)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:875)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
... 1 more
Caused by: java.lang.NullPointerException
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitGenericFunction(BaseSqmToSqlAstConverter.java:1184)
at org.hibernate.query.sqm.tree.expression.function.SqmGenericFunction.accept(SqmGenericFunction.java:49)
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitRelationalPredicate(BaseSqmToSqlAstConverter.java:1774)
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitRelationalPredicate(BaseSqmToSqlAstConverter.java:213)
at org.hibernate.query.sqm.tree.predicate.RelationalSqmPredicate.accept(RelationalSqmPredicate.java:72)
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitAndPredicate(BaseSqmToSqlAstConverter.java:1752)
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitAndPredicate(BaseSqmToSqlAstConverter.java:213)
at org.hibernate.query.sqm.tree.predicate.AndSqmPredicate.accept(AndSqmPredicate.java:33)
at org.hibernate.query.sqm.consume.spi.BaseSqmToSqlAstConverter.visitQuerySpec(BaseSqmToSqlAstConverter.java:417)
at org.hibernate.sql.ast.produce.sqm.spi.SqmSelectToSqlAstConverter.visitSelectStatement(SqmSelectToSqlAstConverter.java:189)
at org.hibernate.sql.ast.produce.sqm.spi.SqmSelectToSqlAstConverter.interpret(SqmSelectToSqlAstConverter.java:113)
at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:162)
at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:275)
at org.hibernate.query.internal.AbstractQuery.list(AbstractQuery.java:1302)
at tadej.TaskManager.<init>(TaskManager.java:129)
at tadej.ChartPanel.sortDataByDay(ChartPanel.java:47)
at tadej.ChartPanel.<init>(ChartPanel.java:31)
at tadej.PanelManager.<init>(PanelManager.java:12)
at tadej.Main.<init>(Main.java:10)
... 13 more
Exception running application tadej.Main
I have a bunch of similar methods that work by the same principle but with different queries and they work just fine. 我有一堆类似的方法,它们使用相同的原理,但使用不同的查询,它们工作得很好。
For example: 例如:
public static void getDataByDescriptionAndDate(String description, String date) {
try {
session.beginTransaction();
Query query = session.createQuery("select c.id, c.description, c.completion, c.date from Task c where c.description = :desc and c.date = :dt");
query.setParameter("desc", description).setParameter("dt", date);
taskList = query.list();
session.getTransaction().commit();
} finally {
Main.getFactory().getCurrentSession().close();
}
}
I have tried passing the date parameter in a String format but same problem occurs, I have also tried the query in MySQL workbench and it works just fine, so I am really confused why it would not work in the program. 我已经尝试以String格式传递date参数但是同样的问题发生了,我也尝试过MySQL工作台中的查询,它工作正常,所以我真的很困惑为什么它不能在程序中工作。 I am insisting with this query since it is the only one I found that would return the data based on month and year gathered from a date parameter, if anyone knows of a query that would also return me the data I need please tell me.
我坚持这个查询,因为它是我发现的唯一一个将根据从日期参数收集的月份和年份返回数据,如果有人知道一个查询也会返回我需要的数据请告诉我。
Problem solved. 问题解决了。 Apparently the latest version of hibernate does not support month() and year() functions, so I will have to select the required data in java from the list of all data taken from the database, or use a different query.
显然,最新版本的hibernate不支持month()和year()函数,因此我必须从数据库中获取的所有数据列表中选择java中所需的数据,或者使用不同的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.