简体   繁体   English

class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load

[英]class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load

I am trying to write a handler for a get request in Spring but I am getting this error:我正在尝试为 Spring 中的 get 请求编写一个处理程序,但出现此错误:

class java.math.BigInteger cannot be cast to class java.lang.String (java.math.BigInteger and java.lang.String are in module java.base of loader 'bootstrap'). class java.math.BigInteger 无法转换为 class java.lang.String(java.math.BigInteger 和 java.lang.String 在模块 '882132848486945' 中)。

please check my controller class请检查我的 controller class

@GetMapping("getListOfStudentsBasedOnTrainerId/{trainerId}")
public ResponseEntity<List<TrainerIdResponse>>  getListOfStudentsBasedOnTrainerId( @PathVariable int trainerId) 
        throws RecordNotFoundException{
    
    List<TrainerIdResponse> trainerIdresp= userService.getListOfStudentsBasedOnTrainerId(trainerId);
    return new ResponseEntity<List<TrainerIdResponse>>(trainerIdresp,new HttpHeaders(), HttpStatus.OK);
    
}

this is user service method这是用户服务方法

 public List<TrainerIdResponse> getListOfStudentsBasedOnTrainerId(@PathVariable int trainerId) throws RecordNotFoundException{
        
        List<Object> trainerIdResponse=searchRepository.getListOfStudentsBasedOnTrainerId(trainerId);
        Iterator it =trainerIdResponse.iterator();
         List<TrainerIdResponse> trainerIdList  =new ArrayList<>();
         while(it.hasNext()) {
             Object[] row=(Object[])it.next();
             TrainerIdResponse trainerIdResponse1= new TrainerIdResponse();
             trainerIdResponse1.setStudentid(Integer.valueOf((Integer)row[0]).intValue());
             trainerIdResponse1.setStudentname(String.valueOf(row[1]));
             trainerIdResponse1.setStudentImage(String.valueOf(row[2]));
             trainerIdResponse1.setEnrolleddate(String.valueOf(row[3]));
             trainerIdResponse1.setHighereducation(String.valueOf(row[4]));
             trainerIdResponse1.setUniversity(String.valueOf(row[5]));
             trainerIdResponse1.setEmployement(String.valueOf(row[6]));
             trainerIdResponse1.setLocation(String.valueOf(row[7]));
             trainerIdResponse1.setTrainerid(Integer.valueOf((Integer)row[8]).intValue());
             trainerIdList.add(trainerIdResponse1);
             
             
         }
        
        
        return trainerIdList;
        
    }

my search repository class我的搜索库 class

   @Query(value ="select u.userid as \"studentid\",u.username as \"studentname\",u.Image_url as \"studentImage\",u.addeddate as \"enrolleddate\", u.highestqualification as \"highereducation\",\r\n"
            + "u.college as \"university\",e.employmenttype as \"employement\",e.location,\r\n"
            + "ur.requestedtoid as \"trainerid\" from users u JOIN userexperience e  \r\n"
            + "on u.userid = e.userid  join userrequests ur on u.userid = ur.requestedtoid \r\n"
            + "where u.userid = ur.requestedtoid or u.userid =:trainerId",nativeQuery = true)
    List<Object> getListOfStudentsBasedOnTrainerId(int trainerId);

my response class我的回复 class

import java.util.Date;

public class TrainerIdResponse {

    private int studentid;
    private String studentname;
    private String studentImage;
    private String enrolleddate;
    private String highereducation;
    private String university;
    private String employement;
    private String location;
    private int trainerid;
    
    
    public int getStudentid() {
        return studentid;
    }
    public void setStudentid(int studentid) {
        this.studentid = studentid;
    }
    public String getStudentname() {
        return studentname;
    }
    public void setStudentname(String studentname) {
        this.studentname = studentname;
    }
    public String getStudentImage() {
        return studentImage;
    }
    public void setStudentImage(String studentImage) {
        this.studentImage = studentImage;
    }
    public  String getEnrolleddate() {
        return enrolleddate;
    }
    public void setEnrolleddate(String enrolleddate) {
        this.enrolleddate = enrolleddate;
    }
    public String getHighereducation() {
        return highereducation;
    }
    public void setHighereducation(String highereducation) {
        this.highereducation = highereducation;
    }
    public String getUniversity() {
        return university;
    }
    public void setUniversity(String university) {
        this.university = university;
    }
    public String getEmployement() {
        return employement;
    }
    public void setEmployement(String employement) {
        this.employement = employement;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public int getTrainerid() {
        return trainerid;
    }
    public void setTrainerid(int trainerid) {
        this.trainerid = trainerid;
    }
    
    
    
}

There's a lot of "noise" in the comments, hence this "response":评论中有很多“噪音”,因此这个“回应”:

  1. Q: What is the actual error message?问:实际的错误信息是什么?
  • Your title:你的题目:

class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load class java.math.BigInteger cannot be cast to class java.lang.Integer (java.math.BigInteger and java.lang.Integer are in module java.base of load

  • Your body:你的身体:

class java.math.BigInteger cannot be cast to class java.lang.String (java.math.BigInteger and java.lang.String are in module java.base of loader 'bootstrap'). class java.math.BigInteger 无法转换为 class java.lang.String(java.math.BigInteger 和 java.lang.String 在模块 '882132848486945' 中)。

??Which is it???? ??这是什么????

  1. Q: What line of is causing the error?问:是哪一行导致了错误?

    • That's an important reason you're being asked for the stack traceback: the traceback will show where in the source code the error is occurring.这是要求您提供堆栈回溯的一个重要原因:回溯将显示源代码中发生错误的位置。
    • POSSIBLE GUESSES:可能的猜测:

trainerIdResponse1.setStudentid(Integer.valueOf((Integer)row[0]).intValue()); trainerIdResponse1.setTrainerid(Integer.valueOf((Integer)row[8]).intValue());

  1. Q: If the guess is correct, what are the data types of row[0] and row[8] in the underlying database? Q:如果猜测正确,底层数据库中row[0]和row[8]的数据类型是什么? Do they correspond to Java "BigInteger"?它们是否对应于 Java“BigInteger”?

  2. You CANNOT cast "BigInteger" to an Integer. It's possible that's your entire problem.不能将“BigInteger”转换为 Integer。这可能是您的全部问题。 You MAY use BigInteger.intValue() .可以使用BigInteger.intValue()

EXAMPLE: int myInt = myBigInteger.intValue();示例: int myInt = myBigInteger.intValue();

  1. Finally, if the underlying value in the database indeed corresponds to a Java BigInteger, you CANNOT use "intValue()" to read it.最后,如果数据库中的基础值确实对应于 Java BigInteger,则您不能使用“intValue()”来读取它。 You'll probably need getBigDecimal() .您可能需要getBigDecimal()

Please let us know if any of this information helped.请让我们知道这些信息是否有帮助。

And please "edit" your post with all the requested information.并请使用所有要求的信息“编辑”您的帖子。


Per the OP根据 OP

private int studentid;私人学生身份; inside database this is bigserial data type and trainerid this is inside db is int8在数据库内部,这是 bigserial 数据类型,而在 db 内部,trainerid 是 int8

So this is the necessary code:所以这是必要的代码:

BigInteger studentId = row[0].getBigDecimal();
trainerIdResponse1.setStudentid(studentId.intValue());
Long trainerId = row[8].getLong();
trainerIdResponse1.setTrainerid(trainerId.intValue()).

NOTE: both are narrowing conversions - you risk truncating data.注意:两者都在缩小转化率——您可能会截断数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Integer不能强制转换为java.math.BigInteger - java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能转换为 java.lang.Integer - java.math.BigInteger cannot be cast to java.lang.Integer 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer? - How to work around java.math.BigInteger cannot be cast to java.lang.Integer? 如何解决 java.math.BigInteger 无法转换为 java.lang.Integer - How to work around java.math.BigInteger cannot be cast to java.lang.Integer Playorm java.lang.Integer无法强制转换为java.math.BigInteger - Playorm java.lang.Integer cannot be cast to java.math.BigInteger java.math.BigInteger 不能强制转换为 java.lang.Integer,ZAC5C24B64B4BAC83 - java.math.BigInteger cannot be cast to java.lang.Integer , sql Dialect is not configured java.lang.ClassCastException:无法将java.lang.Integer强制转换为java.math.BigInteger HTTP状态500 - java.lang.ClassCastException: java.lang.Integer cannot be cast to java.math.BigInteger HTTP Status 500 java.math.BigInteger 无法转换为 java.lang.Long - java.math.BigInteger cannot be cast to java.lang.Long java.lang.Long无法强制转换为java.math.BigInteger - java.lang.Long cannot be cast to java.math.BigInteger class java.math.BigInteger 无法转换为 class java.lang.Boolean - class java.math.BigInteger cannot be cast to class java.lang.Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM