Reputation: 791
So I am trying to compile a simple GUI in Intellij with Java, but I keep getting an error message when I go to run the code. I was not getting this error yesterday, and nothing has changed since then. The project has a gradle dependency - which is currently empty.
To troubleshoot I have uninstalled and reinstalled both Java 8 and Intellij 2019.1.1 - to no avail.
main
public class application {
public static void main(String[] args) {
AppGUI appGUI = new AppGUI();
appGUI.setVisible(true);
}
}
gui
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'
}
I get the following error message:
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()'.
Upvotes: 0
Views: 429
Reputation: 20914
You say it was working? I don't see how. In the code you posted, I don't see where you assign a value to variable rootLabel
. Hence it is null and therefore you get a NullPointerException
when you call method add
of class JFrame
with a null argument. It says so in the stack trace you posted...
at AppGUI.<init>(AppGUI.java:8)
Upvotes: 2
Reputation: 12803
The reason is because you did not initialized JLabel
and JPanel
in AppGUI class.
rootLabel = new JPanel();
testLabel = new JLabel();
Upvotes: 3