如何处理sql查询中的nullPointer异常

分享于2022年07月17日 jpa kotlin mysql spring-boot 问答
【问题标题】:如何处理sql查询中的nullPointer异常(How to handle nullPointer Exception in sql query)
【发布时间】:2022-01-25 09:29:58
【问题描述】:

我想在两个不同的两列之间执行减法 表,但是当我执行我自己的自定义查询时,查询会抛出 空指针异常。 我在我的应用程序中使用 JPA、MySql、Spring Boot、Kotlin 事情在这个应用程序中运行良好,但我的 sql 查询无法 提供我想要的结果。它在运行时抛出 null 应用。 下面是我在申请时遇到的错误类型

Caused by: java.lang.NullPointerException: null
    at java.base/java.lang.String$CaseInsensitiveComparator.compare(String.java:1244) ~[na:na]
    at java.base/java.lang.String$CaseInsensitiveComparator.compare(String.java:1237) ~[na:na]
    at java.base/java.util.TreeMap.getEntryUsingComparator(TreeMap.java:374) ~[na:na]
    at java.base/java.util.TreeMap.getEntry(TreeMap.java:343) ~[na:na]
    at java.base/java.util.TreeMap.get(TreeMap.java:278) ~[na:na]
    at org.hibernate.dialect.function.SQLFunctionRegistry.findSQLFunction(SQLFunctionRegistry.java:45) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:385) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.tree.IdentNode.getDataType(IdentNode.java:367) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.HqlSqlWalker.lookupProperty(HqlSqlWalker.java:669) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.propertyRef(HqlSqlBaseWalker.java:1159) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:2320) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:2261) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1523) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:602) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:330) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:278) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:276) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:113) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:73) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:162) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:613) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:725) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:114) ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:564) ~[na:na]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:362) ~[spring-orm-5.3.12.jar:5.3.12]
    at com.sun.proxy.$Proxy82.createQuery(Unknown Source) ~[na:na]
    at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:90) ~[spring-data-jpa-2.5.6.jar:2.5.6]
    ... 44 common frames omitted

TransactionMaterial.kt

package com.nilmani.workload.entity

import com.nilmani.workload.enum.Material
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id

@Entity
data class TransactionMaterial(
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    val id :Long=-1,
    val userId:Long=-1,
    val withDrawId:Long=-1,
    val material:Int = Material.NONE.type,
    val depositId:Long=-1,
    val pending:Long=-1,
)

TransactionalRepository.kt

package com.nilmani.workload.repository

import com.nilmani.workload.entity.TransactionMaterial
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.RequestParam

interface TransactionRepository :JpaRepository {
    @Transactional
   @Query(" select WithdrawMaterial .id,(WithdrawMaterial .quantity - DepositMaterial .totalDeposit)  FROM WithdrawMaterial INNER JOIN DepositMaterial ON WithdrawMaterial .id = DepositMaterial .id")
    fun pendingMaterial(@RequestParam("withdraw_id")withdraw_id:Long, @RequestParam("deposit_id")deposit_id:Long):Long?

}

ReqUpdateWithdrawMaterial.kt

package com.nilmani.workload.model.request

import com.nilmani.workload.enum.Material

data class ReqUpdateWithdrawMaterial(
    val id:Long=-1,
    val userId:Long=-1,
    val withDrawId:Long=-1,
    val material:Int = Material.NONE.type,
    val depositId:Long=-1,
    val pending:Long=-1,
)
package com.nilmani.workload.model.request

import com.nilmani.workload.enum.Material

data class ReqUpdateWithdrawMaterial(
    val id:Long=-1,
    val userId:Long=-1,
    val withDrawId:Long=-1,
    val material:Int = Material.NONE.type,
    val depositId:Long=-1,
    val pending:Long=-1,
)

MaterialController.kt

@PostMapping("/updateTransaction")
    fun updateWithdrawMaterial(@ModelAttribute request: ReqUpdateWithdrawMaterial ):ResponseEntity<*>{
        val existingUser = employeeRepository.findById(request.userId)
        if (existingUser.isPresent){
            val existWork = withdrawMaterialRepository.findById(request.withDrawId)
            return if (existWork.isPresent){
                val pendingNearUser = TransactionMaterial(
                    userId = request.userId,
                    withDrawId = request.withDrawId,
                    material = request.material,
                    depositId = request.depositId,
                    pending = transactionMaterialRepository.pendingMaterial(request.withDrawId,request.depositId)
                )
                val saveMaterial = transactionMaterialRepository.save(pendingNearUser)
                return ResponseEntity(saveMaterial,HttpStatus.OK)
            }else{
                ResponseEntity(ResMessage("you need to learn then we provide work you"),HttpStatus.BAD_REQUEST)
            }
        }else{
            return ResponseEntity(ResMessage("You are not a registered User ! Register First"),HttpStatus.NOT_ACCEPTABLE)
        }
    }

我不明白为什么查询会抛出空指针异常 我正在使用 Mac Os,IntelIj 代码编辑器的想法

WithdrawMaterial 表 How to handle nullPointer Exception in sql query

DepositMaterial 表 How to handle nullPointer Exception in sql query

我希望 TransactionMaterial 表中的结果如下表所示 How to handle nullPointer Exception in sql query


【解决方案1】:

您的加入查询错误。将查询的连接部分修改为:

' ... FROM WithdrawMaterial INNER JOIN DepositMaterial ON WithdrawMaterial.id = DepositMaterial.with_draw_id'

在您自己的代码中,在 WithdrawMaterial 表中,ID 为 7 和 8,而 DepositMaterial 中没有这样的 id,因此您的联接和查询没有结果。