我正在开发一个执行重复性编程任务的应用程序。
作为该任务的一部分,为变量分配一个值,然后将该变量插入到 hashmap 中,在某些操作中使用,然后将 hashmap 中的值替换为来自同一变量的新值。
我的意思是,在第一个值用完后,另一个值被分配给同一个变量-> 变量被插入到 hashmap-> 从 hashmap 获取的值用于操作-> 等等。
通过这种方式,许多值被添加->使用->从 hashmap 更新。这一切都发生在一个同时有一个计数器变量的循环中。
问题是在 while 循环的随机阶段突然抛出一个错误,即 hashmap 不包含具有指定键的对象。
这永远不会发生在 while 循环的相同计数器编号上……我在这里做错了什么?这是因为我反复在哈希图中插入->使用->更新值吗?
对于您的参考,将数据插入哈希图中的代码如下 -
/* Function to store a variable- key value pair at designated level, in scraper context..
*
*/
public Object putVar(Object key, Variable value, Integer level) {
super.put((this.getStringKey(key)+"~"+level.toString()), value);
return null;
}
从哈希图中获取数据的代码如下 -
/* Function to obtain object (value) with specified key and level in scraper context...
*
*/
public Object get(Object key, Integer level) {
String req= this.getStringKey(key);
boolean found=false;
System.out.println(" REQUIRED- Variable name="+ req + "level="+ level);
for(int i= level; i>=1; i--)
{
if(this.containsKey(req+"~"+level.toString()) )
{
found=true;
break;
}
}
if(found==true)
return(this.get(req+"~"+level.toString()));
else
return null;
}
更新-异常堆栈跟踪如下---
ERROR - Variable 'webpage' is not defined!
org.webharvest.exception.VariableException: Variable 'webpage' is not defined!
at org.webharvest.runtime.processors.VarProcessor.execute(VarProcessor.java:70)
at org.webharvest.runtime.processors.BaseProcessor.run(BaseProcessor.java:119)
at org.webharvest.runtime.processors.BodyProcessor.execute(BodyProcessor.java:28)
at org.webharvest.runtime.processors.BaseProcessor.getBodyTextContent(BaseProcessor.java:176)
at org.webharvest.runtime.processors.BaseProcessor.getBodyTextContent(BaseProcessor.java:184)
at org.webharvest.runtime.processors.BaseProcessor.getBodyTextContent(BaseProcessor.java:188)
at org.webharvest.runtime.processors.FileProcessor.executeFileWrite(FileProcessor.java:146)
at org.webharvest.runtime.processors.FileProcessor.execute(FileProcessor.java:95)
at org.webharvest.runtime.processors.BaseProcessor.run(BaseProcessor.java:119)
at org.webharvest.runtime.processors.BodyProcessor.execute(BodyProcessor.java:28)
at org.webharvest.runtime.processors.WhileProcessor.execute(WhileProcessor.java:112)
at org.webharvest.runtime.processors.BaseProcessor.run(BaseProcessor.java:119)
at org.webharvest.runtime.Scraper.execute(Scraper.java:179)
at org.webharvest.runtime.Scraper.execute(Scraper.java:195)
at org.webharvest.gui.ScraperExecutionThread.run(ScraperExecutionThread.java:56)
最后,抛出这个异常的相关代码如下——使用的hashmap的名字是'context'
Variable var = (Variable) context.get(name, curr_level);
if (var == null) {
throw new VariableException("Variable '" + name + "' is not defined!");
}