org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'zkCuratorConfig': Unsatisfied dependency expressed through field 'baseSleepTimeMs'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [@org.springframework.beans.factory.annotation.Value int] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1402)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:514)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:319)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:866)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'null' to required type 'int'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [@org.springframework.beans.factory.annotation.Value int] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:79)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1224)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1196)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:595)
... 19 common frames omitted
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [null] to type [@org.springframework.beans.factory.annotation.Value int] for value 'null'; nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
at org.springframework.core.convert.support.GenericConversionService.assertNotPrimitiveTargetType(GenericConversionService.java:334)
at org.springframework.core.convert.support.GenericConversionService.handleResult(GenericConversionService.java:327)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:183)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:243)
at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:73)
... 22 common frames omitted
Caused by: java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
... 27 common frames omitted
错误信息表示Spring试图将一个null
值赋给一个基本类型的变量,但是在Java中,基本类型(如int
,boolean
,char
,double
等)不能接受null
值
在使用了@Value
注解来从配置文件中注入一个int
类型的值,但是对应的配置项在配置文件中不存在,或者其值为null
,所以Spring在尝试将null
赋给一个int
类型的变量时抛出了错误。
@Value("${some.config.key}")
private int configValue;
如果some.config.key
在配置文件中不存在或其值为null
,那么Spring就会抛出这个错误。
为了解决这个问题,将字段的类型从基本类型(int
)更改为其包装类(Integer
)。包装类型可以接受null
值。
@Value("${some.config.key:#{null}}")
private Integer configValue;
今天使用jdbctemplate 查询数据封装到 实体里面时出现以下错误,记录一下。
错误信息:
org.springframework.beans.TypeMismatchException:
Failed to convert property value of type 'null' to required type 'float' for property 'longitude’;
There was an unexpected error (type=Bad Request, status=400).
Failed to convert property value of type 'null' to required type 'int' for property 'age';
nested exception is org.spring...
标题一、报错:org.springframework.beans.TypeMismatchException: Failed to convert property value of type ‘null’ to required type ‘double’ for property ‘bonus’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from t
Caused by: org.springframework.core.convert.ConversionFailedException:
Failed to convert from type [null] to type [int] for value 'null';
nested exception is java.lang.IllegalArgumentException:
A null value cannot be assigned to a primitive type
提示: .
如:int 用Integer代替
double 用Double代替
详解看:https://blog.csdn.net/kevinzhangfei/article/details/6995316
原因:数据库所查询的这张表中有一个字段是基本数据类型,而这个字段值为null导致的。
在网上搜了一下,基本上是这样解释的“一般原因是通过数据库给简单类型赋值了Null,因为在数据库中该属性对应字段可以为空,但是简单类型不能为空,所以应该把,数据库中字段值改为能够被赋的值如0,1等,或者该变属性为封装类”。
Null safety 可以帮助开发者避免一些日常开发中很难发现的错误,并且额外的好处是可以改善性能
Flutter 2.2.0 (2021年5月19日发布) 之后的版本都要求使用 null safety
int age = 123;
age = null; //编译报错 A value of type 'Null' can't be assigned to a variable of type 'int'
翻译成中文的意思是空安全, 可以帮助开发者避免一些日常开发中很难被发现的错误,并且额外的好处是可以改善性能,Flutter2.2.0(2021年5月19日发布) 之后的版本都要求使用null safety。最开始 @required 是注解,在它已经作为内置修饰符。主要用于允许根据需要标记任何命名参数(函数或类),使得它们不为空。因为可选参数中必须有个 required 参数或者该参数有个默认值。equired翻译成中文的意思是需要、依赖。关键字主要用于延迟初始化。
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net
The termslvalueandrvalueare not something one runs into often in C/C++ programming, but when one does, it's usually not i...
nested exception is java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type
在Entity里将long 改为Long,int 改为Integer