因此,我尝试使用 Java 在 Intellij 中编译一个简单的 GUI,但当我运行代码时,我不断收到错误消息。昨天我没有收到此错误,从那以后没有任何变化。该项目有一个 gradle 依赖项 - 目前为空。
为了排除故障,我卸载并重新安装了 Java 8 和 Intellij 2019.1.1 - 无济于事。
主要内容
public class application {
public static void main(String[] args) {
AppGUI appGUI = new AppGUI();
appGUI.setVisible(true);
}
}
图形界面
import javax.swing.*;
public class AppGUI extends JFrame{
private JPanel rootLabel;
private JLabel testLabel;
public AppGUI() {
add(rootLabel);
setSize(400,500);
}
}
梯度
plugins {
id 'java'
}
group 'liamgooch'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
我收到以下错误消息:
6:17:13 PM: Executing task 'application.main()'...
> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :application.main() FAILED
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1095)
at java.awt.Container.add(Container.java:1007)
at javax.swing.JFrame.addImpl(JFrame.java:567)
at java.awt.Container.add(Container.java:419)
at AppGUI.<init>(AppGUI.java:8)
at application.main(application.java:3)
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':application.main()'.
> Process 'command 'C:/Program Files/Java/jdk1.8.0_212/bin/java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 1s
2 actionable tasks: 1 executed, 1 up-to-date
Process 'command 'C:/Program Files/Java/jdk1.8.0_212/bin/java.exe'' finished with non-zero exit value 1
6:17:15 PM: Task execution finished 'application.main()'.
最佳答案
原因是您没有在AppGUI类中初始化JLabel
和JPanel
。
rootLabel = new JPanel();
testLabel = new JLabel();
关于Java - Intellij 无法编译 GUI - 退出值 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55791358/