java.lang.ClassCastException: null 解决

java.lang.ClassCastException: null 解决


今天工作中,遇到一个问题,日志 java.lang.ClassCastException: null 相当懵逼,没有详细堆栈信息。

解决方法

在java启动命令中添加-XX:-OmitStackTraceInFastThrow即可输出详细堆栈信息。

问题原因

JVM(HotSpot JVM)进行了优化。当第一次发生异常(通常为NullPointerException)时,将打印完整的堆栈跟踪,并且JVM会记住堆栈跟踪(或者可能只是代码的位置)。 当该异常经常发生时,将不再打印堆栈跟踪,这既可以实现更好的性能,又不会使相同的堆栈跟踪充满日志。
参看JVM源码(参见下),可见这个优化同时试用于以下异常:

  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • ClassCastException
  • ArrayIndexOutOfBoundsException
  • ArrayStoreException
  • ArithmeticExceptio
    相关核心代码片段:
  // If this throw happens frequently, an uncommon trap might cause  // a performance pothole.  If there is a local exception handler,  // and if this particular bytecode appears to be deoptimizing often,  // let us handle the throw inline, with a preconstructed instance.  // Note:   If the deopt count has blown up, the uncommon trap  // runtime is going to flush this nmethod, not matter what.

  if (treat_throw_as_hot      && (!StackTraceInThrowable || OmitStackTraceInFastThrow)) {// If the throw is local, we use a pre-existing instance and// punt on the backtrace.  This would lead to a missing backtrace// (a repeat of 4292742) if the backtrace object is ever asked// for its backtrace.// Fixing this remaining case of 4292742 requires some flavor of// escape analysis.  Leave that for the future.ciInstance* ex_obj = NULL;switch (reason) {case Deoptimization::Reason_null_check:

      ex_obj = env()->NullPointerException_instance();      break;case Deoptimization::Reason_div0_check:

      ex_obj = env()->ArithmeticException_instance();      break;case Deoptimization::Reason_range_check:

      ex_obj = env()->ArrayIndexOutOfBoundsException_instance();      break;case Deoptimization::Reason_class_check:      if (java_bc() == Bytecodes::_aastore) {

        ex_obj = env()->ArrayStoreException_instance();

      } else {

        ex_obj = env()->ClassCastException_instance();

      }      break;default:      break;

    }

参考
java.lang.ClassCastException: null”原因解决
NullPointerException in Java with no StackTrace

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值