类Cast异常错误:尝试写入文件

时间:2012-07-31 13:43:56

标签: java arraylist

我正在尝试导出文件,但获得以下异常:

  

发生意外错误:java.lang.Exception:   sailpoint.tools.GeneralException:应用程序脚本抛出了一个   exception:java.lang.ClassCastException:无法强制转换   sailpoint.tools.xml.PersistentArrayList到java.lang.String BSF info:   导出文件 - Abc at line:0 column:columnNo

我的代码如下:

    import java.io.FileWriter;
    import java.io.File;
    import java.io.BufferedWriter;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.*;

    String appname = "Abc";
    String path = "//home/exportfile//";
    String filename = path+"ApplicationExport-"+appname+".txt";
    String ret = "false";

    QueryOptions ops = new QueryOptions();
    Filter [] filters = new Filter[1];
    filters[0] = Filter.eq("application.name", appname);
    ops.add(filters);

    List props = new ArrayList();
    props.add("identity.name");

    //Do search
    Iterator it = context.search(Link.class, ops, props);

    //Build file and export header row
    BufferedWriter out = new BufferedWriter(new FileWriter(filename));
    out.write("IdentityName,UserName,WorkforceID,Organization");
    out.newLine();          

    //Iterate Search Results
    if (it!=null)
    {                               
            while ( it.hasNext() ) {
                    //Get link and create object
                    Object [] record = it.next();
                    String identityName = (String) record[0];
                    Identity user = (Identity) context.getObject(Identity.class, identityName);

                    //Get Identity attributes for export
                    String workforceid = (String) user.getAttribute("workforceID");                 

                    //Get application attributes for export
                    String userid="";
                      String org="";
                    List links = user.getLinks();
                    if (links!=null)
                    {
                            Iterator lit = links.iterator();
                            while (lit.hasNext())
                            {
                                    Link l = lit.next();
                                    String lname = l.getApplicationName();
                                    if (lname.equalsIgnoreCase(appname))
                                    {
                                              userid = (String) l.getAttribute("User Name");
                                              org= (String) l.getAttribute("Organization");
                                    }
                            }
                    }                               

                    //Output file
                    out.write(identityName+","+userid+","+workforceid+","+org);                             
                    out.newLine();                                                                          
                    out.flush();
            }                       
            ret="true";
    }
    //Close file and return
    out.close();
    return ret;     

在运行上面的代码时,我收到了强制转换异常错误:因为Organization属性是Multivalued.i.e。:在该属性中,您可以有多个值。

非常感谢任何纠正此代码的帮助。

1 个答案:

答案 0 :(得分:1)

它清楚地表明,你得到一个sailpoint.tools.xml.PersistentArrayList类型的对象并试图将它转换为字符串。

从你的问题我明白问题在这里:

org= (String) l.getAttribute("Organization");

所以你需要把它改成:

sailpoint.tools.xml.PersistentArrayList orgList = (sailpoint.tools.xml.PersistentArrayList) l.getAttribute("Organization");

然后从该列表中提取正确的值。