NullPointerException

P

patrick

Ive got a Swing application that sometimes gives this error below.
Ive tried to catch this error from EventDispatchThread but gave up. Dont
know an easy way to do it.Even if I did I might only get the same exception
info as is shown below perhaps.
The error happens when a specific new JFrame is instantiated sometimes.
The application is way to big to post code.
Ive no idea what the problem is.

Ive posted this problem before but the exception I was getting then was:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException.
Its morphed for some reason.

Any help appreciated.

TIA

patrick


java.lang.NullPointerException
at javax.swing.LayoutComparator.compare(LayoutComparator.java:72)
at java.util.Arrays.mergeSort(Arrays.java:1278)
at java.util.Arrays.mergeSort(Arrays.java:1289)
at java.util.Arrays.mergeSort(Arrays.java:1290)
at java.util.Arrays.sort(Arrays.java:1219)
at java.util.Collections.sort(Collections.java:155)
at
javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:117)
at
javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:331)
at
javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:143)
at
javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:391)
at
java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:131)
at
java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:300)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:93)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)
 
P

patrick

Yes I posted everything.
The problem is that the null object occurs inside the AWT code not in my
code.
Something to do with perhaps which window is to be in focus.
Although something Im doing in the JFrame code must be the cause.That is
the connection Im looking for.

patrick
 
V

Vova Reznik

Is it happening when app. is starting (like steVisible(true) for frame
or show()?

If yes - try to do it from EventQueue using invokeLater(Runnable)
 
Z

zero

Ive got a Swing application that sometimes gives this error below.
Ive tried to catch this error from EventDispatchThread but gave up.
Dont know an easy way to do it.Even if I did I might only get the same
exception info as is shown below perhaps.
The error happens when a specific new JFrame is instantiated
sometimes. The application is way to big to post code.
Ive no idea what the problem is.

Ive posted this problem before but the exception I was getting then
was: Exception in thread "AWT-EventQueue-0"
java.lang.ClassCastException. Its morphed for some reason.

Any help appreciated.

TIA

patrick

<stack trace snipped>

Interesting problem. As far as I can tell, it happens because a Component
in the focus cycle does not have a Window parent. Is it possible to show
the code where the JFrame is instantiated?

Does this error happen every time the program creates this JFrame, or do
you know how else to reproduce it? If it appears to occur randomly it will
likely be very hard to locate.
 
P

patrick

well not the app but when a second JFrame is opening. Hmmm. That could be
the problem. Ill see if i can use invokeLater. though have to figure if
that might cause me other problems like changing the sequence of things.

patrick
 
P

patrick

there are 12000 lines.Ive posted some bits below.
Might be some of these JPanels being added are the things that dont have a
Window parent? (I cant change them now)

The error occurs only sometimes that the JFrame is opened.

patrick


Its a class Table that extends JFrame with constructor code:

this.setResizable(false);
this.addWindowListener(this);
this.addWindowFocusListener(this);

tableName = table; numcards = getCardsNeeded(this);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try
{
jbInit();
initiateImages();
// textTotalLabels();
hidePreticks();
// control.setVisible(false);
//Display the window.
this.setState(this.NORMAL);
this.setVisible(true);
}


jbInit() adds a lot of Swing stuff.
Included in jbInit a JLayeredPane:

jLayeredPane = new JLayeredPane();
jLayeredPane.setPreferredSize(new Dimension(tableWidth, tableHeight));

paintPanel = new PaintPanel(this, control.tableImage); //for the
tableImage
paintPanel.setSize(tableWidth, tableHeight);
paintPanel.setVisible(true);
jLayeredPane.add(paintPanel);
// paintPanel.repaint();
contentPane.add(jLayeredPane);
......

jLayeredPane.add(jPanelTableLayerB, new Integer(2), 2);
jLayeredPane.add(jPanelTableLayerC, new Integer(4), 4);
jLayeredPane.add(jPanelTableLayerD, new Integer(10), 10);
 
Z

zero

there are 12000 lines.Ive posted some bits below.
Might be some of these JPanels being added are the things that dont
have a Window parent? (I cant change them now)

The error occurs only sometimes that the JFrame is opened.

That makes it extra hard to locate the problem. I would suggest running a
series of tests, commenting out parts of the code. Strip it down one part
at a time, and each time compile and test to see if the problem persists.
This way you can pinpoint the exact location of the problem. It could be a
lot of work though.

I suggest you start with commenting out the enableEvents statement. IMO
that's the most likely candidate.

Alternatively you could try asking at sun's own awt or swing forums. Even
though it sounds unlikely that someone else ever had this problem, you may
get a useful reply.

swing forum: http://forum.java.sun.com/forum.jspa?forumID=57
awt forum: http://forum.java.sun.com/forum.jspa?forumID=5
 
R

Roedy Green

The null object is not in my code.It is in the AWT.

The link I pointed you to also gives you hints on what to do when the
problem is not in your code. See the last point.
 
R

Roedy Green

java.lang.NullPointerException
at javax.swing.LayoutComparator.compare(LayoutComparator.java:72)
at java.util.Arrays.mergeSort(Arrays.java:1278)
at java.util.Arrays.mergeSort(Arrays.java:1289)
at java.util.Arrays.mergeSort(Arrays.java:1290)
at java.util.Arrays.sort(Arrays.java:1219)
at java.util.Collections.sort(Collections.java:155)
at
javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:117)
at
javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:331)
at
javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:143)
at
javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:391)
at
java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:131)
at
java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:300)
at java.awt.Component.dispatchEventImpl(Component.java:3506)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:93)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:454)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

This is harder than usual to track because it did not originate in
your code. You can't trap it with a breakpoint to watch with Eclipse
debug. You can't even trap the NullPointerException and peek at the
state of the universe.

So we will have to use logic to solve it.

compare died with a null. Compare compares two things, apparently one
of them was null.

but let's just check. Here is the code for LayoutComparator in JDK
1.5. You did not tell us which JDK you were using.


/*
* @(#)LayoutComparator.java 1.7 04/02/05
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package javax.swing;

import java.util.Comparator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Window;


/**
* Comparator which attempts to sort Components based on their size
and
* position. Code adapted from original
javax.swing.DefaultFocusManager
* implementation.
*
* @version 1.7, 02/05/04
* @author David Mendenhall
*/
final class LayoutComparator implements Comparator,
java.io.Serializable {

private static final int ROW_TOLERANCE = 10;

private boolean horizontal = true;
private boolean leftToRight = true;

void setComponentOrientation(ComponentOrientation orientation) {
horizontal = orientation.isHorizontal();
leftToRight = orientation.isLeftToRight();
}

public int compare(Object o1, Object o2) {
Component a = (Component)o1;
Component b = (Component)o2;

if (a == b) {
return 0;
}

// Row/Column algorithm only applies to siblings. If 'a' and
'b'
// aren't siblings, then we need to find their most inferior
// ancestors which share a parent. Compute the ancestory lists
for
// each Component and then search from the Window down until
the
// hierarchy branches.
if (a.getParent() != b.getParent()) {
LinkedList aAncestory, bAncestory;

for(aAncestory = new LinkedList(); a != null; a =
a.getParent()) {
aAncestory.add(a);
if (a instanceof Window) {
break;
}
}
if (a == null) {
// 'a' is not part of a Window hierarchy. Can't cope.
throw new ClassCastException();
}

for(bAncestory = new LinkedList(); b != null; b =
b.getParent()) {
bAncestory.add(b);
if (b instanceof Window) {
break;
}
}
if (b == null) {
// 'b' is not part of a Window hierarchy. Can't cope.
72>>>>>> throw new ClassCastException();
}

for (ListIterator
aIter =
aAncestory.listIterator(aAncestory.size()),
bIter =
bAncestory.listIterator(bAncestory.size()); ;) {
if (aIter.hasPrevious()) {
a = (Component)aIter.previous();
} else {
// a is an ancestor of b
return -1;
}

if (bIter.hasPrevious()) {
b = (Component)bIter.previous();
} else {
// b is an ancestor of a
return 1;
}

if (a != b) {
break;
}
}
}

int ax = a.getX(), ay = a.getY(), bx = b.getX(), by =
b.getY();

int zOrder = a.getParent().getComponentZOrder(a) -
b.getParent().getComponentZOrder(b);
if (horizontal) {
if (leftToRight) {

// LT - Western Europe (optional for Japanese,
Chinese, Korean)

if (Math.abs(ay - by) < ROW_TOLERANCE) {
return (ax < bx) ? -1 : ((ax > bx) ? 1 : zOrder);
} else {
return (ay < by) ? -1 : 1;
}
} else { // !leftToRight

// RT - Middle East (Arabic, Hebrew)

if (Math.abs(ay - by) < ROW_TOLERANCE) {
return (ax > bx) ? -1 : ((ax < bx) ? 1 : zOrder);
} else {
return (ay < by) ? -1 : 1;
}
}
} else { // !horizontal
if (leftToRight) {

// TL - Mongolian

if (Math.abs(ax - bx) < ROW_TOLERANCE) {
return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
} else {
return (ax < bx) ? -1 : 1;
}
} else { // !leftToRight

// TR - Japanese, Chinese, Korean

if (Math.abs(ax - bx) < ROW_TOLERANCE) {
return (ay < by) ? -1 : ((ay > by) ? 1 : zOrder);
} else {
return (ax > bx) ? -1 : 1;
}
}
}
}
}

I have a problem. I can't see how line 72 could possibly have thrown a
NullPointerException. Presumably you were using some other JDK.

So the ball is now in your court to show us the code for the
LayoutComparator that you DID use.

I am impatient and want to get on with solving this. I notice the
first two lines of this class:

"Comparator which attempts to sort Components based on their size and
position.

Hmm. For compare to be able to do this, Components would have to HAVE
a size and position. Maybe they don't. We should check. Further the
code seems to be chasing ancestries. This implies Components must
have an ancestry chain back to a Window for this to work. Maybe they
don't. We should check.

It would nice to instrument this code with debug asserts.
Unfortunately Java does not let you modify Sun's code like that. You
have to write your own code, and somehow persuade Swing to use your
class in preference to the usual LayoutComparator.

You did not show us any of your code, but is seems plausible somewhere
you did some Focus or FocusTraversal stuff.

I expect somewhere in your code is something like this:

this.setFocusTraversalPolicy( new SortingFocusTraversalPolicy() );

or
this.setFocusTraversalPolicy( new SortingFocusTraversalPolicy( new
LayoutComparator()) );

Just change that to:

this.setFocusTraversalPolicy( new SortingFocusTraversalPolicy( new
InstrumentedLayoutComparator()) );

Where InstrumentedLayoutComparator is your version of Sun's code
peppered with asserts and dumps to let you know what is going on.
 
P

patrick

Roedy,

You miss nothing.
I was originally using JDK 1.5 and changed back to 1.4.2_04-b05 (to minimize
user downloads)

jdk1.5 gives a ClassCastException in the same situation, full stacktrace is
below.Which you commented on before in response to an earlier post.

The 1.4.2_04-b05 version is below also.

Now re this: this.setFocusTraversalPolicy(..)
Ive got no such line in my code.

Ive searched my code for 'focus' and the only things I can find are these:

1.
class Table extends JFrame implements ActionListener, WindowListener,
WindowFocusListener,
ChangeListener

2.//in the JFrame constructor:
this.addWindowFocusListener(this);

3. //dont know why this was put in but might be relevant.
enableEvents(AWTEvent.WINDOW_EVENT_MASK);

4.
jButtonB1.setFocusPainted(false);

5.

public void windowOpened(WindowEvent e)
{
toFront();
}
6.//a few like tthis
public void windowIconified(WindowEvent e)
{
}


Now I will try your suggestion and insert my own setFocusTraversalPolicy
using my own NewLayoutComparator.java which I can step through.That might
give me a clue as to what exactly is null.

thanks

patrick






Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException
at javax.swing.LayoutComparator.compare(LayoutComparator.java:72)
at java.util.Arrays.mergeSort(Arrays.java:1284)
at java.util.Arrays.mergeSort(Arrays.java:1296)
at java.util.Arrays.mergeSort(Arrays.java:1295)
at java.util.Arrays.mergeSort(Arrays.java:1295)
at java.util.Arrays.mergeSort(Arrays.java:1296)
at java.util.Arrays.sort(Arrays.java:1223)
at java.util.Collections.sort(Collections.java:159)
at
javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(SortingFocusTraversalPolicy.java:119)
at
javax.swing.SortingFocusTraversalPolicy.getFirstComponent(SortingFocusTraversalPolicy.java:425)
at
javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(LayoutFocusTraversalPolicy.java:148)
at
javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(SortingFocusTraversalPolicy.java:502)
at
java.awt.FocusTraversalPolicy.getInitialComponent(FocusTraversalPolicy.java:149)
at
java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:318)
at java.awt.Component.dispatchEventImpl(Component.java:3841)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.SequencedEvent.dispatch(SequencedEvent.java:93)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:461)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
-------------------
/* * @(#)LayoutComparator.java 1.3 03/01/23 * * Copyright 2003 Sun
Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use
is subject to license terms. */package javax.swing;import
java.util.Comparator;import java.util.LinkedList;import
java.util.ListIterator;import java.awt.Component;import
java.awt.ComponentOrientation;import java.awt.Window;/** * Comparator which
attempts to sort Components based on their size and * position. Code adapted
from original javax.swing.DefaultFocusManager * implementation. * * @version
1.3, 01/23/03 * @author David Mendenhall */final class LayoutComparator
implements Comparator, java.io.Serializable { private static final int
ROW_TOLERANCE = 10; private boolean horizontal = true; private boolean
leftToRight = true; void setComponentOrientation(ComponentOrientation
orientation) { horizontal = orientation.isHorizontal(); leftToRight =
orientation.isLeftToRight(); } public int compare(Object o1, Object
o2) { Component a = (Component)o1; Component b = (Component)o2; if (a == b)
{ return 0; } // Row/Column algorithm only applies to siblings. If 'a'
and 'b' // aren't siblings, then we need to find their most inferior //
ancestors which share a parent. Compute the ancestory lists for // each
Component and then search from the Window down until the // hierarchy
branches. if (a.getParent() != b.getParent()) { LinkedList aAncestory,
bAncestory; for(aAncestory = new LinkedList(); a != null; a =
a.getParent()) { aAncestory.add(a); if (a instanceof Window) {
break; } } if (a == null) { // 'a' is not part of a Window
hierarchy. Can't cope. throw new ClassCastException(a.toString()); }
for(bAncestory = new LinkedList(); b != null; b = b.getParent()) {
bAncestory.add(b); if (b instanceof Window) { break; } } if
(b == null) { // 'b' is not part of a Window hierarchy. Can't cope. throw
new ClassCastException(b.toString()); } for (ListIterator
aIter = aAncestory.listIterator(aAncestory.size()), bIter =
bAncestory.listIterator(bAncestory.size()); ;) { if (aIter.hasPrevious())
{ a = (Component)aIter.previous(); } else { // a is an ancestor
of b return -1; } if (bIter.hasPrevious()) { b =
(Component)bIter.previous(); } else { // b is an ancestor of a
return 1; } if (a != b) { break; } } } int ax = a.getX(),
ay = a.getY(), bx = b.getX(), by = b.getY(); if (horizontal) { if
(leftToRight) { // LT - Western Europe (optional for Japanese, Chinese,
Korean) if (Math.abs(ay - by) < ROW_TOLERANCE) { return (ax < bx) ? -1
: 1; } else { return (ay < by) ? -1 : 1; } } else { //
!leftToRight // RT - Middle East (Arabic, Hebrew) if (Math.abs(ay - by) <
ROW_TOLERANCE) { return (ax > bx) ? -1 : 1; } else { return (ay <
by) ? -1 : 1; } } } else { // !horizontal if (leftToRight) { //
TL - Mongolian if (Math.abs(ax - bx) < ROW_TOLERANCE) { return (ay <
by) ? -1 : 1; } else { return (ax < bx) ? -1 : 1; } } else { //
!leftToRight // TR - Japanese, Chinese, Korean if (Math.abs(ax - bx) <
ROW_TOLERANCE) { return (ay < by) ? -1 : 1; } else { return (ax >
bx) ? -1 : 1; } } } }}-------------------
 
R

Roedy Green

jdk1.5 gives a ClassCastException in the same situation, full stacktrace is
below.Which you commented on before in response to an earlier post.

In that case it looks as though b (the second component being
compared) is null. Did you add a null element to a container?

Another other option looks as though you have come elements being
compared that don't have the same parent. Focus your attention on
them.

Another opotion looks as though you have some elements on your
traversal with no common window ancestor. This is a an error.

So here is yet another way to track this down.

Write a method showAncestors ( Component )

And run it on everything that could be involved in focus including
containers when you add it. You should see a nice logical tree
structure. Do this BEFORE you do a setVisible( true ).

I have never screwed myself this way so I don't know what you can do
to foul it up. Perhaps adding twice, adding to some other container
tree, failing to add a container somewhere along the line? Do you have
more than one JFrame with some overlap?

Pruning the code and posting here on on a website lets others have a
crack at it too.
 
P

patrick

Roedy,

Yes I added a null element to a container indirectly I suppose.

I found stepping through InstrumentedLayoutComparator that a JButton that I
created and added was removed at some point by a removeAll() statement.
(They get added back later)
Hence a null JButton.

Anyway you have solved the problem which has been bugging me for months.Im
simply returning -1 if I find a or b null in the Comparator and this looks
like it wont cause any other problems. If it does I know where the problem
is.

many thanks
patrick
 
T

Thomas G. Marshall

patrick coughed up:
OK thanks for that.
A few ideas to pursue.
patrick


1. Don't ever top post after others have already bottom posted. You're
making a mess, and it is very hard to add comments once you've done that.

2. I've been eyebrow deep in awt/swing event exceptions many times in my
life. They can produce absolutely insane looking stack traces, and for good
reason: Take a gander at how the events are propagated around the components
in the java source files some day.

As it turns out, the best solution in this situation is the one that I've
seen almost always work. Follow Zero's advice, and start commenting out
sweeping sections of the code. I would suggest doing it this way:

1. Store away an exact copy of your entire source tree. You'll need it to
get safely back.
2. Hack away half of the code. Test. If it comes back, then hack away half
of remaining. (you get the idea). Start at one of the upper methods where
most of the components are actually added.

If you limit the cleaving away to simply the components themselves, it will
likely tell you the component triggering it all, but might not tell you what
is actually causing the problem. However, I have to say, that in easily 90%
of the cases I've banged my head against, just finding the component itself
exposes the problem instantly. But it might not.

So, once you have figured out the component that causes the problem to go
away when erased and has the problem come back when it is put back, then
start focusing on cleaving out the surrounding code that adds listeners and
other support code for that component.

I've had a great deal of success with this. In short: 1. focus on the
components, 2. then focus on what you do with that component.

If all else fails with this, then at least you will have a greatly pared
down example to show others.

Good luck.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top