So, i have a code in Java that is says its successfull but i get errors that isn't even from my project. I can't do anything about it though.... I've tried importing only the needed parts and i still get same errors. I don't even know what a "container" is. I just finished the Java basics and moved on to this. Now i have a second computer (a macbook air) and it's got same code but it doesn't get any errors at all. Might it be me IDE, Java JDK or something? Or just something weird with the imported files? Thanks.
Code :
package windowsgui;
import javax.swing.*;
import java.awt.*;
public class WindowsGUI extends JFrame {
private JLabel label;
private JButton button;
private JTextField testfield;
public WindowsGUI() {
setLayout (new FlowLayout());
label = new JLabel("This is a label");
add(testfield);
button = new JButton("This is a button");
add(button);
}
public static void main (String args[]) {
WindowsGUI gui = new WindowsGUI();
gui.setSize(600, 400);
gui.setResizable(false);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
}
}
Error :
run:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1091)
at java.awt.Container.add(Container.java:1003)
at javax.swing.JFrame.addImpl(JFrame.java:564)
at java.awt.Container.add(Container.java:415)
at windowsgui.WindowsGUI.<init>(WindowsGUI.java:19)
at windowsgui.WindowsGUI.main(WindowsGUI.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)
Copyright Notice:Content Author:「GameRight」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/27812844/java-javax-swing-and-awt-errors