java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String
我在下面的代码中得到了这个例外:
String colValue= (String)data_ForSave.elementAt(r).get(ColValindex);
其中ForSave
是包含表值的String的向量,我无法理解为什么抛出此异常。
以下是该摘要的完整堆栈跟踪。
堆叠异常追踪
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String
at Utilities.Utility.DataHeaderMapping(Utility.java:1122)
at jewelleryerpapplication.GUI.ProductsInformation.jbtnSaveActionPerformed(ProductsInformation.java:4222)
at jewelleryerpapplication.GUI.ProductsInformation.access$1500(ProductsInformation.java:40)
at jewelleryerpapplication.GUI.ProductsInformation$37.actionPerformed(ProductsInformation.java:3292)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Row : 0
提前答复,因为我坚持使用它。
答案 0 :(得分:4)
从您的评论中看来,您使用的是不正确的通用类型。
e.g。
Vector g = new Vector();
List list = new ArrayList();
list.add(1.0f);
g.add(list);
Vector<List<String>> grid = g; // incorrect cast, but only a warning.
String s = g.elementAt(0).get(0); // throws a class cast exception.
BTW:我不知道为什么你会使用elementAt(index)和get(index)的组合,因为这些是旧的(pre Java 1.2)和更新的样式(post Java 1.2),除非Swing需要它。 ;)
我会用它来反转不正确的泛型类型,虽然修改类型会更好。
List list = (List) data_ForSave.elementAt(r);
String colValue= String.valueOf(list.get(ColValindex));
因为你不能将Float转换为String。你可以把它变成一个字符串。
答案 1 :(得分:4)
问题是data_ForSave.elementAt(r).get(ColValindex)
返回的是Float
,而不是String
。如果您想将其格式化为字符串 - 例如,您希望3.45
转换为"3.45"
,那么您可以写:
String colValue= data_ForSave.elementAt(r).get(ColValindex).toString();
答案 2 :(得分:1)
将data_ForSave
的声明更改为
Vector<String> data_ForSave = new Vector<String>();
并且您的IDE应该通过错误或警告指导您解决此问题的根源。
答案 3 :(得分:0)
根据你的评论,我看到的是ForSave矢量不是通用的。因此,当您尝试在没有验证实例的情况下进行投射时,它会给您一个错误。我的建议是:
String colValue = "";
if(data_ForSave.elementAt(r).get(ColValindex) instaceof Float)
colValue = String.valueOf(data_ForSave.elementAt(r).get(ColValindex));
else
colValue = data_ForSave.elementAt(r).get(ColValindex);