cancel
Showing results for 
Search instead for 
Did you mean: 

Error : Array constants can only be used in initializers

Former Member
0 Kudos

Hello All,

I am using tableview model and declared two-dimensional array.

Getting error Array constants can only be used in initializers in the line retVal<i>[j] = { { createBy,desc,dispName } };

String[][] retVal;

retVal = new String[20][20]

//Fetching IResorce propeties here

int resourceCounter = 0;

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

for (int j = 0; j < i; j++) {

IResource ir = list.get(i);

createBy = ir.getCreatedBy();

desc = ir.getDescription();

dispName = ir.getDisplayName();

retVal<i>[j] = { { createBy,desc,dispName } };

resourceCounter++;

}

}

return retVal;

Please help me out.

Thanks

Risha }

Accepted Solutions (1)

Accepted Solutions (1)

former_member182598
Active Contributor
0 Kudos

Hi Risha,

What exactly you are trying to do...? The construct you are using is used for static initialization of the array, and that's why it's complaining.

Draw a 2d diagram on the paper and go through and do a dry run on the paper to see what's going on.

Essentially what you are trying to do is putting 3 objects in one storage space, which any way is not going to work.

Refer to some tutorials on web about 2D array to get an understanding of how it works.

As far as TableView is concerned you are better off using a Vector which is easy to use and handle.

Thanks

Prashant

Former Member
0 Kudos

Hi Prashant,

Thanks for Reply.

I am following this URL [; to populate the data in the tableview model.

If I will use vector how to add IResource propeties into vector ?

Thanks

Risha

former_member182598
Active Contributor
0 Kudos

It's quite straightforward.

I am attaching a code snippet here. It's just to give you an idea.


private DefaultTableViewModel createNewTable(DefaultTableViewModel model) {

// get a vector with a filled up data array
		Vector data = createData();
// get a new vector for the titles
		Vector colName = new Vector();
// set titles

		colName.addElement("LASTNAME");
		colName.addElement("FIRSTNAME");
		colName.addElement("STREET");
		colName.addElement("ZIP");
		colName.addElement("CITY");

		model = new DefaultTableViewModel(data, colName);

		return model;
	}

	private Vector createData() {
// create vector
		Vector dataVec = new Vector();
		Vector retVector = new Vector();

// set data
		dataVec.addElement("Backer");
		dataVec.addElement("Melissa");
		dataVec.addElement("528 34th Ave");
		dataVec.addElement("94121");
		dataVec.addElement("San Francisco");
		retVector.addElement(dataVec);

		dataVec = new Vector();
		dataVec.addElement("Hamilton");
		dataVec.addElement("Ann");
		dataVec.addElement("4752 17th St");
		dataVec.addElement("94117");
		dataVec.addElement("San Francisco");
		retVector.addElement(dataVec);

		dataVec = new Vector();
		dataVec.addElement("Hudson");
		dataVec.addElement("Bree");
		dataVec.addElement("16 Hudson Ct");
		dataVec.addElement("94124");
		dataVec.addElement("San Francisco");
		retVector.addElement(dataVec);

		return retVector;
	}

Former Member
0 Kudos

I created ArrayList


static public String[] colnames =
		{
			"News Title",
			"Short Description",
			"Published",
			"Valid To",
			"Read Count",
			"Users Details",
			"Reply" };
  
	public ArrayList createData() {
      	ArrayList rowArrList = new ArrayList();
		ArrayList tableArrList = new ArrayList();
		String createBy = null;
		String desc = null;
		String dispName = null;
		String lastMod = null;
		String name = null;
		String resType = null;
		try {

			com.sapportals.portal.security.usermanagement.IUser user = null;
			user = WPUMFactory.getUserFactory().getEP5User(request.getUser());
			IResourceContext resourceContext = new ResourceContext(user);
			String path = "/documents/Australia/News";
			RID rid = RID.getRID(path);

			com.sapportals.wcm.repository.IResource res =
				ResourceFactory.getInstance().getResource(rid, resourceContext);

			IProperty prop = null;
			String propValue = null;
			PropertyName propName = null;

			if (res != null) {
				if (res.isCollection()) {
					ICollection collection = (ICollection) res;
					IResourceList list = collection.getChildren();

					int resourceCounter = 0;
					for (int i = 0; i < list.size(); i++) {						
//						for (int j = 0; j < i; j++) {
							IResource ir = list.get(i);
							
							createBy = ir.getCreatedBy();
							desc = ir.getDescription();
							dispName = ir.getDisplayName();
							lastMod = ir.getLastModifiedBy();
							name = ir.getName();
							resType = ir.getResourceType();
							
							rowArrList.add(createBy);
							rowArrList.add(desc);
							rowArrList.add(dispName);
							rowArrList.add(lastMod);
							rowArrList.add(name);
							rowArrList.add(resType);
	
							tableArrList.add(i,rowArrList);
						//}//Inside for loop
					}//First for loop
				}//Inside If
			}

		} catch (ResourceException e) {
			// TODO Auto-generated catch block
			response.write("Error " + e.getMessage());
		} catch (UserManagementException ex) {
			ex.printStackTrace();
			response.write("Error " + ex.getMessage());
		}
		return tableArrList;
	}
	/**
	 * Constructor.
	 */
	public TableViewBean() {
		ArrayList tabArr = createData();
		String[][] data = (String[][])tabArr.toArray();		
		model = new DefaultTableViewModel(data, colnames);

	}

Edited by: Risha on May 26, 2011 8:52 AM

Answers (0)