0

I have three SWT group control with the same name apart from the number (i.e. 'grp1','grp2','grp3'). I want to make visible the group control in a for cycle; for this I have created an Array that contains the Group control.

This is the code:

Group [] grpArray = new Group[3];
    grpArray[0] = grp1;
    grpArray[1] = grp2;
    grpArray[2] = grp3;

    txtLvl = new Text(composite, SWT.BORDER | SWT.READ_ONLY);

    txtLvl.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {

            for (int i = 1; i <= Integer.parseInt(txtLvl.getText()); i++) {

                grpArray[i-1].setVisible(true);


            }
            }

        }
    );

This is the error code:

at it.anabasibdg.viste.AnagPdc$4.modifyText(AnagPdc.java:296)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Text.wmCommandChild(Unknown Source)
at org.eclipse.swt.widgets.Control.WM_COMMAND(Unknown Source)
at org.eclipse.swt.widgets.Control.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Display.windowProc(Unknown Source)
at org.eclipse.swt.internal.win32.OS.CallWindowProcW(Native Method)
at org.eclipse.swt.internal.win32.OS.CallWindowProc(Unknown Source)
at org.eclipse.swt.widgets.Text.callWindowProc(Unknown Source)
at org.eclipse.swt.widgets.Control.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Text.windowProc(Unknown Source)
at org.eclipse.swt.widgets.Display.windowProc(Unknown Source)
at org.eclipse.swt.internal.win32.OS.SetWindowTextW(Native Method)
at org.eclipse.swt.internal.win32.OS.SetWindowText(Unknown Source)
at org.eclipse.swt.widgets.Text.setText(Unknown Source)
at it.anabasibdg.viste.AnagPdc$3.widgetSelected(AnagPdc.java:213)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at it.anabasibdg.viste.Main.open(Main.java:58)
at it.anabasibdg.viste.LoginForm$3.widgetSelected(LoginForm.java:191)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at it.anabasibdg.viste.LoginForm.open(LoginForm.java:110)
at it.anabasibdg.viste.LoginForm$1.run(LoginForm.java:59)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at it.anabasibdg.viste.LoginForm.main(LoginForm.java:55)
mespo
  • 9
  • 6
  • 1
    you missed the error line in the stack trace. (the one right before the first one you pasted) – jhamon Apr 10 '20 at 09:18
  • Hi. I believe this is because you are modifying a UI element inside a listener that is executed in a non-UI thread. Any UI element within the eclipse framework can only be modified through the UI thread. You could have a look here or on google: https://stackoverflow.com/questions/10621554/eclipse-rcp-multithreading/30926575#30926575 – pandaadb Apr 10 '20 at 09:22
  • The first linke of stack trace: java.lang.NullPointerException – mespo Apr 10 '20 at 09:28
  • 1
    @pandaadb SWT listeners always run in the UI thread. This is not the issue – greg-449 Apr 10 '20 at 10:19

1 Answers1

-1

I have just solved my problem in this way:

private ArrayList<Group> grpArray1 = new ArrayList<Group>();


    txtLvl.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {

            groupArray(grp1, grp2,grp3);

            for (Group grp : grpArray1) {

                grp.setVisible(true);

            }


    public void groupArray(Group... c) {

    for (Group group : c) {

        grpArray1.add(group);

    }

}
mespo
  • 9
  • 6
  • This isn't an answer. Edit your question to add details. If you are getting a NullPointerException one of the variables txtLvl or grpArray is null. – greg-449 Apr 10 '20 at 10:18
  • @greg-449 If I print the grpArray.length, the System prints 3. It seems that I can't set or get any properties from group array. I obtain the same error if I try to getText() of the grpArray[0] element. – mespo Apr 10 '20 at 10:42
  • So you probably haven't assigned any non-null values to the elements in the array. Where do 'grp1'.... come from in you code snippet? Show is a proper [mre] – greg-449 Apr 10 '20 at 10:44