Link to home
Start Free TrialLog in
Avatar of ScarletBlue
ScarletBlue

asked on

out of memory error

hi

i get an "Out of memory" error. I use string buffer. how can i solve this.
results is an instance of NamingEnumeration
my code:

StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
//SearchResult res = (SearchResult) results.next();
//Attributes attrs = res.getAttributes();
if (results.hasMore())
sb.append(attrs1.get("member").toString().substring(4) + ",");
else
sb.append(attrs1.get("member").toString().substring(4));
}
System.out.println(sb.toString());

thnx in advance
sb
Avatar of girionis
girionis
Flag of Greece image

 At a first glance you do not seem to get the next object of the result so it loops forever.
 You need to call results.next();
StringBuffer sb = new StringBuffer();
while( true )
{
    SearchResult res = (SearchResult) results.next();
    Attributes attrs = res.getAttributes();
    if (results.hasMore())
        sb.append(attrs1.get("member").toString().substring(4) + ",");
    else
    {
        sb.append(attrs1.get("member").toString().substring(4));
        break ;
    }
}
System.out.println(sb.toString());


or something...
Avatar of grim_toaster
grim_toaster

Your code looked as if you were getting there.  Your commented out lines of code are what you will need, plus with one other small change (use attrs instead of attrs1):

StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
SearchResult res = (SearchResult) results.next();
Attributes attrs = res.getAttributes();
if (results.hasMore())
sb.append(attrs.get("member").toString().substring(4) + ",");
else
sb.append(attrs.get("member").toString().substring(4));
}
System.out.println(sb.toString());
 
> (use attrs instead of attrs1):

That's the bit I missed...heh
Avatar of ScarletBlue

ASKER

hi  grim_toaster

When i do put in the commented out lines in...it gives me this error
                java.lang.ClassCastException: java.lang.String
      at com.sasol.iview.LDAPLookup.main(LDAPLookup.java:43)

this error is pointing to SearchResult res = (SearchResult) results.next();

can u help with this
sb
 The object you are receiving is probably of type String, therefore you need to cast it into String instead of SearchResults.

I have changed it a bit and this seems to work. It brings back the results I want...
but I got a feeling im doin something wrong.
Do you gurus see anything wrong with this.

DirContext dctx = new InitialDirContext(env);
Attributes attrs = dctx.getAttributes("CN=SAPPortal-Sec-Prod-Admin,OU=SAPPortal Groups,OU=Groups,OU=Core,DC=sasol,DC=com");
StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
if (results.hasMore()){
sb.append(attrs.get("member").toString().substring(4) + ",");
break;
}
results.next();
}
System.out.println(sb.toString());
 It looks fine to me apart from this statement:

> break;

  Why do you have it there?
its weird , if i take out the break...it returns the string about 4/5 times...so I would have duplicate results.
when i add in the break..it returns the string just once.


 Yes because you enter the if loop as long as there are more results.
ASKER CERTIFIED SOLUTION
Avatar of grim_toaster
grim_toaster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i dont know why i needed the loop either...
thanx grim_toaster, your suggested code works.

thanx for everyone's help

have a great day
scarlet