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("memb er").toStr ing().subs tring(4) + ",");
else
sb.append(attrs1.get("memb er").toStr ing().subs tring(4));
}
System.out.println(sb.toSt ring());
thnx in advance
sb
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("memb
else
sb.append(attrs1.get("memb
}
System.out.println(sb.toSt
thnx in advance
sb
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("memb er").toStr ing().subs tring(4) + ",");
else
{
sb.append(attrs1.get("memb er").toStr ing().subs tring(4));
break ;
}
}
System.out.println(sb.toSt ring());
or something...
while( true )
{
SearchResult res = (SearchResult) results.next();
Attributes attrs = res.getAttributes();
if (results.hasMore())
sb.append(attrs1.get("memb
else
{
sb.append(attrs1.get("memb
break ;
}
}
System.out.println(sb.toSt
or something...
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("membe r").toStri ng().subst ring(4) + ",");
else
sb.append(attrs.get("membe r").toStri ng().subst ring(4));
}
System.out.println(sb.toSt ring());
StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
SearchResult res = (SearchResult) results.next();
Attributes attrs = res.getAttributes();
if (results.hasMore())
sb.append(attrs.get("membe
else
sb.append(attrs.get("membe
}
System.out.println(sb.toSt
> (use attrs instead of attrs1):
That's the bit I missed...heh
That's the bit I missed...heh
ASKER
hi grim_toaster
When i do put in the commented out lines in...it gives me this error
java.lang.ClassCastExcepti on: java.lang.String
at com.sasol.iview.LDAPLookup .main(LDAP Lookup.jav a:43)
this error is pointing to SearchResult res = (SearchResult) results.next();
can u help with this
sb
When i do put in the commented out lines in...it gives me this error
java.lang.ClassCastExcepti
at com.sasol.iview.LDAPLookup
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.
ASKER
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=SAP Portal-Sec -Prod-Admi n,OU=SAPPo rtal Groups,OU=Groups,OU=Core,D C=sasol,DC =com");
StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
if (results.hasMore()){
sb.append(attrs.get("membe r").toStri ng().subst ring(4) + ",");
break;
}
results.next();
}
System.out.println(sb.toSt ring());
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=SAP
StringBuffer sb = new StringBuffer();
while (results.hasMore()) {
if (results.hasMore()){
sb.append(attrs.get("membe
break;
}
results.next();
}
System.out.println(sb.toSt
It looks fine to me apart from this statement:
> break;
Why do you have it there?
> break;
Why do you have it there?
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
thanx grim_toaster, your suggested code works.
thanx for everyone's help
have a great day
scarlet