Java : javax swing and awt errors
NickName:GameRight Ask DateTime:2015-01-07T13:16:54

Java : javax swing and awt errors

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

More about “Java : javax swing and awt errors” related questions

Java : javax swing and awt errors

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 sti...

Show Detail

JUnit failing from HeadlessException in program using java.awt/javax.swing

I have a Java project that I need to create a JUnit ant task and some tests for. The tests should not be dependent on the environment. The program that is to be tested uses a lot of GUI-related thi...

Show Detail

javax.swing.JScrollPane cannot be cast to javax.swing.AbstractButton after changing to SeaGlass Look and Feel

My Java Swing application looks like this if no special look and feel applies: But after I installed the Sea Glass Look and Feel, the program is running but it shows different errors at different...

Show Detail

Javax.swing and Java.awt clashing

Im having a problem with all my code involving gui. For example the public class Ritpanel extends Jpanel line gives me an error: getBaselineResizeBehavior()' in 'javax.swing.JComponent' clashes wi...

Show Detail

Java Swing and Event errors?

I have been running a game server and I get this error. I'm not really familiar with java yet so please help me out. I have a couple event managers to handle player events, like woodcutting or mini...

Show Detail

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JButton

I need to solve this error. The window that i need to register the beer is not openning. I'm creating a simple system where we can register and search beer's aparrently. Here is the code. package

Show Detail

[Java Swing AWT]: why is this exception raised out of my code?

In our Java RMI distributed board game (where player are seen as a virtual ring network where one player allows the following to play his turn as soon as it has finished his) sometimes a client can...

Show Detail

When do I need to import java.awt.*? If I want to import javax.swing.* do I need to also import java.awt.*?

I'm getting back into Java and reviewing some of my old code and I'm seeing a lot of places where I have done import javax.swing.*; import java.awt.*; or actually importing specific classes from ...

Show Detail

javax.swing.JPanel cannot be cast to javax.swing.JLayeredPane

I got a small issue, i have an exception in my code. I have a base frame class: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import ...

Show Detail

mixing awt and swing in GUI programming using Java

I read on SO that mixing awt and swing is not really a good approach for GUI programming in Java. I cannot though, find any examples which does not use some awt components while using swing. For ex...

Show Detail