Java - Intellij无法编译GUI - 退出值1

问题描述 投票:0回答:2

因此,我尝试使用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);
    }
}

gradle这个

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()'.
java user-interface gradle intellij-idea
2个回答
3
投票

原因是因为你没有在AppGUI类中初始化JLabelJPanel

rootLabel = new JPanel();
testLabel = new JLabel();

2
投票

你说它有用吗?我不知道怎么样。在您发布的代码中,我没有看到您为变量rootLabel指定值的位置。因此它是null,因此当你使用null参数调用类NullPointerException的方法add时,你得到一个JFrame。它在你发布的堆栈跟踪中如此说明......

at AppGUI.<init>(AppGUI.java:8)
© www.soinside.com 2019 - 2024. All rights reserved.