Can't find the source of an exception in Java - java

Basically an exception is being thrown and I can't find the reason. Here is what I get on the console:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at org.apache.batik.gvt.renderer.StrokingTextPainter.computeTextRuns(Unknown Source)
at org.apache.batik.gvt.renderer.StrokingTextPainter.getTextRuns(Unknown Source)
at org.apache.batik.gvt.renderer.StrokingTextPainter.getOutline(Unknown Source)
at org.apache.batik.gvt.renderer.BasicTextPainter.getGeometryBounds(Unknown Source)
at org.apache.batik.gvt.TextNode.getGeometryBounds(Unknown Source)
at org.apache.batik.gvt.TextNode.getSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.AbstractGraphicsNode.getTransformedSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getTransformedSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getTransformedSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getTransformedSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.getSensitiveBounds(Unknown Source)
at org.apache.batik.gvt.CompositeGraphicsNode.nodeHitAt(Unknown Source)
at org.apache.batik.gvt.event.AbstractAWTEventDispatcher.dispatchMouseEvent(Unknown Source)
at org.apache.batik.gvt.event.AbstractAWTEventDispatcher.dispatchEvent(Unknown Source)
at org.apache.batik.gvt.event.AWTEventDispatcher.dispatchEvent(Unknown Source)
at org.apache.batik.gvt.event.AbstractAWTEventDispatcher.mouseEntered(Unknown Source)
at org.apache.batik.swing.gvt.AbstractJGVTComponent$Listener.dispatchMouseEntered(Unknown Source)
at org.apache.batik.swing.svg.AbstractJSVGComponent$SVGListener.dispatchMouseEntered(Unknown Source)
at org.apache.batik.swing.gvt.AbstractJGVTComponent$Listener.mouseEntered(Unknown Source)
at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)
at java.awt.AWTEventMulticaster.mouseEntered(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.trackMouseEnterExit(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
It is obviously from a batik lib that I use to paint SVG files, but I made sure that nothing is painted until the document is loaded, ready and showing on screen. When thrown nothing is painted.
Another interesting thing is the timing of the throwing. I am unable to find any logical patern, as sometimes it is thrown as soon as I initiate the class and sometimes it needs more then five minutes. In addition to this, as far as I tested there is no single action that calls repaint() that triggers it or rather all do.
I am new to Java and all the other exceptions had the class and row number of where they were thrown so I don't know what to do here.
Any suggestions would be greatly appreciated.
The code is enormous so I'll put just the paint method and if anything additional is needed please say so.
#Override
public void paint(Graphics g) {
if(documentLoaded && showingOnScreen){
try{
rad = (int)(radInit+zoom*faktorRad); //max rad = 20
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
paintElements(g2d);
}
catch(NullPointerException nulle){
}
}
}
edit: There is no array in my class so i can't check any index. I think that this exception is thrown from a library I use, but it's a .jar file and I don't know how to open it or if I can.

It appears that the exception is thrown after a "mouse entered" event while the library is attempting to calculate something about the geometry. None of the classes in the stack trace appear to be listeners you have defined. I'm afraid I don't know anything about this library, but I suggest you go through the UI you have defined for things that aren't quite right, or even just things that you can remove and attempt to reproduce the problem. It looks like either something is wrong with the rendered geometry or the listener is getting called before it is supposed to, and depends on rendering that has not happened yet.

Related

Java: Invalid thread access - pop up menu

Within an eclipse plugin I am trying to create a pop up window that activates within an editor and adds syntax to a string depending on the selection chosen in the pop up window. I have an editor class that extends AbstractTextEditor and within it I am trying to use a keylistener to pop up a window that contains a list of string manipulation functions and then when an item in the list is selected I want to replace some selected content with the new string returned from the method in the pop up window. When I attempt to do this (with the code in the 2 linked files), I receive the error below:
Exception in thread "AWT-EventQueue-0" org.eclipse.swt.SWTException:
Invalid thread access
at org.eclipse.swt.SWT.error(SWT.java:4595)
at org.eclipse.swt.SWT.error(SWT.java:4510)
at org.eclipse.swt.SWT.error(SWT.java:4481)
at org.eclipse.swt.widgets.Widget.error(Widget.java:451)
at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:369)
at org.eclipse.swt.custom.StyledText.getSelectionRange(StyledText.java:4743)
at githubflavoredmarkdowneclipseplugin.MarkdownEditor.test(MarkdownEditor.java:140)
at autocomplete.AutoComplete$1.keyPressed(AutoComplete.java:50)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown
Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown
Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown
Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
It seems that when autoupdate class tries to inform the editor class that a change has been selected it cannot manipulate the styledText variable. Does anyone know how to fix this?
The two files that contain this code can be reviewed at these links:
http://www.filedropper.com/markdowneditor
http://www.filedropper.com/autocomplete
The reason you are getting that exception is because when you make changes that are UI related with SWT you have to execute a runnable vis Display.syncExec(myRunable) otherwise that exception will occur.

java security not allowing me to open take pics using Jfilechooser

I am making an application in which one of the features the constructor requires a picture. The picture is selected using the JFileChooser obviously and then displayed on a JLabel. My problem is I do not have security privilege to access the picture. I tested to see if I do get the absolute path and if the file exists and I did get the path and true for the latter. So how do I give my app access to at least just get pictures?
My code
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("jpg","png"));
int returnVal = chooser.showOpenDialog(diag);
if(returnVal == JFileChooser.APPROVE_OPTION) {
weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
weaponPic.setIcon(weaponImg);
weaponPic.revalidate();
weaponPic.repaint();
My error
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ui.TempDialogs$5.mouseClicked(TempDialogs.java:171)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
java security not allowing me ..
This has nothing to do with security, despite the stack trace mentioning 'security' in some of the lines. The real problem is at the very top of the stack trace, ..
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
And I expect it all starts with this line of code..
weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
Which is both wrong and unnecessarily convoluted.
Get resource is for producing an URL from resources on the application's class path, it is neither needed nor useful for files.
So in this case of trying to access a file, we can use either a File object, or a String the represents a path to a file on the file system. So it could be shortened to:
weaponImg = new ImageIcon(chooser.getSelectedFile().getAbsolutePath()); // use String
But as alluded to, it could also be loaded as a plain File, so this would also work:
weaponImg = new ImageIcon(chooser.getSelectedFile()); // use File!

How to find an (informative) stack trace of unknown exception thrown by Swing components

I have setup an UncaughtExceptionHandler in my Swing app which will catch and log any uncaught exceptions in my code and report them to me. The stack traces for most of these errors are helpful in diagnosing the issues. However, sometimes I get a problem like this:
Thread : AWT-EventQueue-2
Thread[AWT-EventQueue-2,6,javawsApplicationThreadGroup]
Error Message:
java.lang.Double cannot be cast to java.lang.Integer
Error String:
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer
StackTrace: class java.lang.ClassCastException
java.lang.Integer.compareTo(Unknown Source)
javax.swing.table.TableRowSorter$ComparableComparator.compare(Unknown Source)
javax.swing.DefaultRowSorter.compare(Unknown Source)
javax.swing.DefaultRowSorter.access$100(Unknown Source)
javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
javax.swing.DefaultRowSorter$Row.compareTo(Unknown Source)
java.util.ComparableTimSort.binarySort(Unknown Source)
java.util.ComparableTimSort.sort(Unknown Source)
java.util.Arrays.sort(Unknown Source)
javax.swing.DefaultRowSorter.sortExistingData(Unknown Source)
javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
javax.swing.DefaultRowSorter.toggleSortOrder(Unknown Source)
javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(Unknown Source)
java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
java.awt.Component.processMouseEvent(Unknown Source)
javax.swing.JComponent.processMouseEvent(Unknown Source)
java.awt.Component.processEvent(Unknown Source)
java.awt.Container.processEvent(Unknown Source)
java.awt.Component.dispatchEventImpl(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
java.awt.Container.dispatchEventImpl(Unknown Source)
java.awt.Window.dispatchEventImpl(Unknown Source)
java.awt.Component.dispatchEvent(Unknown Source)
java.awt.EventQueue.dispatchEventImpl(Unknown Source)
java.awt.EventQueue.access$500(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.awt.EventQueue$3.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.awt.EventQueue$4.run(Unknown Source)
java.security.AccessController.doPrivileged(Native Method)
java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
java.awt.EventQueue.dispatchEvent(Unknown Source)
java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.pumpEvents(Unknown Source)
java.awt.EventDispatchThread.run(Unknown Source)
I don't need help diagnosing this particular problem, the problem is clear (the user is trying to sort a JTable somewhere in the application, but the values in that column are Doubles, but the table declaration said to expect Integers). However, I have no idea by this stack trace which JTable is the source of the problem (and there are a LOT of tables in my app). Without knowing WHERE the problem is coming from, I have no way of fixing it.
So, is there any more useful debugging information I can obtain to help find the source of this problem and others like it?
Note: it's not just the fact that every line says "Unknown source," because none of the classes listed in the stack trace are my classes, they are all library classes. So even if they gave a line number to the problem, it would be to a class I cannot fix.
In cases like this I override something just to add debugging info. For example in this case, we see in the stack trace that DefaultRowSorter.setSortKeys was called, so we could use a class
public class MyRowSorter<M, I> extends DefaultRowSorter<M, I> {
....
#Override
public void setSortKeys(List<? extends SortKey> sortKeys) {
try {
super.setSortKeys(sortKeys);
} catch (Exception e) {
//
// print debugging info here!
//
throw e;
}
}
}
Now you can set MyRowSorter to sort your tables, add the necessary debugging info to MyRowSorter, and you can debug which table had the problem.

Problems in running I/O in EDT of applet

Our applet source code is kind of spaghetti (written in 2000, Java 1.3 then) and we want to recompile it to Java 1.6 or 1.7.
When I'm testing it, most of the Swing is OK but after sometime, an Exception occurred, which is EDT exception. Specifically, when a drag event is done, a series of EDT exceptions appear.
Is this something to with coding the I/O part in ActionListeners because I've read that it is bad to code I/O operations in action listeners, which EDT executes when a action is performed.
EDIT:
This is the recurring exception
Exception in thread "AWT-EventQueue-3" java.lang.NullPointerException
at javax.swing.BufferStrategyPaintManager.flushAccumulatedRegion(Unknown Source)
at javax.swing.BufferStrategyPaintManager.copyArea(Unknown Source)
at javax.swing.RepaintManager.copyArea(Unknown Source)
at javax.swing.JViewport.blitDoubleBuffered(Unknown Source)
at javax.swing.JViewport.windowBlitPaint(Unknown Source)
at javax.swing.JViewport.setViewPosition(Unknown Source)
at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.vsbStateChanged(Unknown Source)
at javax.swing.plaf.basic.BasicScrollPaneUI$Handler.stateChanged(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.fireStateChanged(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.setRangeProperties(Unknown Source)
at javax.swing.DefaultBoundedRangeModel.setValue(Unknown Source)
at javax.swing.JScrollBar.setValue(Unknown Source)
at javax.swing.plaf.basic.BasicScrollBarUI$TrackListener.setValueFrom(Unknown Source)
at javax.swing.plaf.basic.BasicScrollBarUI$TrackListener.mouseDragged(Unknown Source)
at java.awt.Component.processMouseMotionEvent(Unknown Source)
at javax.swing.JComponent.processMouseMotionEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
I guess it has something to do with the version of java plugin.
In java 1.6_10, a new version of this plugin is release, i just disabled the option in java found in control panel
Advance->Java Plug In ->Enable the next generation Java Plug in
When i disable this, this recurring error with no distinct behavior no longer appears.
I guess also it has something to do with our code written in the days of Java 1.3.

I thought that java interfaces could hold a subclass, but maybe not in ActionListener... correct?

The issue could be different then what I suspect, but I figure the problem is either I read wrongly when learning about interfaces or it applies differently inside of the action listener.
First off, here is my error:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.JButton cannot be cast to main.GUI
at main.GUI$3.actionPerformed(GUI.java:224)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
The code I am using is as follows:
this.select.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
((GUI) e.getSource()).tool = new Selection();
}
});
I have an interface called tool, which I make a global variable Tool tool; for the GUI class. Then I attempt in the method above to assign the Selection class to the var tool which implements Tool. Am I doing something wrong and how do I fix it? If you need more code just ask.
Also When I assign a new Selection() to the global var tool outside of the ActionListener it works... so that is what I don't understand.
I have the button inside of a toolbar.
Use Action, shown here, to encapsulate functionality.
Regarding
I thought that java interfaces could hold a subclass, but maybe not in ActionListener… correct?
No, this has nothing to do with your problem. Rather your problem is an incorrect cast pure and simple. The object returned by e.getsource() is not a GUI object but rather an AbstractButton, perhaps a JButton (it's the variable named "select"). The component that has the ActionListener added to it is the one that is returned by ActionEvent#getSource().
Also, I don't see where you have an interface holding a subclass anywhere.
could you do?:
this.select.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
tool = new Selection(); //??
}
});
It's hard to say what you should do as we really don't know what the structure of the rest of your code looks like.

Categories

Resources