Solved

out of memory error

Posted on 2003-12-10
13
837 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:ScarletBlue
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 9910853
 At a first glance you do not seem to get the next object of the result so it loops forever.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9910872
 You need to call results.next();
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9910896
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...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:grim_toaster
ID: 9910969
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());
 
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9911007
> (use attrs instead of attrs1):

That's the bit I missed...heh
0
 

Author Comment

by:ScarletBlue
ID: 9911023
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
0
 
LVL 35

Expert Comment

by:girionis
ID: 9911043
 The object you are receiving is probably of type String, therefore you need to cast it into String instead of SearchResults.

0
 

Author Comment

by:ScarletBlue
ID: 9911122
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());
0
 
LVL 35

Expert Comment

by:girionis
ID: 9911165
 It looks fine to me apart from this statement:

> break;

  Why do you have it there?
0
 

Author Comment

by:ScarletBlue
ID: 9911323
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.


0
 
LVL 35

Expert Comment

by:girionis
ID: 9911339
 Yes because you enter the if loop as long as there are more results.
0
 
LVL 7

Accepted Solution

by:
grim_toaster earned 20 total points
ID: 9911810
As a question, why do you actually need your loop?  As you are getting the Attributes object outside of the loop, and then reading the same value from it each time.  So the below should work:

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");
System.out.println(attrs.get("member").toString().substring(4));
0
 

Author Comment

by:ScarletBlue
ID: 9912038
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 441
java imports not found 4 68
passing enum to a method 4 47
Tomcat 9 + java 8 error while trying to deploy a war file 2 841
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question