WulLaoGod
2019-09-21 21:24
采纳率: 50%
浏览 2.7k

idea,gradle项目报错,解决不了/(ㄒoㄒ)/~~.

import javax.swing.*;

public class test {
    private JPanel panel1;

    public static void main(String[] args) {
        JFrame frame = new JFrame("test");
        frame.setContentPane(new test().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
//自动生成的方法,第六行会报错,注释掉能运行......

图片说明

报错信息:

Gradle Daemon started in 2 s 140 ms

Task :compileJava
Task :processResources NO-SOURCE
Task :classes

Task :test.main() FAILED
Exception in thread "main" java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at java.desktop/javax.swing.JRootPane.setContentPane(JRootPane.java:598)
at java.desktop/javax.swing.JFrame.setContentPane(JFrame.java:679)
at test.main(test.java:8)

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':test.main()'.

    Process 'command 'C:/Program Files/Java/jdk-12.0.2/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

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.2.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 6s
2 actionable tasks: 2 executed

21:15:23: External task execution finished ' 'test.main()''.

图片说明

根据网上的教程搞得,以前可以运行,一顿操作后就报错了,更新版本,然后插件出了问题,卸载自己安装的插件.jdk一直是最新的12.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • Json-Huang 2019-09-22 09:19
    已采纳

    new test().panel1为空导致,可以先给panel1的属性设置值,一般获取自身属性,不需要先new,直接传入字段名称即可,
    frame.setContentPane(new test().panel1);可以改成frame.setContentPane(panel1);

    已采纳该答案
    打赏 评论