简体   繁体   English

Java swt IllegalArgumentException:参数不能为null

[英]Java swt IllegalArgumentException: Argument cannot be null

I'm trying to write a simple GUI for another program. 我正在尝试为另一个程序编写一个简单的GUI。 Using Eclipse and the plugin 'WindowBuilder' for that. 为此使用Eclipse和插件“ WindowBuilder”。 This is what I got so far: 这是我到目前为止所得到的:

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;

public class LightGUI {

  protected Shell shell;
  private Text lichterEingabe;
  private Text befehleEingabe;
  private Text ipEingabe;
  private Text portEingabe;

 /**
  * Launch the application.
  * @param args
  */        

public static void main(String[] args) {
    try {
        LightGUI window = new LightGUI();
//PROBLEM APPARENTLY HERE: 
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

/**
 * Open the window.
 */
public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
//...OR HERE:
        if (!display.readAndDispatch()) {
                display.sleep();
        }   
    }
}

/**
 * Create contents of the window.
 */
protected void createContents() {
    shell = new Shell();
    shell.setBackground(SWTResourceManager.getColor(230, 230, 250));
    shell.setSize(701, 513);
    shell.setText("SWT Application");

    Label lichter = new Label(shell, SWT.NONE);
    lichter.setBackground(SWTResourceManager.getColor(230, 230, 250));
    lichter.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    lichter.setBounds(70, 71, 76, 21);
    lichter.setText("Lichter:");

    Label befehle = new Label(shell, SWT.NONE);
    befehle.setBackground(SWTResourceManager.getColor(230, 230, 250));
    befehle.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    befehle.setBounds(70, 130, 76, 22);
    befehle.setText("Befehle:");

    Label ip = new Label(shell, SWT.NONE);
    ip.setBackground(SWTResourceManager.getColor(230, 230, 250));
    ip.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    ip.setBounds(350, 71, 76, 21);
    ip.setText("IP:");

    Label port = new Label(shell, SWT.NONE);
    port.setBackground(SWTResourceManager.getColor(230, 230, 250));
    port.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    port.setBounds(350, 130, 76, 22);
    port.setText("Port:");

    lichterEingabe = new Text(shell, SWT.BORDER);
    lichterEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    lichterEingabe.setBounds(177, 71, 88, 28);

    befehleEingabe = new Text(shell, SWT.BORDER);
    befehleEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    befehleEingabe.setBounds(177, 124, 88, 28);

    ipEingabe = new Text(shell, SWT.BORDER);
    ipEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    ipEingabe.setBounds(456, 71, 88, 28);

    portEingabe = new Text(shell, SWT.BORDER);
    portEingabe.setFont(SWTResourceManager.getFont("Segoe UI", 11, SWT.NORMAL));
    portEingabe.setBounds(455, 124, 89, 28);

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.setForeground(SWTResourceManager.getColor(138, 43, 226));
    btnNewButton.setFont(SWTResourceManager.getFont("Segoe UI", 14, SWT.BOLD));
    btnNewButton.setBounds(417, 239, 120, 49);
    btnNewButton.setText("OK!");

    Label rueckmeldung = new Label(shell, SWT.NONE);
    rueckmeldung.setBackground(SWTResourceManager.getColor(230, 230, 250));
    rueckmeldung.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
    rueckmeldung.setBounds(70, 319, 134, 28);
    rueckmeldung.setText("Rueckmeldung:");

    StyledText styledText = new StyledText(shell, SWT.BORDER);
    styledText.setFont(SWTResourceManager.getFont("Segoe UI", 10, SWT.NORMAL));
    styledText.setEditable(false);
    styledText.setBounds(70, 353, 340, 96);


    btnNewButton.addListener(SWT.Selection, new Listener() {
          public void handleEvent(Event e) {
              switch (e.type) {
              case SWT.Selection:
                rueckmeldung.setText(null);
                if(lichterEingabe.getText()!=null && befehleEingabe.getText()!=null && ipEingabe.getText()!=null && portEingabe.getText()!=null){
                    new Steuerung(Integer.parseInt(lichterEingabe.getText()), Integer.parseInt(befehleEingabe.getText()));
                }
                break;
              }
            }
          });


    }
}

The window is visible and all after launching. 该窗口是可见的,并且在启动后全部可见。 This is the result of my code: 这是我的代码的结果:

Small window 小窗户

One is meant to write numbers into the four textfields (the fifth, bigger one is for the sole purpose of displaying error messages). 一个用于将数字写入四个文本字段中(第五个较大的数字仅用于显示错误消息)。 After filling all of them, the button OK has to be clicked. 填写所有内容后,必须单击“确定”按钮。 Based on that, some calculations will happen in the background (calculations which won't be relevant until this works). 基于此,一些计算将在后台进行(这些计算在此工作之前是不相关的)。

However, what happens instead is the following: My window automatically disappears as soon as I've clicked the button. 但是,发生的是以下情况:单击按钮后,我的窗口自动消失。 And Eclipse console shows the following error message: Eclipse控制台显示以下错误消息:

java.lang.IllegalArgumentException: Argument cannot be null
at org.eclipse.swt.SWT.error(SWT.java:4514)
at org.eclipse.swt.SWT.error(SWT.java:4448)
at org.eclipse.swt.SWT.error(SWT.java:4419)
at org.eclipse.swt.widgets.Widget.error(Widget.java:482)
at org.eclipse.swt.widgets.Label.setText(Label.java:403)
at LightGUI$1.handleEvent(LightGUI.java:126)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4418)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4236)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3824)
at LightGUI.open(LightGUI.java:49)
at LightGUI.main(LightGUI.java:34)

Can anyone see something I don't? 谁能看到我看不到的东西? Why is this happening? 为什么会这样呢? There shouldn't be any changes visible, yet the program seems to think that 'window.open();' 不应有任何可见变化,但是程序似乎认为'window.open();' or display.readAndDispatch() return null for some reason? 或display.readAndDispatch()由于某些原因返回null?

A hint as to what exactly is the problem would be appreciated because I don't even have the faintest idea. 关于问题到底是什么的提示将不胜感激,因为我什至没有最微弱的想法。

It's as the error message says: Argument cannot be null 如错误消息所述: Argument cannot be null

And you have rueckmeldung.setText(null); 你有rueckmeldung.setText(null); . Use rueckmeldung.setText(""); 使用rueckmeldung.setText(""); instead, because empty labels don't contain a null value, but an empty string. 相反,因为空标签不包含null值,而是空字符串。

For the same reason in this line 由于同一原因

if(lichterEingabe.getText()!=null && befehleEingabe.getText()!=null && ipEingabe.getText()!=null && portEingabe.getText()!=null)

you might want to replace the null-checks with !...getText().isEmpty() . 您可能希望将!...getText().isEmpty()替换为空检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SWT java.lang.IllegalArgumentException:参数不能为null - SWT java.lang.IllegalArgumentException: Argument cannot be null java.lang.IllegalArgumentException:“原始”消息参数不能为null - java.lang.IllegalArgumentException: The 'original' message argument cannot be null SWT Composite构造函数在非null参数上引发IllegalArgumentException - SWT Composite constructor throws IllegalArgumentException on a non-null argument 运行测试时获取“java.lang.IllegalArgumentException:解码参数不能为空” - Getting "java.lang.IllegalArgumentException: Decode argument cannot be null" when running tests java.lang.IllegalArgumentException: uri 不能是 null - java.lang.IllegalArgumentException: uri cannot be null java.lang.IllegalArgumentException:[断言失败]-此参数是必需的; 它不能为空 - java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null IllegalArgumentException,插件不能为空 - IllegalArgumentException, Plugin cannot be null IllegalArgumentException:类型不能为空 - IllegalArgumentException: Type cannot be null 休眠查询java.lang.IllegalArgumentException:要遍历的节点不能为null - hibernate query java.lang.IllegalArgumentException: node to traverse cannot be null java.lang.IllegalArgumentException:迭代变量不能为null - java.lang.IllegalArgumentException: Iteration variable cannot be null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM