[英]java : Class cast exception
I have a map with strings and tupleList. 我有一个包含字符串和tupleList的地图。 i am trying to get the tupleList into double array , but i am getting type cast exception.
我试图让tupleList成为双数组,但我得到了类型转换异常。 my code is --
我的代码是 -
Map<String, TupleList> results = null; -- Some data in it and TupleList has int or double value.
public void drawGraph(){ public void drawGraph(){
Object[] test = new Object[results.size()];
int index = 0;
for (Entry<String, TupleList> mapEntry : results.entrySet()) {
test[index] = mapEntry.getValue();
index++;
}
BarChart chart = new BarChart();
chart.setSampleCount(4);
String[] values = new String[test.length];
for(int i = 0; i < test.length; i++)
{
values[i] = (String) test[i];
}
// double[] values = new double[] {32,32,65,65};
String[] sampleLabels = new String[] {"deny\nstatus", "TCP\nrequest", "UPD\nrequest", "ICMP\nrequest"};
String[] barLabels = new String[] {"STATUS", "TCP", "UDP", "PING"};
//chart.setSampleValues(0, values);
chart.setSampleColor(0, new Color(0xFFA000));
chart.setRange(0, 88);
chart.setFont("rangeLabelFont", new Font("Arial", Font.BOLD, 13));
error---- 错误 - -
java.lang.ClassCastException: somePackagename.datamodel.TupleList cannot be cast to java.lang.String
at com.ibm.biginsights.ExampleAPI.drawGraph(ExampleAPI.java:177)
at com.ibm.biginsights.ExampleAPI.main(ExampleAPI.java:95)
i am getting exception @ 我异常@
String[] values = new String[test.length];
for(int i = 0; i < test.length; i++)
{
values[i] = (String) test[i];
Thanks 谢谢
I am assuming that the error is taking place here: values[i] = (String) test[i];
我假设错误发生在这里:
values[i] = (String) test[i];
. 。 The problem is that you are trying to throw an object of type
TupleList
into a string. 问题是您正在尝试将
TupleList
类型的对象抛出到字符串中。 What you need to do, is to call the .toString()
method, that should give you a string representation of the object. 你需要做的是调用
.toString()
方法,它应该给你一个对象的字符串表示。
Note however, you will have to override the toString()
method within the TupleList
class so that you can get a string representation of the object which suits your needs. 但是请注意,您必须覆盖
TupleList
类中的toString()
方法,以便您可以获得适合您需要的对象的字符串表示。
So in short, just doing test[i].toString()
will most likely yield something along the lines of this: TupleList@122545
. 所以简而言之,只是做
test[i].toString()
很可能会产生这样的东西: TupleList@122545
。 What you need to do is this: 你需要做的是:
public class TupleList
...
@Override
public String toString()
{
return "...";
}
...
Obviously your test array contains TupleLists. 显然,您的测试数组包含元组列表。 You add them at
你添加它们
Object[] test = new Object[results.size()];
int index = 0;
for (Entry<String, TupleList> mapEntry : results.entrySet()) {
test[index] = mapEntry.getValue();
index++;
}
And then you cast TupleList to String ang get ClassCastException You could use toString if you want. 然后你将TupleList转换为String ang get ClassCastException你可以使用toString。
values[i] = test[i].toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.