Hello, I am quite new to swing, and I was trying the JFrame component, when this exception occurred , just when I tried to instantiate the JFrame object. It says the following:
Exception in thread "main" java.lang.NullPointerException
at JFrameDemo.main(JFrameDemo.java:8)
and line 8 is the following
JFrame frame = new JFrame("Title");
How come the nullexc. occurs when I try to instantiate a new JFrame object?
Here's the complete code.
import java.awt.*;
import javax.swing.*;
public class JFrameDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel textLabel = new JLabel("I'm a bloody label",SwingConstants.CENTER);
textLabel.setPreferredSize(new Dimension(300,100));
frame.getContentPane().add(textLabel,BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}