2014-07-14 48 views
0

这是(Managed-Bean best practice)的扩展。我写了一个类AppProperties定义在类的各种物品:除Managed Bean最佳实践

public class AppProperties implements Serializable { 

    private static final long serialVersionUID = 1L; 
     //contents of AppProperties Object 
     private String appRepID; 
     private String helpRepID; 
     private String ruleRepID; 
     private String filePath; 
     private Vector formNames; 
     //end content 
     private Session s; 
     private String serverName; 

     public String getAppRepID() { 
      return appRepID; 
     } 
     public void setAppRepID(String appRepID) { 
      this.appRepID = appRepID; 
     } 
//rest if getters and setters 
} 

在我的豆我有以下几点:

import ca.wfsystems.core.AppProperties; 

private final Map<String, AppProperties> internalMap = new HashMap<String, AppProperties>(); 

    public ApplicationMap() { 
     this.buildMap(internalMap); 
    } 

    private void buildMap(Map<String, AppProperties> theMap) { 

     try{ 
      AppProperties ap = null; 
      Session s = ExtLibUtil.getCurrentSession(); 
      vwApps = s.getCurrentDatabase().getView("vwWFSApplications"); 
      veCol = vwApps.getAllEntries(); 
      ve = veCol.getFirstEntry(); 
      tVE = null; 
      while (ve != null){ 
       Vector colVal = ve.getColumnValues(); 
       String tAppRepID = colVal.get(2).toString(); 
       ap.setAppRepID(colVal.get(2).toString()); 
       ap.setHelpRepID(colVal.get(3).toString()); 
       ap.setRuleRepID(colVal.get(4).toString()); 

       theMap.put(colVal.get(0).toString(), ap); 
      } 
     }catch(Exception e){ 
      System.out.println(e.toString()); 
     }finally{ 
      Utils.recycleObjects(s,vwApps); 
     } 
    } 

一切似乎除了在ap.setAppRepID OK(colVal( 2).toString())有一个编译器错误:“空指针访问变量ap只能在此时为空”setAppRepID和setHelpRepID的代码是相同的,并且setHelpRepID或setRuleRepID都没有编译器错误。我不确定问题是否在设置AppProperties ap = null试图创建AppProperties ap =新的AppProperties,但它不喜欢。我觉得我真的很接近做这个工作,但是......

感谢所有在我爬上JAVA斜坡时一直非常耐心的人。

回答

1

编译器错误是正确的,你的变量AP只能是在这一点空。 按照每个语句从

AppProperties ap = null; 

ap.setAppRepID(colVal.get(2).toString()); 

,并在任何一点AP初始化一个对象,它仍然会是空的。

您还会在setHelpRepID或setRuleRepID上看到编译器错误,但它并不会显示给您,因为它已经是第一条语句的问题。您可以通过注释setAppRepID行来尝试此操作,并且您应该在下一行看到相同的错误。

使公共构造在AppProperties类

public AppProperties() {}; 

,然后尝试改变

AppProperties ap = null; 

AppProperties ap = new AppProperties(); 
+0

感谢卡梅伦就是这样 - 在需要的空的构造我AppProperties Class,所以我可以创建一个AppProperties的新实例。现在编译正确,现在我可以开始测试它了。再次感谢 –

+0

没问题比尔! –

0

这绝对是“AppProperties ap = null”这一行。当您说您尝试过“AppProperties ap = new AppProprties”时,是否在末尾加入了“()”(即“AppProperties ap = new AppProperties()”)?它看起来像你的AppProperties bean有默认的无参数构造函数,所以这应该工作。具体来说,从你的代码猜测,我希望你想要将该行移到while循环打开之后。

你也有一个无限循环躺在等待:你从来没有在while循环中设置条目到下一个。如果你不使用从OpenNTF的Domino API,我建议一个成语是这样的:

ViewEntry entry = veCol.getFirstEntry(); 
while(entry != null) { 
    Vector<?> colVal = ve.getColumnValues(); 
    ... 

    entry.recycle(colVal); 
    ViewEntry tempEntry = entry; 
    entry = veCol.getNextEntry(); 
    tempEntry.recycle(); 
}