首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >java-当我重复使用一个元素,然后将该键的值更新到一个hashmap中时,会反复收到错误

java-当我重复使用一个元素,然后将该键的值更新到一个hashmap中时,会反复收到错误
EN

Stack Overflow用户
提问于 2012-06-03 16:51:39
回答 2查看 212关注 0票数 1

我正在开发一个执行重复性编程任务的应用程序。

作为此任务的一部分,将一个值分配给一个变量,然后将该变量插入到hashmap中,用于某些操作,然后将hashmap中的值替换为来自同一变量的新值。

我的意思是,在第一个值用完之后,另一个值被分配给同一个变量->变量被插入到从hashmap获取的hashmap->值中,以用于操作->,依此类推。

通过这种方式,从hashmap添加了许多值->使用->更新。这一切都发生在一个while循环中,该循环也有一个计数器变量。

问题是,在while循环的随机阶段,突然抛出一个错误,表明hashmap不包含具有指定键的对象。

这永远不会发生在相同的计数器数量,而loop...What我在这里做错了吗?这是不是因为我反复在hashmap中插入->使用->更新值?

对于您的ref,将数据插入到hashmap的代码如下--

代码语言:javascript
复制
   /* 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;
 }

从hashmap获取数据的代码如下所示--

代码语言:javascript
复制
 /* 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;
}

更新-异常堆栈跟踪如下

代码语言:javascript
复制
 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‘

代码语言:javascript
复制
Variable var = (Variable) context.get(name, curr_level);
 if (var == null) {
        throw new VariableException("Variable '" + name + "' is not defined!");
    }
EN

回答 2

Stack Overflow用户

发布于 2012-06-03 17:19:17

您的get与您的put不同:put遵循level的值。您的get执行了一些迭代,但是在任何地方都没有使用迭代变量i。在get中对哈希表的所有访问都使用相同的固定值level

对我来说,这似乎不仅仅是可疑的。

票数 1
EN

Stack Overflow用户

发布于 2012-06-03 17:47:30

您需要确保在您找到的级别返回该值:

也就是说,您需要稍微更改您的get方法:

代码语言:javascript
复制
/**
 * Function to obtain object (value) with specified key and level in scraper context
 * i.e. if a value is not found for the key at the given level, lower levels will
 * be searched.
 */
public Object get(Object key, Integer level) {
    String req= this.getStringKey(key);
    System.out.println(" REQUIRED- Variable name="+ req + "level="+ level);
    for(int i= level; i>=1; i--) {
        if(this.containsKey(req + "~" + i) ) {
            return(this.get(req + "~" + i));
        }
    }
    return null; //or, throw your exception here to differentiate between
                 //finding a null value and not finding the key at all
}

目前,您只检查并返回键req+"~"+level.toString()的值。您还可以考虑向异常添加更多信息,例如get失败的级别。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10869027

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档