cancel
Showing results for 
Search instead for 
Did you mean: 

ArrayIndexoOutOfBoundsException

Former Member
0 Kudos

Hi,

I have a Webdynpro application which uses MDM4J APIs. I need to extract the field values based on some other field value from the same table.

Ex: I need to populate a Drop Down box with 2 field values namely Name and ID separeted by comma.

Although all the records have value for name, ID is not compulsory.

I am using the following code, but getting an exception.

A2iValueArray dropDownValues;

A2iValueArray dropDownValuesID;

try

{

dropDownValues =

catalog.GetDistinctValues(

tableName,

fieldName,

0,

Integer.MAX_VALUE);

for (int i = 0; i < dropDownValues.GetSize(); i++)

{

if(wdContext.currentContextElement ().getVa_Action_Selected().equals(

"Allocate An Associate"))

{

dropDownValuesID =

catalog.GetDistinctValues(

tableName,

"Project_ID",

i,

1);

modifiableValuSet.put(

dropDownValues.GetValueAt(i).GetStringValue() + "," +

dropDownValuesID.GetValueAt(0).GetStringValue(),

dropDownValues.GetValueAt(i).GetStringValue() + "," +

dropDownValuesID.GetValueAt(0).GetStringValue());

}

else

{

modifiableValuSet.put(

dropDownValues.GetValueAt(i).GetStringValue(),

dropDownValues.GetValueAt(i).GetStringValue());

}

}

}

I am getting the following exception:

java.lang.java.lang.ArrayIndexoOutOfBoundsException: 0>=0

Please suggest where i am making teh mistake in the code.

Regards,

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Becky,

First try to print the value of dropDownValues.GetSize(); and check whether you are able to fetch the records from MDM.

Regards,

Jitesh Talreja

Former Member
0 Kudos

Hi Jitesh,

I tried something with what u suggested and now i am able to get the ID values, but teh problem is as i am using GetDistinctValues method, it is just giving me the ID irrespective of the Names i.e. i am not getting the ID for that respective Name in my drop down. The names are getting displayed in alphabetical order and beside that the IDs are coming in numerical order.

How can i get teh ID for the respective Name?? Plz help me with this.

Regards,

Becky.

Former Member
0 Kudos

Hi Becky,

I think the problem is at dropDownValuesID.GetValueAt(0).GetStringValue().

As you told ID may not exist for the name, so when you are trying to get Id for name using GetDistinctValues method , result may be empty.

You can retrieve name and id from the look up table in a single step and form a single string like (Record id u2013 Name , ID) for each record and store it in a list(arraylist).

Use this array list in you web dynpro application to populate the drop down. If you are using DropdownbyKey UI element, you can split concatenated string into two parts (there split function on String) by u201C-u201C . It gives you String array of size 2, first element is Record Id and second element is (Name , Id ).

Please find below for sample code and let us know if still you have problem.

public void populateDropDown(IModifiableSimpleValueSet valueSet,List result){

if(result != null && valueSet != null){

for (int i = 0; i < result.size(); i++) {

String lookupValues = (String )result.get(i);

String[] resultSet= lookupValues.split(" - ");

if(resultSet != null){

valueSet.put(resultSet[0],resultSet[1]);

}

}

}

}

public List getLookupData(String tableName,String fieldName, String idField) throws Exception{

List list = new ArrayList();

RetrieveLimitedRecordsCommand limitedRecord = new RetrieveLimitedRecordsCommand(context);

Search search = new Search(tableSchema.getTable().getId());

ResultDefinition resultDe = new ResultDefinition(tableSchema.getTable().getId());

resultDe.setSelectFields(tableSchema.getFieldIds());

//or

//Add Name and ID fields using AddSelectedFields

/* Search */

limitedRecord.setSearch(search);

limitedRecord.setResultDefinition(resultDe);

limitedRecord.execute();

/* Result Set */

RecordResultSet resultSet =limitedRecord.getRecords();

Record [] records = resultSet.getRecords();

for (int i = 0; i < records.length; i++) {

Record record = records<i>;

MdmValue value = record.getFieldValue(tableSchema.getFieldId("Name"));

MdmValue IdValue= record.getFieldValue(tableSchema.getFieldId("ID"));

String concatString = ecord.getId().getString() +" - " + value.toString() + ", "+ IdValue.toString();

list.add(concatString);

}

return list;

}

Former Member
0 Kudos

Hi Veera,

Thanx for the response, but i am using MDM4J and not the new mdm java apis.

Any code snippet using the 4J jars??

Regards,

Becky.

Former Member
0 Kudos

Hi Becky,

I do not have much idea on old MDM4J, i have started using new MDM apis directly.....

Anyway i believe there should be an option to read entire record from MDM table in MDM4J, what i suggest you, try to read entire record (name and Id ) and preserve record id and Name & ID in arrayList, Use this arraylist to populate the dropdown in webdynpro.

Thanks,

Veeru.

Former Member
0 Kudos

Hi,

Please refer this blog.. [https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/3702] [original link is broken] [original link is broken] [original link is broken];

This blog explains how to retrieve records from lookup table (please see "searchCountries" method) using MDM4J. You just have to include "Id field" also in result set definition( rsd.AddField("ID");) You can have name and id field values in result set .

I hope it helps.

Thanks,

Veeru.

Former Member
0 Kudos

Hi Veera/Jitesh,

I got some help from the below code, but the problem is i am able to retreive only those records which are having values for both Name and ID.

But i want to display all the Names with ID (either null or some value).

I am gettinh all teh records in the A2iResultSet (rs1) but while retreiving or trying to print the values its just omitting those lines if the ID is null, its coming out of teh loop.

Below is teh code used:

public void populateDropDown( java.lang.String nodeName, java.lang.String attributeName, java.lang.String tableName, java.lang.String fieldName, boolean select )

{

//@@begin populateDropDown()

ISimpleTypeModifiable modifiableAttribute;

if (nodeName.equals("None"))

{

modifiableAttribute =

wdContext

.getNodeInfo()

.getAttribute(attributeName)

.getModifiableSimpleType();

}

else

{

modifiableAttribute =

wdContext

.getNodeInfo()

.getChild(nodeName)

.getAttribute(attributeName)

.getModifiableSimpleType();

}

IModifiableSimpleValueSet modifiableValuSet =

modifiableAttribute.getSVServices().getModifiableSimpleValueSet();

modifiableValuSet.clear();

if (select)

{

//modifiableValuSet.put("Select", "Select");

}

A2iValueArray dropDownValues;

//A2iValueArray dropDownValuesID;

try

{

dropDownValues =

catalog.GetDistinctValues(

tableName,

fieldName,

0,

Integer.MAX_VALUE);

reportMsg("size:" + dropDownValues.GetSize());

for (int i = 0; i < dropDownValues.GetSize(); i++)

{

if(wdContext.currentContextElement().getVa_Action_Selected().equals(

"Allocate An Associate"))

{

Search search = new Search("Client_Projects");

// get free form table parameters

FreeFormTableParameter fftpName = search.GetParameters().NewFreeFormTableParameter("Client_Projects");

// define a parameter field for the 'Part Number field in the 'Products' table

FreeFormParameterField ffpfName = fftpName.GetFields().New("Project_Name");

// Getting All the records

ffpfName.GetFreeForm().NewString(dropDownValues.GetValueAt(i).GetStringValue(),FreeFormParameter.PrefixSearchType);

// add field to parameter table

fftpName.Add(ffpfName);

ResultSetDefinition rsd1 = new ResultSetDefinition("Client_Projects");

rsd1.AddField("Client");

rsd1.AddField("Project_Name");

rsd1.AddField("Project_ID");

rsd1.SetSize(150);

String sortField = "Project_Name";

boolean sortAscending = true;

A2iResultSet rs1;

try

{

rs1 = catalog.GetResultSet(search, rsd1, sortField, sortAscending, 0);

reportMsg("GetRecordCount : " + rs1.GetRecordCount());

int i1=0;

recordID = rs1.GetRecordIDAt(i1);

reportMsg("after recordID:" + recordID);

reportMsg("proj name:" + dropDownValues.GetValueAt(i).GetStringValue());

// for (i1 = 0; i1 < rs1.GetRecordCount(); i1++)

// {

//reportMsg("test1");

reportMsg("proj id: " + rs1.GetValueAt(i1, "Project_ID").GetStringValue());

//reportMsg("test1");

reportMsg("proj name: " + rs1.GetValueAt(i1, "Project_Name").GetStringValue());

try{

if (rs1.GetValueAt(i1, "Project_ID").GetStringValue().equalsIgnoreCase("")==true)

{

reportMsg("null val");

reportMsg("i=" + i + ",i1=" + i1);

modifiableValuSet.put(

dropDownValues.GetValueAt(i).GetStringValue() + "," + "NA",

dropDownValues.GetValueAt(i).GetStringValue() + "," + "NA"

);

}

else

{

reportMsg("non null val");

reportMsg("i=" + i + ",i1=" + i1);

modifiableValuSet.put(

dropDownValues.GetValueAt(i).GetStringValue() + "," +

rs1.GetValueAt(i1, "Project_ID").GetStringValue(),

dropDownValues.GetValueAt(i).GetStringValue() + "," +

rs1.GetValueAt(i1, "Project_ID").GetStringValue());

}

} catch (Exception e1) {

}

}

// }

catch(Exception exc){}

}

else

{

modifiableValuSet.put(

dropDownValues.GetValueAt(i).GetStringValue(),

dropDownValues.GetValueAt(i).GetStringValue());

}

}

}

catch (StringException e)

{

wdComponentAPI.getMessageManager().reportException(

"Error populating the location table." + e.toString(),

true);

}

//@@end

}

Please suggest....

Former Member
0 Kudos

Hi,

I can see empty catch blocks in attached code, I think exception is occuring and you are ignoring it, please include trace message (wdComponentAPI.getMessageManager().reportSuccess(:exception"+e.getMessage()) in catch blocks and run it again.May be it will help us to identify the actual problem.

Cheers,

Veeru.

Former Member
0 Kudos

Hi Veera,

I am getting java.lang.NullPointerException.

But Project name field is having value for it, however Project ID is null.

But the serach is based on Project Name only, then why is it happening??

Regards,

Becky.

Former Member
0 Kudos

Hi Becky,

Replace the following if (rs1.GetValueAt(i1, "Project_ID").GetStringValue().equalsIgnoreCase("")==true)

with the below code and try to run it again , if still problem exist please attach the error message and Webdynpro screen shot if possible.

if (rs1.GetValueAt(i1, "Project_ID") != null &&

rs1.GetValueAt(i1, "Project_ID") .GetStringValue().equalsIgnoreCase(""))

Thanks ,

Veeru.

Former Member
0 Kudos

Hi,

Its solved now, the problem was with the print statements, as the record was returning null values.

Thanks for your response.

Regards,

Becky.

Answers (0)