0

for a larg-DB based dynamically-multiple-area-menu, I created a class MenuPoint:

class MenuPoint{
public int areaId;

public int preID;

public String name;
public String desc;

public String stepInImg = "bsp.img";
public String stepOutImg = "bsp.img";

public String detailedForm = "bsp.fxml";
public String detailedImg = "bsp.img";

public String [] additionalOptionForm = new String[0];
public String [] additionalOptionName = new String[0];
public String [] additionalOptionImg = new String[0];}

and initialised it as an 0-length array in my main class: MenuController
public MenuPoint [] menuItem = new MenuPoint[0];

I use a API call to get my DB stored information by initialisation of MenuController. I store the results by using the following code:

int dataStructlength = 12;
    String[] exampleApiResultKeys = new String[dataStructlength];
    exampleApiResultKeys[0] = "SITE_NUMBER";
    exampleApiResultKeys[1] = "SITE_DESC";
    exampleApiResultKeys[2] = "SITE_NUMBER_EXT";
    exampleApiResultKeys[3] = "CELL_NUMBER";
    exampleApiResultKeys[4] = "CELL_DESC";
    exampleApiResultKeys[5] = "CELL_TYPE";
    exampleApiResultKeys[6] = "MACHINE_GROUP_NUMBER";
    exampleApiResultKeys[7] = "MACHINE_GROUP_DESC";
    exampleApiResultKeys[8] = "MACHINE_GROUP_TYPE";
    exampleApiResultKeys[9] = "STATION_NUMBER";
    exampleApiResultKeys[10] = "STATION_DESC";
    exampleApiResultKeys[11] = "STATION_TYPE";

    exampleApiController.test_mdataGetMachineAssetStructure(exampleApiFilter, exampleApiResultKeys);

    for(int i = 0; exampleApiController.resultValues.value != null && i < exampleApiController.resultValues.value.length/12; i++)
    {
        boolean isUseless = false;
        for(int a = 0; a < dataStructlength; a ++)
            if(true == exampleApiController.resultValues.value[i*dataStructlength+a].trim().isEmpty())
                isUseless = true;

        if(!isUseless)
        {
        int preId= -1;
        if("M".equals(exampleApiController.resultValues.value[i*12+5]))
        {
            if(giveItemId(0, preId, exampleApiController.resultValues.value[i*12]) >= 0)
                preId = giveItemId(0, preId, exampleApiController.resultValues.value[i*12]);
            else
                preId = addMenuItem(0, preId, exampleApiController.resultValues.value[i*12], exampleApiController.resultValues.value[i*12+1], "bsp.form");
        }
        if("M".equals(exampleApiController.resultValues.value[i*12+5]))
        {
            if(giveItemId(1, preId, exampleApiController.resultValues.value[i*12+3]) >= 0)
                preId = giveItemId(0, preId, exampleApiController.resultValues.value[i*12+3]);
            else
                preId = addMenuItem(1, preId, exampleApiController.resultValues.value[i*12+3], exampleApiController.resultValues.value[i*12+4], "bsp.form");
        }
        if("M".equals(exampleApiController.resultValues.value[i*12+8]))
        {
            if(giveItemId(2, preId, exampleApiController.resultValues.value[i*12+6]) >= 0)
                preId = giveItemId(0, preId, exampleApiController.resultValues.value[i*12+6]);
            else
                preId = addMenuItem(2, preId, exampleApiController.resultValues.value[i*12+6], exampleApiController.resultValues.value[i*12+7], "bsp.form");
        }
        if("M".equals(exampleApiController.resultValues.value[i*12+11]))
        {
            if(giveItemId(3, preId, exampleApiController.resultValues.value[i*12+9]) >= 0)
                preId = giveItemId(0, preId, exampleApiController.resultValues.value[i*12+9]);
            else
                preId = addMenuItem(3, preId, exampleApiController.resultValues.value[i*12+9], exampleApiController.resultValues.value[i*12+10], "bsp.form");
        }
        }

giveItemId:

public int giveItemId(int areaId_temp, int preId_temp, String name_temp)
{
    for(int i = 0; i < menuItem.length; i++)
    {
        if(menuItem[i].areaId == areaId_temp && menuItem[i].name.equals(name_temp) &&  menuItem[i].preID == preId_temp)
            return i;
    }
    return -1;
}

addMenuItem:

public int addMenuItem(int areaId_temp, int preId_temp, String name_temp, String desc_temp, String form_temp)
{
    Object newArray1 = Array.newInstance(menuItem.getClass().getComponentType(), Array.getLength(menuItem)+1);  // +1 Arrayfeld
    System.arraycopy(menuItem, 0, newArray1, 0, Array.getLength(menuItem));
    menuItem = (MenuPoint[]) newArray1; // expend but missing attributes

    menuItem[menuItem.length-1].areaId = areaId_temp;
    menuItem[menuItem.length-1].preID = preId_temp;
    menuItem[menuItem.length-1].name = name_temp;
    menuItem[menuItem.length-1].desc = desc_temp;
    menuItem[menuItem.length-1].detailedForm = form_temp;
    return menuItem.length-1;
}

I found out that menuItem dons´t carry any attributes after expending it.

Do I have to create "new" Instances of MenuPoint to make it work? Is it even possible to expend a class-array without loosing attributes or their values? It shoud, because in the end menuItem is just a pointer-array, pointing on multiple workstorage-addresses, aren't it?

Thank you guys for any tips or better concept you can give me. (I know this class-concept is silly but I have no idea for a better one) And please excuse my bad grammar.

tkw83
  • 185
  • 1
  • 5
xXTobiXx
  • 17
  • 5

2 Answers2

1

You create a new array in method addMenuItem which is then populated with the members from the existing menuItem, but the element at index (new) length - 1 isn't initialized:

menuItem[menuItem.length-1] = new MenuPoint();

You should have gotten a NullPointerException when trying to set the fields.

All this usage of java.lang.reflect.Array is rather contrived. There are simpler ways to achieve this. Here is a simplified versio of addMenuItem

public int addMenuItem(int areaId, String name){
    MenuPoint[] newMenuItem = new MenuPoint[menuItem.length + 1];
    System.arraycopy(menuItem, 0, newMenuITem, 0, menuItem.length);
    menuItem = newMenuItem;
    menuItem[menuItem.length-1] = new MenuPoint();
    menuItem[menuItem.length-1].areaId = areaId;
    menuItem[menuItem.length-1].name = name;
    return menuItem.length; // Why -1 ? 
}

But, above all, do use a List<MenuPoint>.

laune
  • 31,114
  • 3
  • 29
  • 42
1

For the answer of your question Do I have to create "new" Instances of MenuPoint to make it work?

Yes

From your code snippets you need to initialize MenuPoint class object to store its values to the array menuItem.
Initialize your array element with below line

menuItem[menuItem.length-1] = new MenuPoint();

Place above line after copying your array by menuItem = (MenuPoint[]) newArray1;

Without initialization there is NullPointerException exception for setting values to null object.

There are some better ways to maintain list of object using ArrayList class.

To use ArrayList you need following code change

public List<MenuPoint> menuItem = new ArrayList<MenuPoint>; //Initialize list    

Your Function will be

public int addMenuItem(int areaId_temp, int preId_temp, String name_temp, String desc_temp, String form_temp)
    {
        MenuPoint menuPoint = new MenuPoint();

        menuPoint.areaId = areaId_temp;
        menuPoint.preID = preId_temp;
        menuPoint.name = name_temp;
        menuPoint.desc = desc_temp;
        menuPoint.detailedForm = form_temp;
        menuItem.add(menuPoint);

        return menuItem.size()-1;
    }

I think this is easy than using reflection with array.

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
  • Thank you, of curse I have to do it like this ^^ But I think laune is right :) I shoud use lists ^^ – xXTobiXx Feb 09 '15 at 09:07