搜索词>>NullPointerException 耗时0.0020
  • Java编程中如何避免NullPointerException

    Java编程中如何避免NullPointerException(空指针异常)Java编程中如何避免NullPointerException(空指针异常)
  • Dubbo 整合 quartz NullPointerException错误解决

    环境信息dubbo 2.6.5spring-boot-starter-quartz 2.0.6.RELEASEspring boot 2.0.6.RELEASENPE错误信息org.quartz.SchedulerException: Jo环境信息dubbo 2.6.5spring-boot-starter-quartz 2.0.6.RELEASEspring boot 2.0.6.RELEASENPE错误信息org.quartz.SchedulerException: Job instantiation failed at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:47) ~[spring-context-support-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127) ~[quartz-2.3.0.jar:na] at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:392) [quartz-2.3.0.jar:na] Caused by: java.lang.NullPointerException: null at com.alibaba.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor.postProcessBeforeInitialization(DubboConfigBindingBeanPostProcessor.java:79) ~[dubbo-2.6.5.jar:2.6.5] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1686) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:407) ~[spring-beans-5.0.10.RELEASE.jar:5.0.10.RELEASE] at org.springframework.boot.autoconfigure.quartz.AutowireCapableBeanJobFactory.createJobInstance(AutowireCapableBeanJobFactory.java:45) ~[spring-boot-autoconfigure-2.0.6.RELEASE.jar:2.0.6.RELEASE] at org.springframework.scheduling.quartz.AdaptableJobFactory.newJob(AdaptableJobFactory.java:43) ~[spring-context-support-5.0.10.RELEASE.jar:5.0.10.RELEASE] ... 2 common frames omitted ​NullPointerException 原因分析spring-boot-autoconfigure-2.0.2.RELEASE.jar->AutowireCapableBeanJobFactory.java    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {        Object jobInstance = super.createJobInstance(bundle);        this.beanFactory.autowireBean(jobInstance);        this.beanFactory.initializeBean(jobInstance, (String)null);        return jobInstance;   }注意,上面创建job实列时候,赋值的bean的name为 `nulldubbo-2.6.5.jar>DubboConfigBindingBeanPostProcessor.java    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        if (beanName.equals(this.beanName) && bean instanceof AbstractConfig) {            AbstractConfig dubboConfig = (AbstractConfig)bean;            this.bind(this.prefix, dubboConfig);            this.customize(beanName, dubboConfig);       } ​        return bean;   }提示,dubbo在使用beanName的时候未作空验证,导致NullPointerException解决办法根据dubbo的说法升级spring boot 版本,参考https://github.com/apache/dubbo/issues/2429    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.3.5.RELEASE</version>        <relativePath/>    </parent>测试通过,升级到 2.1.9.RELEASE , 2.1.18.RELEASE ,2.3.5.RELEASEapplication.properties 文件增加配置spring.main.allow-bean-definition-overriding=true提示:如果application.properties文件不添加上方配置可能在启动时候报错当dubbo版本为(2.6.9)时候,2.6.5版本不会
  • java处理RSA非对称加解密

    java处理RSA非对称加解密 <pre> <code class="language-java">package org.xqlee.utils.security; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.security.InvalidKeyException; import java.security.Key; import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import org.apache.commons.codec.binary.Base64; /** * * * <pre> * _________________________INFO_____________________________ * | Description : [RAS加密/解密工具类|其他说明,非对称加密速度非常慢,内容过大一般使用对称加密,然后对对称加密的密文进行非对称加密] * | Encoding : [UTF-8] * | Package : [org.xqlee.utils.security] * | Project : [utils] * | Author : [LXQ] * | CreateDate : [] * | Updater : [] * | UpdateDate : [] * | UpdateRemark: [] * | Company : [www.zhljc.com] * | Version : [v 1.0] * | Libs : [commons-codec-1.x.jar] * __________________________________________________________ * </pre> */ public class RSAUtil { /** 换行符 **/ private final static String lineSeparator = System.getProperty("line.separator", "\n"); /** 加解密算法键值-这里RSA **/ private final static String KEY_ALGORITHM = "RSA"; /** 获取公钥的KEY **/ public final static String PUBLIC_KEY = "RSAPublicKey"; /** 获取私钥的KEY **/ public final static String PRIVATE_KEY = "RSAPrivateKey"; /** * 生成RSA算法的密钥对 * * @return 密钥对,Map-> key=RSAPublicKey|KEY=RSAPrivateKey * @throws NoSuchAlgorithmException * 获取密钥对可能发生的异常 */ public static Map<String, Object> genKeyPair() throws NoSuchAlgorithmException { KeyPairGenerator kGenerator = KeyPairGenerator.getInstance(KEY_ALGORITHM); // init the keyPair KeyPair keyPair = kGenerator.generateKeyPair(); // get public key RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // get private key RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String, Object> keys = new HashMap<String, Object>(); keys.put(PUBLIC_KEY, publicKey); keys.put(PRIVATE_KEY, privateKey); return keys; } /** * 加密 * * @param key * 加密使用的[公钥/私钥] * @param input * 需要加密的明文 * @return 加密后的密文 * @throws Exception * 加密过程中可能发生的异常 */ public static byte[] encrypt(Key key, byte[] input) throws Exception { if (key != null) { // Cipher负责完成加密或解密工作,基于RSA Cipher cipher; try { cipher = Cipher.getInstance(KEY_ALGORITHM); // 根据公钥,对Cipher对象进行初始化 cipher.init(Cipher.ENCRYPT_MODE, key); byte[] resultBytes = cipher.doFinal(input); return resultBytes; } catch (NoSuchAlgorithmException e) { throw new Exception("无此解密算法:" + e.getMessage()); } catch (NoSuchPaddingException e) { throw new Exception("加密过程中发生异常:" + e.getMessage()); } catch (InvalidKeyException e) { throw new Exception("解密私钥非法,请检查:" + e.getMessage()); } catch (IllegalBlockSizeException e) { throw new Exception("密文长度非法:" + e.getMessage()); } catch (BadPaddingException e) { throw new Exception("密文数据已损坏:" + e.getMessage()); } } else { throw new Exception("解密私钥为空, 请设置"); } } /** * 解密 * * @param key * 对应解密的[公钥或私钥] * @param input * 需要解密的加密信息 * @return 解密后的明文信息 * @throws Exception * 解密过程中可能发生的异常 */ public static byte[] decrypt(Key key, byte[] input) throws Exception { if (key == null) { throw new Exception("解密私钥为空, 请设置"); } Cipher cipher = null; try { // Cipher负责完成加密或解密工作,基于RSA cipher = Cipher.getInstance(KEY_ALGORITHM); // 根据私钥,对Cipher对象进行初始化 cipher.init(Cipher.DECRYPT_MODE, key); byte[] output = cipher.doFinal(input); return output; } catch (NoSuchAlgorithmException e) { throw new Exception("无此解密算法:" + e.getMessage()); } catch (NoSuchPaddingException e) { throw new Exception("解密过程中发生异常:" + e.getMessage()); } catch (InvalidKeyException e) { throw new Exception("解密私钥非法,请检查:" + e.getMessage()); } catch (IllegalBlockSizeException e) { throw new Exception("密文长度非法:" + e.getMessage()); } catch (BadPaddingException e) { throw new Exception("密文数据已损坏:" + e.getMessage()); } } /** * 通过文件获取私钥对象 * * @param file * 私钥文件 * @return 私钥对象 * @throws Exception * 从文件私钥获取私钥Java对象过程中可能发生的异常 */ public static PrivateKey getPrivateKey(File file) throws Exception { String strPrivateKey = null; try { BufferedReader br = new BufferedReader(new FileReader(file)); String readLine = null; StringBuilder sb = new StringBuilder(); while ((readLine = br.readLine()) != null) { sb.append(readLine); sb.append(lineSeparator); } br.close(); // 字符 strPrivateKey = sb.toString().substring(0, sb.toString().length() - lineSeparator.length()); } catch (IOException e) { throw new Exception("私钥数据流读取错误:" + e.getMessage()); } try { byte[] buffer = Base64.decodeBase64(strPrivateKey.getBytes()); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); return keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("=====无此算法====="); } catch (InvalidKeySpecException e) { throw new Exception("=====密钥非法====="); } catch (NullPointerException e) { throw new Exception("=====私钥数据为空====="); } } /** * 通过字符串获取私钥对象 * * @param strPrivateKey * 字符串的私钥 * @return 私钥对象 * @throws Exception * 从字符串私钥获取私钥Java对象过程中可能发生的异常 */ public static PrivateKey getPrivateKey(String strPrivateKey) throws Exception { try { byte[] buffer = Base64.decodeBase64(strPrivateKey.getBytes()); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(buffer); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); return keyFactory.generatePrivate(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("=====无此算法====="); } catch (InvalidKeySpecException e) { throw new Exception("=====密钥非法====="); } catch (NullPointerException e) { throw new Exception("=====私钥数据为空====="); } } /** * 从公钥文件中加载获取公钥Java对象 * * @param file * 公钥文件 * @return Java公钥对象 * @throws Exception * 获取过程中可能发生的异常 */ public static PublicKey getPublicKey(File file) throws Exception { String strRSAPublicKey = null; try { BufferedReader br = new BufferedReader(new FileReader(file)); String readLine = null; StringBuilder sb = new StringBuilder(); while ((readLine = br.readLine()) != null) { sb.append(readLine); sb.append(lineSeparator); } br.close(); strRSAPublicKey = sb.toString().substring(0, sb.toString().length() - lineSeparator.length()); } catch (IOException e) { throw new Exception("公钥数据流读取错误"); } catch (NullPointerException e) { throw new Exception("公钥输入流为空"); } try { byte[] buffer = Base64.decodeBase64(strRSAPublicKey.getBytes()); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); return keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("=====无此算法====="); } catch (InvalidKeySpecException e) { throw new Exception("=====公钥非法====="); } catch (NullPointerException e) { throw new Exception("=====公钥数据为空====="); } } /** * 从公钥字符串加载获取公钥Java对象 * * @param strPublicKey * 公钥字符串 * @return Java公钥对象 * @throws Exception * 获取过程中可能发生的异常 */ public static PublicKey getPublicKey(String strPublicKey) throws Exception { try { byte[] buffer = Base64.decodeBase64(strPublicKey.getBytes()); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); return keyFactory.generatePublic(keySpec); } catch (NoSuchAlgorithmException e) { throw new Exception("=====无此算法====="); } catch (InvalidKeySpecException e) { throw new Exception("=====公钥非法====="); } catch (NullPointerException e) { throw new Exception("=====公钥数据为空====="); } } /** * 将Java对象的公钥或者私钥转换为字符串 * * @param key * 公钥/私钥 * @return 秘钥的字符串 */ public static String key2String(Key key) { byte[] keyBytes = key.getEncoded(); String result = new String(Base64.encodeBase64(keyBytes)); return result; } } </code></pre>