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);
		
	}
}

I don't see how you can get a NPE at the line of code you show.

Are you sure that the code you posted is what you are executing? Compile it again and try to execute it again.

Edited 6 Years Ago by NormR1: n/a

I don't see how you can get a NPE at the line of code you show.

Are you sure that the code you posted is what you are executing? Compile it again and try to execute it again.

I'm positive man, that's what's running. I compiled it and ran it, that is the exception. I do not see how's that possible myself. Maybe I need to set something so I can use Java Swing? Some paths, system variables or something? I don't have a clue...

Execute the program in a command prompt window and copy and paste the contents of the window here.

Your program compiled and execute with no problem for me.

Execute the program in a command prompt window and copy and paste the contents of the window here.

Your program compiled and execute with no problem for me.

I compiled it using cmd, and when I tried to execute it this message showed up:

Exception in thread "main" java.lang.NullPointerException
        at sun.awt.FontConfiguration.getVersion(Unknown Source)
        at sun.awt.FontConfiguration.readFontConfigFile(Unknown Source)
        at sun.awt.FontConfiguration.<init>(Unknown Source)
        at sun.awt.windows.WFontConfiguration.<init>(Unknown Source)
        at sun.awt.Win32GraphicsEnvironment.createFontConfiguration(Unknown Sour
ce)
        at sun.java2d.SunGraphicsEnvironment$2.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.java2d.SunGraphicsEnvironment.<init>(Unknown Source)
        at sun.awt.Win32GraphicsEnvironment.<init>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
rce)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(Unknown Sour
ce)
        at java.awt.Window.init(Unknown Source)
        at java.awt.Window.<init>(Unknown Source)
        at java.awt.Frame.<init>(Unknown Source)
        at javax.swing.JFrame.<init>(Unknown Source)
        at JFrameDemo.main(JFrameDemo.java:8)

I guess there's something wrong with your java installation. Try uninstalling and reinstalling it.

At errors you get it says this:

at JFrameDemo.main(JFrameDemo.java:8)

What does it say at the line 8 of the file JFrameDemo.java. Something there is null

at javax.swing.JFrame.<init>(Unknown Source)
at JFrameDemo.main(JFrameDemo.java:8)

It looks like its in the constructor for JFrame.

It looks like its in the constructor for JFrame.

I find it hard to believe that that code could throw such exception. Don't forget that the line numbers you get from the errors have nothing to do with the line numbers that appear in the post. You shouldn't rely that a line of code here is at the exact line in the poster's java file

Look at the stack trace.
What was called when the NPE occurred?
It looks to me like the constructor for the JFrame class was called at line 8 in the OP's program.

It's definitely the constructor of JFrame, but as it was said back on the top - there
s nothing wrong with the code. I suspected it earlier, and now a friend suggested that I try running this via NetBeans instead of Eclipse, and it succeeded. So I guess Eclipse is missing something so it can execute java swing, but why couldn't I execute it via cmd?

You should say you are running the code from inside of an IDE.
I guess we should have asked.

You never tried it the way I suggested several posts back:

Execute the program in a command prompt window and copy and paste the WHOLE contents of the window here.


Try that and see what happens

Edited 6 Years Ago by NormR1: n/a

Dude, I pasted the WHOLE contents earlier. After I compiled it, I typed "Java JFrameDemo" and the exception that I've pasted earlier occurred. Again - I used Eclipse the first time, then I tried it using cmd, as you said and it didn't work. Then I tried NetBeans and it worked. But I want it to work via cmd.

Looks a lot like a Java install/config issue to me. Do you have multiple JRE installations - if so how many, where, and what versions? May be a good idea to clear it all out and just do a clean install of the latest Java

@paul ince
I asked you to paste the WHOLE contents of the window
The whole contents shows the directory and the command line.
You edited out/left off the commmand line
the WHOLE contents of the command prompt (on Windows):

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

D:\Norms\Norms Tools>java asdf
Exception in thread "main" java.lang.NoClassDefFoundError: asdf
Caused by: java.lang.ClassNotFoundException: asdf
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: asdf. Program will exit.

D:\Norms\Norms Tools>

For me it sounds like installation problems.
It is working fine for me. see screenshot. Which OS do you use.
I guess you should re-install JDK

Attachments Screenshot.png 61.41 KB
This article has been dead for over six months. Start a new discussion instead.