I am getting the following exception, which does specify any lines in my code, so how can I track down the problem --- any ideas?
TIA,
ablivian23
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicScrollPaneUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Are you by any chance trying to impelment a new L&F within your application? This sort of exceptions occur when you are using a L&F that was written with either a newer or older version of the JDK.
Here's a crazy thought. How about running your
program in a debugger, or failing that dropping println
statements in there to see what's going on?
You should probably read my question again -- I don't think you understand the situation.
Are you by any chance trying to impelment a new L&F
within your application? This sort of exceptions occur
when you are using a L&F that was written with either
a newer or older version of the JDK.
Otherwise, post some code
I am not trying to use a new L&F. Ummm as for code there is a lot of swing component -- but I think that it may be in this code:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionListener;
publicclass GUIManager
extends javax.swing.JFrame
implements java.io.Serializable {
public GUIManager() {
super("GUI Manager");
content = new Vector();
names = new Vector();
twice = false;
screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
}
publicvoid initComponents() {
list = new JList(names);
scrollPane = new JScrollPane();
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
scrollPane.setViewportView(list);
getContentPane().add(scrollPane);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(new ListSelectionListener() {
publicvoid valueChanged(
javax.swing.event.ListSelectionEvent evt) {
int index = list.getMinSelectionIndex();
setContent(index, true);
}
});
pack();
}
public GUIManager(BorderContent[] bc) {
super("GUI Manager");
content = new Vector();
names = new Vector();
twice = false;
screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
for (int i = 0; i < bc.length; i++) {
bc[i].createContent();
content.add(bc[i]);
names.add(bc[i].getTitle());
}
list = new JList(names);
scrollPane = new JScrollPane();
label = new JLabel("Select View");
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
scrollPane.setViewportView(list);
getContentPane().add(label);
getContentPane().add(scrollPane);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.addListSelectionListener(new ListSelectionListener() {
publicvoid valueChanged(
javax.swing.event.ListSelectionEvent evt) {
int index = list.getMinSelectionIndex();
setContent(index, true);
}
});
pack();
setVisible(true);
setContent(0, false);
}
privatevoid setContent(int contentIndex, boolean dispose) {
if (twice) {
twice = !twice;
return;
}
if (dispose) {
gui.dispose();
}
gui = new TemplateGUI((BorderContent) content.get(contentIndex));
gui.setLocation(new java.awt.Point(getX() + getWidth(), getY()));
gui.setSize(
new java.awt.Dimension(
(int) (screenSize.getWidth() - getWidth() - MARGIN),
(int) screenSize.getHeight() - MARGIN));
gui.setVisible(true);
twice = !twice;
}
publicvoid add(BorderContent bc) {
//Dimension size = getSize();
bc.createContent();
content.add(bc);
names.add(bc.getTitle());
}
publicvoid add(BorderContent bc, String customTitle) {
//Dimension size = getSize();
bc.createContent();
content.add(bc);
names.add(customTitle);
}
publicvoid add(BorderContent bc, String customTitle, boolean serialize) {
if (serialize) {
BorderContent serial_bc = null;
try {
File file =
File.createTempFile(customTitle.replace(' ', '_'), ".tmp");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(bc);
//Read In
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
serial_bc = (BorderContent) ois.readObject();
ois.close();
fis.close();
oos.close();
fos.close();
} catch (Exception e) {
}
add(serial_bc, customTitle);
} else {
add(bc, customTitle);
}
}
publicvoid initGUI() {
initComponents();
setVisible(true);
setContent(0, false);
}
/**
*@param bc the BorderContent that the user would like displayed first
*/
publicvoid initGUI(BorderContent bc) {
initComponents();
setVisible(true);
int index = content.indexOf(bc);
setContent((index == -1 ? 0 : index), false);
}
publicclass TemplateGUI
extends javax.swing.JFrame
implements java.io.Serializable {
public TemplateGUI(BorderContent bc) {
//Initialize all objects
super();
Component center = bc.getCenterContent();
Component east = bc.getEastContent();
Component south = bc.getSouthContent();
Dimension size = bc.getSize();
setTitle(bc.getTitle());
if (center != null) {
getContentPane().add(center, BorderLayout.CENTER);
}
if (east != null) {
getContentPane().add(east, BorderLayout.EAST);
}
if (south != null) {
getContentPane().add(south, BorderLayout.SOUTH);
}
pack();
p("bc = " + bc);
//if (size != null) {
// setSize(size);
//}
}
}
publicstaticvoid p(String s) {
System.out.println(s);
}
//Content for the manager to manage
private TemplateGUI gui;
private Vector content;
private Dimension screenSize;
//Components to display list
private JScrollPane scrollPane;
private JList list;
private JLabel label;
private Vector names;
privateboolean twice;
privatefinalint MARGIN = 100;
//Pixel border between the displayed table and the edge of the screen
}
I'm not sure whether I should be impressed or just break down sobbing. You might try mentioning what you are doing when the error occurs. It looks like it might be happening when you open one of these TemplateGUI thingies. Or maybe the second one???
From a quick look at your code, the only thing that jumps to mind is that a component can only have one parent. If you have a component in one container and then add it to another, it is removed from the first container.
I have compiled the source with both jdks 1.5.0 and 1.4.2 and it gets the same exceptions with jre versions of 1.3.1, 1.4.2 and 1.5.0.
OK, but you did you (intentionally or accidentally) recompile the JRE itself? "Unknown Source" in a stack trace means that the class was compiled without debugging information (javac -g:none). It's very unusual to see that with java.* classes, since Sun compiles the standard JRE distributions with full debugging info.
Here's a crazy thought. How about running your
program in a debugger, or failing that dropping
println
statements in there to see what's going on?
You should probably read my question again -- I don't
think you understand the situation.
The suggestion is applicable to your situation. It's just that a) you don't really know where to dbug or println and b) even if you find out which line of your code is triggering this, you may not be able to easily figure out what to do about it.
Presumably the reason you don't see any of your code in the stack trace is because it's a different thread.
You can put print statements in or debug and see how far your code is getting before this happens. Since it's a separate thread, you may get different results each time you run it, but hopefully if you do it enough, you'll see a small handful of statements show up the most. A few calls to sleep() could help narrow it down after that.
When you say you tried various JREs, are you sure? Is it possible there's an older JRE that appears earlier in your PATH?
I understand your problem - you've got a bug somewhere, and the debugging info you're being handed doesn't give you a whole lot to go by.
So run the code through a debugger, or if you don't have one start dropping println statements in there. You need to figure out what line of code triggers this error so you can work backwards from there to find the ultimate source of the problem.
publicvoid paint(Graphics g, JComponent c) {
Border vpBorder = scrollpane.getViewportBorder();
if (vpBorder != null) {
Rectangle r = scrollpane.getViewportBorderBounds();
vpBorder.paintBorder(scrollpane, g, r.x, r.y, r.width, r.height);
}
}
There is a check on vpBorder before it's dereferenced, so it can't be causing the exception.
Since scrollpane.getViewportBorderBounds() creates a new rectangle and returns it, it cannot be null.
Therefore the only reference that can be null is scrollpane.
The scrollpane field is set in the installUI() and the uninstallUI() methods. The normal JComponent implementation maintains the inverse relation, so that a component won't call the ui paint method of any ui that isn't installed.
Are you doing anything with these two methods that would break the association?