Link to home
Start Free TrialLog in
Avatar of ScarletBlue
ScarletBlue

asked on

String index out of range: -1

Hi guru's

I have a index out of range error : -1, occuring on this line:
      String parent  = node.getName().substring(0,node.getName().lastIndexOf("/"));

My understanding is that it cannot find this character ('/'). How can i get past this?

Below is the function:
public LinkList getCurrentNodeLinkList(IPortalComponentRequest request){
     continueSearch=true;
     INavigationNode node =  this.getCurrentNode(request);
     INavigationNode nodeFound = null;
     String parent  = node.getName().substring(0,node.getName().lastIndexOf("/"));
     INavigationNode parentNode = this.getNode(parent,request);

try{

     NavigationNodes nodes = this.getInitialNodes(request);
     nodeFound = this.findNode(nodes,parentNode);

}catch (Exception e) {
     e.printStackTrace();
}
LinkList list = this.iterateNode(nodeFound,request);

return list;
}
Avatar of girionis
girionis
Flag of Greece image

Are you sure the name contains the character "/"?
Avatar of radarsh
radarsh

Do this:

String parent  = node.getName().substring(0, node.getName().lastIndexOf("/") != -1 ?  node.getName().lastIndexOf("/") : node.getName().length());

________
radarsh
Avatar of ScarletBlue

ASKER

Hi girionis
Yes the name dos contain the character..

This is the output of node.getName:
-----------------------------------------
ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.ro_comm_sect_capital_projects/com.company.europe.ws_comm_sect_common/european_area/commercial/com.company.europe.home

and this is the output of parent:
-------------------------------------
ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.ro_comm_sect_capital_projects/com.company.europe.ws_comm_sect_common/european_area/commercial

Please help further..

The output is correct, this is what the "parent" variable should have, the node.getName() data without the last com.company.europe.home. When is the exception thrown? Are you sure it is thrown in the line you say? If you can read the "parent" then you shouldn't have got the exception.
radarsh..i have tried that..it still gives the String index out of range : -1

girionis...

i see what you mean ..
Below is the stack trace..the line the exception is pointing to is
        INavigationNode      parentNode = this.getNode(parent,request);



Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
      at java.lang.String.substring(String.java:1591)
      at com.sapportals.portal.navigation.NavigationService.getNode(NavigationService.java:541)
      at com.company.navigation.worksetmap.WorksetMapBean.getNode(WorksetMapBean.java:61)
      at com.company.navigation.worksetmap.WorksetMapBean.getCurrentNodeLinkList(WorksetMapBean.java:196)
      at pagelet._sapportalsjsp_StarlightHome.subDoContent(_sapportalsjsp_StarlightHome.java:163)
      at pagelet._sapportalsjsp_StarlightHome.doContent(_sapportalsjsp_StarlightHome.java:37)
      at com.sapportals.portal.prt.component.AbstractPortalComponent.serviceDeprecated(AbstractPortalComponent.java:209)
      at com.sapportals.portal.prt.component.AbstractPortalComponent.service(AbstractPortalComponent.java:114)
      at com.sapportals.portal.prt.core.broker.PortalComponentItemFacade.service(PortalComponentItemFacade.java:343)
      at com.sapportals.portal.prt.core.broker.PortalComponentItem.service(PortalComponentItem.java:817)
      at com.sapportals.portal.prt.core.PortalRequestManager.dispatchRequest(PortalRequestManager.java:386)
      at com.sapportals.portal.prt.core.PortalRequestManager.dispatchRequest(PortalRequestManager.java:463)
      at com.sapportals.portal.prt.core.async.AsyncPortalComponentResponse.include(AsyncPortalComponentResponse.java:708)
      at com.sapportals.portal.htmlb.page.JSPDynPage.doOutput(JSPDynPage.java:76)
      at com.sapportals.htmlb.page.PageProcessor.handleRequest(PageProcessor.java:109)
      at com.sapportals.portal.htmlb.page.PageProcessorComponent.doContent(PageProcessorComponent.java:135)
      at com.sapportals.portal.prt.component.AbstractPortalComponent.serviceDeprecated(AbstractPortalComponent.java:209)
      at com.sapportals.portal.prt.component.AbstractPortalComponent.service(AbstractPortalComponent.java:114)
      at com.sapportals.portal.prt.core.PortalRequestManager.callPortalComponent(PortalRequestManager.java:301)
      at com.sapportals.portal.prt.core.PortalRequestManager.dispatchRequest(PortalRequestManager.java:138)
      at com.sapportals.portal.prt.core.async.AsyncIncludeRunnable$1$DoDispatchRequest.run(AsyncIncludeRunnable.java:363)
      at java.security.AccessController.doPrivileged(Native Method)
      at com.sapportals.portal.prt.core.async.AsyncIncludeRunnable.run(AsyncIncludeRunnable.java:376)
      at EDU.oswego.cs.dl.util.concurrent.PooledExecutor$Worker.run(PooledExecutor.java:729)
      at java.lang.Thread.run(Thread.java:479)
>>Do this:
No need to call the methods twice:

String parent = null;
String nodeName = node.getName();
int ixLastForwardSlash = nodeName.lastIndexOf('/');
if (ixLastForwardSlash > -1) {
   parent  = nodeName().substring(0, ixLastForwardSlash);
}
// Do something if parent != null
>Below is the stack trace..the line the exception is pointing to is
>        INavigationNode     parentNode = this.getNode(parent,request);

Again this is weird because I do not see any substring method there. What type is the "this" class?
>>com.sapportals.portal.navigation.NavigationService.getNode

Is that your code? That's where the error is occurring
My apologies!!!
My colleague has merged the roles..
If you see below at the output..variable parent is missing a bracket. Ive added this :
      parent =parent + ")";

It does not give the index out of range error..but now it does not display my content :-(


So the output of the variables are such:

The value for variable node.getName() :
-----------------------------------------------
(ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.ro_comm_sect_capital_projects/com.company.europe.ws_comm_sect_common/european_area/commercial/com.company.europe.home|ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.rocommsectallcommodities/com.company.europe.ws_comm_sect_common/european_area/commercial/com.company.europe.home)

The value for variable parent :
-----------------------------------
(ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.ro_comm_sect_capital_projects/com.company.europe.ws_comm_sect_common/european_area/commercial/com.company.europe.home|ROLES://portal_content/com.company.companyroot/com.company.europe/com.company.europe.roles/com.company.europe.rocommsectallcommodities/com.company.europe.ws_comm_sect_common/european_area/commercial
>>.but now it does not display my content :-(

Sounds like a different problem (question) altogether ...
This is what it is supposed to display.
One question..

Is there a way to keep that end bracket without me having to add this line:
               parent =parent + ")";

You could use a StringBuffer and just delete characters after '/'
Hi

I am trying to use stringbuffer..but failing..

for a simple example ..
i have a value "xyz\mmp2"

i want the result to be "xyz2"

My code is :

StringBuffer nodeName = new StringBuffer (xyz\mmp2);
int sblen = nodeName.length();
char[] chars = new char[64];
int index = (nodeName.lastIndexOf("\"));
nodeName.getChars(0, index, chars, 0);

The line im confused on is:
                  nodeName.getChars(0, index, chars, 0);

Do i fist of all use the getChars() method for this purpose?


SOLUTION
Avatar of girionis
girionis
Flag of Greece image

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
SOLUTION
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
> int index = (nodeName.lastIndexOf("\"));

should be:

int index = nodeName.lastIndexOf('\');
ASKER CERTIFIED SOLUTION
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
thanx