奇怪的Java转换异常。为什么我不能投放长的浮子? [英] Strange Java cast exception. Why can't I cast Long to a Float?

查看:101
本文介绍了奇怪的Java转换异常。为什么我不能投放长的浮子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能将Long转换为浮点值?



我收到此错误消息:


java.lang.ClassCastException:java.lang.Long无法转换为java.lang.Float


为什么这是一个问题?我想要投射的数字是域[-10.0,10.0]中的小数。它们作为使用 JFormattedTextField.getValue()返回的Object实例开始。



堆栈跟踪:



 线程AWT-EventQueue-0中的异常java.lang.ClassCastException:java.lang.Long不能转换为java.lang.Float 
at submodeler.animation.Timeline.setKeyedAttribute(Timeline.java:59)
at submodeler.ui.attributes.TransformationAttributePanel.actionPerformed(TransformationAttributePanel.java:247)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton $ Handler.actionPerformed(AbstractButton.java:2351)
在javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
在javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6348)
at javax.swing.JComponent .processMouseEvent(JComponent.java:3267)
在java.awt.Component.processEvent(Component.java:6113)
在java.awt.Container.processEvent(Container.java:2085)
在java.awt.Component.dispatchEventImpl(Component.java:4714)
在java.awt.Container.dispatchEventImpl(Container.java:2143)
在java.awt.Component.dispatchEvent(Component。 java:4544)
在java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
在java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
在java.awt .LightweightDispatcher.dispatchEvent(Container.java:4212)
在java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2475)
at java.awt.Component.dispatchEvent(Component.java:4544)
在java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
在java.awt.EventDispatchThread.pumpOneEventForFilters( EventDispatchThread.java:296)
在java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
在java .awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
在java.awt.EventDispatchThread.run(EventDispatchThread.java:122 )


解决方案

/ p>

否则,标准解决方案是用 someLongValue.floatValue()替换(Float)someLongValue



如果你正在处理原始类型,你可以只是从long转换为float,虽然它是一个 5.1.2扩大基本转换,但可能会失去精确度,所以要小心!显然,您有包装类型,不能隐式转换,因此你得到classcastexception。这可能是因为自动装箱,或显式的长对象创建。



一些更多的未被邀请的建议:如果你的有效值是-10到+10范围内的小数,标准数据类型是 int (primitive)。避免浮动,如果你的意思是精确的数字。 long 也不是最优的,因为它不是完全原子的,像int,它需要2倍的内存。如果允许不同的状态未分配,则 Integer 可能为null,也可以。


Why can't I cast Long to a Float?

I get this error message:

java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float

Why is this a problem? The numbers that I'm trying to cast are decimals in the domain [-10.0, 10.0]. They start out as Object instances returned using JFormattedTextField.getValue(). But they must be converted to floats.

Stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Float
    at submodeler.animation.Timeline.setKeyedAttribute(Timeline.java:59)
    at submodeler.ui.attributes.TransformationAttributePanel.actionPerformed(TransformationAttributePanel.java:247)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6348)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6113)
    at java.awt.Container.processEvent(Container.java:2085)
    at java.awt.Component.dispatchEventImpl(Component.java:4714)
    at java.awt.Container.dispatchEventImpl(Container.java:2143)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4618)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.java:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2475)
    at java.awt.Component.dispatchEvent(Component.java:4544)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:635)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

解决方案

It would help if you provide a stacktrace.

Otherwise, the standard solution is to replace (Float) someLongValue with someLongValue.floatValue()

If you are dealing with primitive types, you can just cast from long to float, although it is a 5.1.2 Widening Primitive Conversion, but one that may lose precision, so be careful! Obviously you have the wrapper type Long, which cannot implicitly be converted, thus you get the classcastexception. This may be because of autoboxing, or explicit Long object creation.

Some more uninvited advice: if your valid values are decimals in the range of -10 to +10 the standard data type is int (primitive). avoid float if you mean exact numbers. long is also not optimal, because it is not fully atomic like int and it takes 2x the memory. If you allow a different state "not assigned" then Integer, which may take null, is also OK.

这篇关于奇怪的Java转换异常。为什么我不能投放长的浮子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆