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.getN ame().last IndexOf("/ "));
My understanding is that it cannot find this character ('/'). How can i get past this?
Below is the function:
public LinkList getCurrentNodeLinkList(IPo rtalCompon entRequest request){
continueSearch=true;
INavigationNode node = this.getCurrentNode(reques t);
INavigationNode nodeFound = null;
String parent = node.getName().substring(0 ,node.getN ame().last IndexOf("/ "));
INavigationNode parentNode = this.getNode(parent,reques t);
try{
NavigationNodes nodes = this.getInitialNodes(reque st);
nodeFound = this.findNode(nodes,parent Node);
}catch (Exception e) {
e.printStackTrace();
}
LinkList list = this.iterateNode(nodeFound ,request);
return list;
}
I have a index out of range error : -1, occuring on this line:
String parent = node.getName().substring(0
My understanding is that it cannot find this character ('/'). How can i get past this?
Below is the function:
public LinkList getCurrentNodeLinkList(IPo
continueSearch=true;
INavigationNode node = this.getCurrentNode(reques
INavigationNode nodeFound = null;
String parent = node.getName().substring(0
INavigationNode parentNode = this.getNode(parent,reques
try{
NavigationNodes nodes = this.getInitialNodes(reque
nodeFound = this.findNode(nodes,parent
}catch (Exception e) {
e.printStackTrace();
}
LinkList list = this.iterateNode(nodeFound
return list;
}
Are you sure the name contains the character "/"?
Do this:
String parent = node.getName().substring(0 , node.getName().lastIndexOf ("/") != -1 ? node.getName().lastIndexOf ("/") : node.getName().length());
________
radarsh
String parent = node.getName().substring(0
________
radarsh
ASKER
Hi girionis
Yes the name dos contain the character..
This is the output of node.getName:
-------------------------- ---------- -----
ROLES://portal_content/com .company.c ompanyroot /com.compa ny.europe/ com.compan y.europe.r oles/com.c ompany.eur ope.ro_com m_sect_cap ital_proje cts/com.co mpany.euro pe.ws_comm _sect_comm on/europea n_area/com mercial/co m.company. europe.hom e
and this is the output of parent:
-------------------------- ---------- -
ROLES://portal_content/com .company.c ompanyroot /com.compa ny.europe/ com.compan y.europe.r oles/com.c ompany.eur ope.ro_com m_sect_cap ital_proje cts/com.co mpany.euro pe.ws_comm _sect_comm on/europea n_area/com mercial
Please help further..
Yes the name dos contain the character..
This is the output of node.getName:
--------------------------
ROLES://portal_content/com
and this is the output of parent:
--------------------------
ROLES://portal_content/com
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.
ASKER
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,reques t);
Caused by: java.lang.StringIndexOutOf BoundsExce ption: String index out of range: -1
at java.lang.String.substring (String.ja va:1591)
at com.sapportals.portal.navi gation.Nav igationSer vice.getNo de(Navigat ionService .java:541)
at com.company.navigation.wor ksetmap.Wo rksetMapBe an.getNode (WorksetMa pBean.java :61)
at com.company.navigation.wor ksetmap.Wo rksetMapBe an.getCurr entNodeLin kList(Work setMapBean .java:196)
at pagelet._sapportalsjsp_Sta rlightHome .subDoCont ent(_sappo rtalsjsp_S tarlightHo me.java:16 3)
at pagelet._sapportalsjsp_Sta rlightHome .doContent (_sapporta lsjsp_Star lightHome. java:37)
at com.sapportals.portal.prt. component. AbstractPo rtalCompon ent.servic eDeprecate d(Abstract PortalComp onent.java :209)
at com.sapportals.portal.prt. component. AbstractPo rtalCompon ent.servic e(Abstract PortalComp onent.java :114)
at com.sapportals.portal.prt. core.broke r.PortalCo mponentIte mFacade.se rvice(Port alComponen tItemFacad e.java:343 )
at com.sapportals.portal.prt. core.broke r.PortalCo mponentIte m.service( PortalComp onentItem. java:817)
at com.sapportals.portal.prt. core.Porta lRequestMa nager.disp atchReques t(PortalRe questManag er.java:38 6)
at com.sapportals.portal.prt. core.Porta lRequestMa nager.disp atchReques t(PortalRe questManag er.java:46 3)
at com.sapportals.portal.prt. core.async .AsyncPort alComponen tResponse. include(As yncPortalC omponentRe sponse.jav a:708)
at com.sapportals.portal.html b.page.JSP DynPage.do Output(JSP DynPage.ja va:76)
at com.sapportals.htmlb.page. PageProces sor.handle Request(Pa geProcesso r.java:109 )
at com.sapportals.portal.html b.page.Pag eProcessor Component. doContent( PageProces sorCompone nt.java:13 5)
at com.sapportals.portal.prt. component. AbstractPo rtalCompon ent.servic eDeprecate d(Abstract PortalComp onent.java :209)
at com.sapportals.portal.prt. component. AbstractPo rtalCompon ent.servic e(Abstract PortalComp onent.java :114)
at com.sapportals.portal.prt. core.Porta lRequestMa nager.call PortalComp onent(Port alRequestM anager.jav a:301)
at com.sapportals.portal.prt. core.Porta lRequestMa nager.disp atchReques t(PortalRe questManag er.java:13 8)
at com.sapportals.portal.prt. core.async .AsyncIncl udeRunnabl e$1$DoDisp atchReques t.run(Asyn cIncludeRu nnable.jav a:363)
at java.security.AccessContro ller.doPri vileged(Na tive Method)
at com.sapportals.portal.prt. core.async .AsyncIncl udeRunnabl e.run(Asyn cIncludeRu nnable.jav a:376)
at EDU.oswego.cs.dl.util.conc urrent.Poo ledExecuto r$Worker.r un(PooledE xecutor.ja va:729)
at java.lang.Thread.run(Threa d.java:479 )
girionis...
i see what you mean ..
Below is the stack trace..the line the exception is pointing to is
INavigationNode parentNode = this.getNode(parent,reques
Caused by: java.lang.StringIndexOutOf
at java.lang.String.substring
at com.sapportals.portal.navi
at com.company.navigation.wor
at com.company.navigation.wor
at pagelet._sapportalsjsp_Sta
at pagelet._sapportalsjsp_Sta
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.html
at com.sapportals.htmlb.page.
at com.sapportals.portal.html
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at com.sapportals.portal.prt.
at java.security.AccessContro
at com.sapportals.portal.prt.
at EDU.oswego.cs.dl.util.conc
at java.lang.Thread.run(Threa
>>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
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,reques t);
Again this is weird because I do not see any substring method there. What type is the "this" class?
> INavigationNode parentNode = this.getNode(parent,reques
Again this is weird because I do not see any substring method there. What type is the "this" class?
>>com.sapportals.portal.na vigation.N avigationS ervice.get Node
Is that your code? That's where the error is occurring
Is that your code? That's where the error is occurring
ASKER
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/co m.company. companyroo t/com.comp any.europe /com.compa ny.europe. roles/com. company.eu rope.ro_co mm_sect_ca pital_proj ects/com.c ompany.eur ope.ws_com m_sect_com mon/europe an_area/co mmercial/c om.company .europe.ho me|ROLES:/ /portal_co ntent/com. company.co mpanyroot/ com.compan y.europe/c om.company .europe.ro les/com.co mpany.euro pe.rocomms ectallcomm odities/co m.company. europe.ws_ comm_sect_ common/eur opean_area /commercia l/com.comp any.europe .home)
The value for variable parent :
-------------------------- ---------
(ROLES://portal_content/co m.company. companyroo t/com.comp any.europe /com.compa ny.europe. roles/com. company.eu rope.ro_co mm_sect_ca pital_proj ects/com.c ompany.eur ope.ws_com m_sect_com mon/europe an_area/co mmercial/c om.company .europe.ho me|ROLES:/ /portal_co ntent/com. company.co mpanyroot/ com.compan y.europe/c om.company .europe.ro les/com.co mpany.euro pe.rocomms ectallcomm odities/co m.company. europe.ws_ comm_sect_ common/eur opean_area /commercia l
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/co
The value for variable parent :
--------------------------
(ROLES://portal_content/co
>>.but now it does not display my content :-(
Sounds like a different problem (question) altogether ...
Sounds like a different problem (question) altogether ...
This is what it is supposed to display.
ASKER
One question..
Is there a way to keep that end bracket without me having to add this line:
parent =parent + ")";
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 '/'
ASKER
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> int index = (nodeName.lastIndexOf("\") );
should be:
int index = nodeName.lastIndexOf('\');
should be:
int index = nodeName.lastIndexOf('\');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
thanx
(: