【发布时间】:2018-11-04 22:17:09
【问题描述】:
我是 Java 新手,但对编程并不陌生,所以作为我的第一个项目,我决定为工作中的某人创建一个 .txt-.csv 解析器。我读取了 .txt 文件中的每一行,并将其分成单独的 Maps,用于部分、小节、小节和小节的内容。然后将每个地图分配给它上面的地图(更多内容见下文)。我可以很好地打印所有内容,但是当我尝试阅读它时出现以下错误:"java.lang.String cannot be cast to java.util.Map"。该错误仅在代码运行后出现,而不是在编译时出现,在 NetBeans IDE 中也不会出现。
我的地图采用以下形式,每个对象都是它下面的地图:(为什么 Java 不能让这变得简单 -_- 关联数组就是我想要的)
(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>
可能不是最有效的阅读方式,计划稍后将其转换为递归函数,但这是我的代码,从我的项目中复制粘贴。我将 cmets 放在我发现错误所在的位置。
public static Map<String,Object> array=new HashMap<String,Object>();
/* Code for populating the following Maps and pushing them into array
<String,Object>subarray
<String,Object>subsubarray
<String,String>subsubcontents
*/
Set section=array.entrySet();
Iterator sectionI=section.iterator();
while(sectionI.hasNext()) {
Map.Entry sectionInfo=(Map.Entry)sectionI.next();
Map<String,Object> subMap=(Map)sectionInfo.getValue();
Set subSet=subMap.entrySet();
Iterator subI=subSet.iterator();
while(subI.hasNext()) {
Map.Entry subInfo=(Map.Entry)subI.next();
Map<String,Object> subsubMap=(Map)subInfo.getValue();
Set subsubSet=subsubMap.entrySet();
Iterator subsubI=subsubSet.iterator();
while(subsubI.hasNext()) {
System.out.println("test");
Map.Entry subsubInfo=(Map.Entry)subsubI.next();
Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue();
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
Set subcontentsSet=subcontentsMap.entrySet();
Iterator keys=subcontentsSet.iterator();
while(keys.hasNext()) {
Map.Entry keyMap=(Map.Entry)keys.next();
}
Iterator values=subcontentsSet.iterator();
while(values.hasNext()) {
Map.Entry valueMap=(Map.Entry)values.next();
}
}
}
}
任何帮助将不胜感激。这几天我一直在为此苦苦挣扎。
【问题讨论】:
-
你能发布整个堆栈跟踪吗?
-
getStackTrace() 返回 [Ljava.langStackTraceElement;@c0a150