Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

W

Warren Tang

Hello,

I see nothing wrong within my beginner's program, yet it throws an
NullPointerException. Could you look through the code and tell where I
was doing wrong please? I am using Fedora Linux / Eclipse JDT if that
matters.

Here is the stack trace, followed by the source code.
----------------------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tang.warren.LookFrame.makeButton(LookTest.java:40)
at tang.warren.LookFrame.<init>(LookTest.java:31)
at tang.warren.LookTest$1.run(LookTest.java:13)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:227)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:603)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:276)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)

----------------------------------
package tang.warren;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class LookTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame f = new LookFrame();
f.setVisible(true);
}
});
}
}

class LookFrame extends JFrame {
private JPanel lookPanel;

public LookFrame(){
this.setTitle("Look and Feel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());

for(LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels())
{
//if(laf != null)
makeButton(laf);
}

lookPanel = new JPanel();
this.add(lookPanel);
}

private void makeButton(final LookAndFeelInfo laf) {
JButton lookButton = new JButton(laf.getName());
lookPanel.add(lookButton);

lookButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg) {
try {
UIManager.setLookAndFeel(laf.getClassName());
SwingUtilities.updateComponentTreeUI(LookFrame.this);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
}
 
W

Warren Tang

Warren said:
Hello,

I see nothing wrong within my beginner's program, yet it throws an
NullPointerException. Could you look through the code and tell where I
was doing wrong please? I am using Fedora Linux / Eclipse JDT if that
matters.
Got it! I used the lookPanel before it's created.

Now I was wondering why Eclipse JDT doesn't point out *which line* the
exception occurs so that I can find the cause directly. When I press
F11(Start Debugging), all I get is a
"EventDispatchThread.Run() line 173. Source not found. edit source
lookup path" window.

Could you guide me through this? The stack trace doesn't point to the
exact location either. (It points to
"tang.warren.LookFrame.makeButton(LookTest.java:40)" but I why not the
exact line (lookPanel.add(lookButton); //lookPanel is null.)?
 
D

dimka

Hello,

I see nothing wrong within my beginner's program, yet it throws an
NullPointerException. Could you look through the code and tell where I
was doing wrong please? I am using Fedora Linux / Eclipse JDT if that
matters.

Here is the stack trace, followed by the source code.
----------------------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tang.warren.LookFrame.makeButton(LookTest.java:40)
at tang.warren.LookFrame.<init>(LookTest.java:31)
at tang.warren.LookTest$1.run(LookTest.java:13)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:227)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:603)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.jav-a:276)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:2-01)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.jav-a:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)

----------------------------------
package tang.warren;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class LookTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame f = new LookFrame();
f.setVisible(true);
}
});
}

}

class LookFrame extends JFrame {
private JPanel lookPanel;

public LookFrame(){
this.setTitle("Look and Feel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());

for(LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels())
{
//if(laf != null)
makeButton(laf);
}

lookPanel = new JPanel();
this.add(lookPanel);
}

private void makeButton(final LookAndFeelInfo laf) {
JButton lookButton = new JButton(laf.getName());
lookPanel.add(lookButton);

lookButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg) {
try {
UIManager.setLookAndFeel(laf.getClassName());
SwingUtilities.updateComponentTreeUI(LookFrame.this);
}catch(Exception e){
e.printStackTrace();
}
}
});
}



}

Hello!
You have NPE in method makeButton in line:
--->lookPanel.add(lookButton);

You create lookPanel only after cycle, where you create buttons.
Modify you code like this:
private JPanel lookPanel;

public LookFrame(){
this.setTitle("Look and Feel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize
());
lookPanel = new JPanel();
for(LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels())
{
//if(laf != null)
makeButton(laf);
}
this.add(lookPanel);
}
 
R

Roedy Green

Now I was wondering why Eclipse JDT doesn't point out *which line* the
exception occurs so that I can find the cause directly. When I press
F11(Start Debugging), all I get is a
"EventDispatchThread.Run() line 173. Source not found. edit source
lookup path" window.

because it did not happen in your code. You created an event and when
the event was dispatched the code to handle it failed because you
asked it to operate on a null panel.

What you are wishing Javac would do is point you where you _should_
have done a new. That would require AI and perhaps a bit of ESP.

see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION
--
Roedy Green Canadian Mind Products
http://mindprod.com

Never discourage anyone... who continually makes progress, no matter how slow.
~ Plato 428 BC died: 348 BC at age: 80
 
W

Warren Tang

rossum said:
Your error happened while processing line 40 of your program. That is
what the "40" at the end of the message means. It tells you the file
being run and the line where the error was first noticed:

"(LookTest.java:40)"

The file is LookTest.java and the error was noticed at line 40 of that
file. The error may not be on that line, as in your case, but that is
where the error first surfaced.

rossum
Thank you! I didn't realize that's the line number where the exception
was thrown.
 
W

Warren Tang

Roedy said:
because it did not happen in your code. You created an event and when
the event was dispatched the code to handle it failed because you
asked it to operate on a null panel.

What you are wishing Javac would do is point you where you _should_
have done a new. That would require AI and perhaps a bit of ESP.

see
http://mindprod.com/jgloss/runerrormessages.html#NULLPOINTEREXCEPTION

Actually I didn't expect the IDE to be clever like that. All I need is
that it points to where the first chance exception occurs.
 
W

Warren Tang

dimka said:
Hello,

I see nothing wrong within my beginner's program, yet it throws an
NullPointerException. Could you look through the code and tell where I
was doing wrong please? I am using Fedora Linux / Eclipse JDT if that
matters.

Here is the stack trace, followed by the source code.
----------------------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tang.warren.LookFrame.makeButton(LookTest.java:40)
at tang.warren.LookFrame.<init>(LookTest.java:31)
at tang.warren.LookTest$1.run(LookTest.java:13)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:227)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:603)
at
java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.jav-a:276)
at
java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:2-01)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.jav-a:191)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:186)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:139)

----------------------------------
package tang.warren;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

public class LookTest {
public static void main(String[] args){
EventQueue.invokeLater(new Runnable(){
@Override
public void run() {
JFrame f = new LookFrame();
f.setVisible(true);
}
});
}

}

class LookFrame extends JFrame {
private JPanel lookPanel;

public LookFrame(){
this.setTitle("Look and Feel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize());

for(LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels())
{
//if(laf != null)
makeButton(laf);
}

lookPanel = new JPanel();
this.add(lookPanel);
}

private void makeButton(final LookAndFeelInfo laf) {
JButton lookButton = new JButton(laf.getName());
lookPanel.add(lookButton);

lookButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg) {
try {
UIManager.setLookAndFeel(laf.getClassName());
SwingUtilities.updateComponentTreeUI(LookFrame.this);
}catch(Exception e){
e.printStackTrace();
}
}
});
}



}

Hello!
You have NPE in method makeButton in line:
--->lookPanel.add(lookButton);

You create lookPanel only after cycle, where you create buttons.
Modify you code like this:
private JPanel lookPanel;

public LookFrame(){
this.setTitle("Look and Feel");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(Toolkit.getDefaultToolkit().getScreenSize
());
lookPanel = new JPanel();
for(LookAndFeelInfo laf :
UIManager.getInstalledLookAndFeels())
{
//if(laf != null)
makeButton(laf);
}
this.add(lookPanel);
}

It's not long before I found that. Thanks you anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top