I have this exception and I don't understand why it would be thrown or, how I should handle it.
try {
os.writeObject(element);
} catch (IOException e) {
e.printStackTrace();
}
Where element is a TransformGroup containing some other TransformGroups an instance of the class Atom:
public class Atom extends Group implements Serializable{
float pozX,pozY;
Group group= new Group();
Color3f blue = new Color3f(new Color(255));
Color3f black = new Color3f(new Color(0));
Sphere AtSph=new Sphere();
public Atom(final float WEIGHT, final int BOUNDS,final float radius,Color3f color)
{
AppSetting ap= new AppSetting(color, black);
AtSph=new Sphere(radius,1,100,ap);
}
}
The full error log:
java.io.NotSerializableException: javax.media.j3d.TransformGroup
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at cls.MolecularBuilder.addAtom(MolecularBuilder.java:511)
at cls.MolecularBuilder$Console.HidrogenItemActionPerformed(MolecularBuilder.java:897)
at cls.MolecularBuilder$Console$2.actionPerformed(MolecularBuilder.java:746)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$200(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)
AppSetting (in Atom class) is just a custom class that extends Appearance.
The fields of your object have in turn their fields, some of which do not implement Serializable. In your case the offending class is TransformGroup. How to solve it?
if the class is yours, make it Serializable
if the class is 3rd party, but you don't need it in the serialized form, mark the field as transient
if you need its data and it's third party, consider other means of serialization, like JSON, XML, BSON, MessagePack, etc. where you can get 3rd party objects serialized without modifying their definitions.
java.io.NotSerializableException can occur when you serialize an inner class instance because:
serializing such an inner class instance will result in serialization
of its associated outer class instance as well
Serialization of inner classes (i.e., nested classes that are not
static member classes), including local and anonymous classes, is
strongly discouraged
Ref: The Serializable Interface
Make the class serializable by implementing the interface java.io.Serializable.
java.io.Serializable - Marker Interface which does not have any methods in it.
Purpose of Marker Interface - to tell the ObjectOutputStream that this object is a serializable object.
As mentioned above if the class is a 3rd party class you need to convert it to a JSON/XML/BSON object as the 3rd party class object cannot be serialized.
I was working on my project and had the same issue and i used gson library to convert my class object to a JSON String. Afterwhich i used this String in that object and passed it through ObjectOutputStream object.
And over the client side i used the same to revert the JSON String back to the 3rd party class object.
You can download the jar from over here : https://jar-download.com/artifacts/com.google.code.gson/gson/2.8.2/source-code!
Here's how i did it :
// Method that returns String through a Object as a parameter i.e to be converted
public static String generateJSONStringFromObject(ThirdPartyClass object) throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
String jsonString = mapper.writeValueAsString(object);
return jsonString;
}
// Method that returns Object through a String as a parameter
public static ThirdPartyClass generateObjectFromJSONString(String jsonString){
Gson gson = new Gson();
ThirdPartyClass thirdPartyClassObject = gson.fromJson(jsonString, ThirdPartyClass.class);
return thirdPartyClassObject;
}
Related
There are two types of agents in my Model:
AgentA - it's a parcel which must be delivered to the DeliveryPoint. This AgentA has a corresponding parameter "DeliveryPoint".
AgentB - it's a package (an agent-container) containing parcels (AgentA). This AgentB has a parameter "DeliveryPoint" too that depends on the "DeliveryPoint" of the parcel (AgentA) delivering at the moment.
I need to count agents AgentA in the agent-container AgentB which meet the given condition:
AgentA.DeliveryPoint == AgentB.DeliveryPoint
I tried this expression:
count(agentB.contents(), p -> ((AgentA)agentB.contents()).DeliveryPoint == agentB.DeliveryPoint))
Code example:
private void _movingToDeliveryPoint_onExit_xjal( final com.anylogic.libraries.processmodeling.Delay<AgentB> self, AgentB agent ) {
traceln(count(agent.contents(), p -> ((AgentA)agent.contents()).DeliveryPoint == agent.DeliveryPoint));
}
but when I run my Model, I recieve this error in the block "Delay" ("movingToDeliveryPoint") that executes this code:
java.base/java.util.Collections$UnmodifiableList cannot be cast to smm_chain.AgentA
java.lang.ClassCastException: java.base/java.util.Collections$UnmodifiableList cannot be cast to smm_chain.AgentA
at smm_chain.sc.lambda$0(sc.java:5168)
at com.anylogic.engine.UtilitiesCollection.count(Unknown Source)
at smm_chain.sc._movingToDeliveryPoint_onExit_xjal(sc.java:5168)
at smm_chain.sc.access$25(sc.java:5163)
at smm_chain.sc$17.onExit(sc.java:2319)
at smm_chain.sc$17.onExit(sc.java:1)
at com.anylogic.libraries.processmodeling.Delay.b(Unknown Source)
at com.anylogic.libraries.processmodeling.Delay.b(Unknown Source)
at com.anylogic.libraries.processmodeling.Delay$6.onExit(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBuffer.c(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBuffer.c(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBuffer$1.onExit(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock.c(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock$1.b(Unknown Source)
at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)
at com.anylogic.libraries.processmodeling.InPort.receiveImmediately(Unknown Source)
at com.anylogic.libraries.processmodeling.InputBlock$1.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutPort.b(Unknown Source)
at com.anylogic.libraries.processmodeling.OutPort.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock$2.a(Unknown Source)
at com.anylogic.libraries.processmodeling.OutputBlock$2.action(Unknown Source)
at com.anylogic.libraries.processmodeling.AsynchronousExecutor_xjal.executeActionOf(Unknown Source)
at com.anylogic.engine.EventTimeout.execute(Unknown Source)
at com.anylogic.engine.Engine.b(Unknown Source)
at com.anylogic.engine.Engine.nb(Unknown Source)
at com.anylogic.engine.Engine.k(Unknown Source)
at com.anylogic.engine.Engine$a.run(Unknown Source)
Could you help me?
Thanks a lot!
You cannot use agentB.contents(). Although it looks like a good method, it is not supposed to be used by modellers.
Also, do not compare objects using ==. Use the equals() method instead.
Do this, assuming you have a population or list named myAgentsA of AgentA within AgentB:
count(myAgentsA, a->a.DeliveryPoint.equals(DeliveryPoint))
This assumes you call the code from within AgentB
I'm quite new to java, so i'm sorry if i'm not noticing some mistake i made.
As the title said, when i build a project which is perfectly running on Netbeans 7.2.1 and then double click on the jar file, nothing happens.
Checking on the web i read it was suggested, in order to spot errors better, to go to the command prompt and write:
java -jar C:\Users\conserva\Documents\NetBeansProjects\EasyText\dist\EasyText.jar
It'gives me this:
C:\>java -jar C:\Users\conserva\Documents\NetBeansProjects\EasyText\dist\EasyText.jar
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at easytext.MainWindow.<init>(MainWindow.java:25)
at easytext.MainWindow$4.run(MainWindow.java:220)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(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.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)
This is the part of code where line 25 is:
public MainWindow() {
initComponents();
selectfolder.removeAllItems();
String tempordir = ("src/easytext/textfiles/");
String[] arr = new java.io.File(tempordir).list();
/*line 25 */ int len = arr.length;
//System.out.println("arr.lenght รจ: "+arr.length);
for (int i = 0; i<len; ++i){
selectfolder.addItem(arr[i]);}
selecttext.removeAllItems();
tempordir = ("src/easytext/textfiles/"+selectfolder.getSelectedItem().toString());
arr = new java.io.File(tempordir).list();
for (int i = 0; i<len; ++i){
selecttext.addItem(arr[i]);}
GetText("src/easytext/textfiles/"+selectfolder.getSelectedItem().toString()+"/"+selecttext.getSelectedItem().toString());
}
So it looks like the nullpointerecxeption is thrown as i refer to the lenght of len array.
How can i solve this?
Thanks in advance, and sorry once again if i made some dumb mistake or didn't see something clear. I'm really spending some time on this without getting to a solution.
From the documentation of File.list():
An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
So it sounds like the directory src/easytext/textfiles/ probably doesn't exist from where you're executing the code.
When I update the JList, I want to keep the selection of what it was before updating the JList. When updating a JList, it can either remove an object or add an object.
This is what I have right now:
Object obj = list.getSelectedValue(); // This is line 47
list.clearSelection();
list.setListData(peerList);
if(obj != null)
{
int selectedIndex = list.getNextMatch(obj.toString(), 0, Position.Bias.Forward);
if(selectedIndex != -1)
list.setSelectedIndex(selectedIndex);
else
list.clearSelection();
}
But then sometimes, when an object is added or removed, it would throw an exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 >= 3
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.JList$5.getElementAt(Unknown Source)
at javax.swing.JList.getSelectedValue(Unknown Source)
at MyThread$1.run(MyThread.java:47) // I marked up top where line 47 is
at java.awt.event.InvocationEvent.dispatch(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.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)
That seems to me like a concurrency issue. Are you updating the selection index and removing/adding elements all on the Event Dispatch Thread (EDT), or are you running concurrent threads that do this? You should only change the selection or modify the list on the EDT.
I figured out what my problem was. peerList is a Vector, so before this I do the adding and deleting of elements from the Vector, not the JList, though I don't know if this is possible. So if I deleted an object from the Vector and I asked to get the selected value from a JList before updating it, it wouldn't be able to find it. But the JList would still show the deleted element until you use setListData and update the JList.
I have this exception and I don't understand why it would be thrown or, how I should handle it.
try {
os.writeObject(element);
} catch (IOException e) {
e.printStackTrace();
}
Where element is a TransformGroup containing some other TransformGroups an instance of the class Atom:
public class Atom extends Group implements Serializable{
float pozX,pozY;
Group group= new Group();
Color3f blue = new Color3f(new Color(255));
Color3f black = new Color3f(new Color(0));
Sphere AtSph=new Sphere();
public Atom(final float WEIGHT, final int BOUNDS,final float radius,Color3f color)
{
AppSetting ap= new AppSetting(color, black);
AtSph=new Sphere(radius,1,100,ap);
}
}
The full error log:
java.io.NotSerializableException: javax.media.j3d.TransformGroup
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at cls.MolecularBuilder.addAtom(MolecularBuilder.java:511)
at cls.MolecularBuilder$Console.HidrogenItemActionPerformed(MolecularBuilder.java:897)
at cls.MolecularBuilder$Console$2.actionPerformed(MolecularBuilder.java:746)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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$200(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)
AppSetting (in Atom class) is just a custom class that extends Appearance.
The fields of your object have in turn their fields, some of which do not implement Serializable. In your case the offending class is TransformGroup. How to solve it?
if the class is yours, make it Serializable
if the class is 3rd party, but you don't need it in the serialized form, mark the field as transient
if you need its data and it's third party, consider other means of serialization, like JSON, XML, BSON, MessagePack, etc. where you can get 3rd party objects serialized without modifying their definitions.
java.io.NotSerializableException can occur when you serialize an inner class instance because:
serializing such an inner class instance will result in serialization
of its associated outer class instance as well
Serialization of inner classes (i.e., nested classes that are not
static member classes), including local and anonymous classes, is
strongly discouraged
Ref: The Serializable Interface
Make the class serializable by implementing the interface java.io.Serializable.
java.io.Serializable - Marker Interface which does not have any methods in it.
Purpose of Marker Interface - to tell the ObjectOutputStream that this object is a serializable object.
As mentioned above if the class is a 3rd party class you need to convert it to a JSON/XML/BSON object as the 3rd party class object cannot be serialized.
I was working on my project and had the same issue and i used gson library to convert my class object to a JSON String. Afterwhich i used this String in that object and passed it through ObjectOutputStream object.
And over the client side i used the same to revert the JSON String back to the 3rd party class object.
You can download the jar from over here : https://jar-download.com/artifacts/com.google.code.gson/gson/2.8.2/source-code!
Here's how i did it :
// Method that returns String through a Object as a parameter i.e to be converted
public static String generateJSONStringFromObject(ThirdPartyClass object) throws JsonProcessingException{
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
String jsonString = mapper.writeValueAsString(object);
return jsonString;
}
// Method that returns Object through a String as a parameter
public static ThirdPartyClass generateObjectFromJSONString(String jsonString){
Gson gson = new Gson();
ThirdPartyClass thirdPartyClassObject = gson.fromJson(jsonString, ThirdPartyClass.class);
return thirdPartyClassObject;
}
I have read through all the threads out there that looked as if they could solve my problem and I've also read all the answers on here, but I'm still at my wit's end. I'm not sure why the exception is thrown, although I have a feeling this might be to do with threading. If it does, please let me know where to include the new Runnable()and whether to invokeLater() or invokeAndWait(), as I have tried it to no avail.
Please bear with me while I give you the code that leads to the exception + the stacktrace (below).
EDIT: I had included a number of sysos in the AnnoTable section just before tableChanged is called and they don't show up in the console, hence I think the problem must occur even before the application gets to that point, i.e. either when it's called from AAView or when data and table model are instantiated...
EDIT II: The problem was the overwritten tableChanged method. That would obviously fire an Exception. I've removed the tableChanged() call (which wouldn't make a difference) as well. Now I've got another problem: understanding how a change in the underlying data (AnnoData) can automatically update the table. Although this is perhaps for another query (after an extended Google search), please feel free to post helpful comments in this thread, as I'll continue reading it... THANKS A LOT for all the helpful comments and tips!
EDIT III:* I've solved the problem. I needed to instantiate another object from AnnoData, pass that to a new instance of AnnoTableModel, set this instance to my table and THEN fireTableDataChanged().
EDIT IV: Okay, so fireTableDataChanged() (as used in EDIT III) is unnecessary after all. I still would want to use it rather than creating new objects all the time. I guess I should ask a new question... Thanks!
This method in AAView should create an object extending a JTable, put it into a JScrollPane, etc. (the latter does work).
private JPanel createAnnoTablePanel() {
annoTablePanel = new JPanel();
annoTable = new AnnoTable(aameth);
setAnnoTable(annoTable);
JScrollPane scrollPane = new JScrollPane(getAnnoTable());
annoTablePanel.add(scrollPane);
return annoTablePanel;
}
Here is the class AnnoTable (aameth is an instance object containing business logic to access a data model, works fine).
public class AnnoTable extends JTable implements TableModelListener
{
public AnnoTable(AAMethods aameth)
{
int tokenCount = aameth.getTokenCount();
AnnoData annoData = new AnnoData(aameth); // cf. below, AnnoData is a Vector(Vector<Object>,String[])
TableModel tableModel = new AnnoTableModel(annoData.getAnnoData(),
// AnnoTableModel extends AbstractTableModel(Vector, String[])
annoData.getColTitles());
setModel(tableModel);
getModel().addTableModelListener(this);
TableModelEvent tme = new TableModelEvent(tableModel);
this.tableChanged(tme);
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
setCellSelectionEnabled(true);
getColumnModel().getSelectionModel().addListSelectionListener(new AnnoTableSelectionListener(this));
setPreferredScrollableViewportSize(this.getPreferredSize());
}
public void tableChanged(TableModelEvent e) {
int row = e.getFirstRow();
int column = e.getColumn();
AbstractTableModel model = (AbstractTableModel)e.getSource();
String columnName = model.getColumnName(column);
Object data = model.getValueAt(row, column); // This is where the exception is thrown!
}
}
If you need the source code for AnnoTableModel() (which is a fairly generic extension of AbstractTableModel) or AnnoData (which constructs a Vector containing three Vector<Object> and a String[] for column titles), please let me know.
Here's the stacktrace.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at package.AnnoTable.tableChanged(AnnoTable.java:52)
at javax.swing.JTable.setModel(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at javax.swing.JTable.<init>(Unknown Source)
at package.AnnoTable.<init>(AnnoTable.java:25)
at package.AAView.createAnnoTablePanel(AAView.java:464)
at package.AAView.createNorthPanel(AAView.java:455)
at package.AAView.displayAndAnnotate(AAView.java:444)
at package.AAView.loadProject(AAView.java:333)
at package.AAView.actionPerformed(AAView.java:286)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(Unknown Source)
at javax.swing.JMenuBar.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(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$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 can see the EDT in there so from what I've learned this might really be a problem of threading. However, I don't know how to find out where I should start a new Thread (or invoke a new Runnable().
On a side note, the Exception only started appearing when I changed AnnoTable to extend JTable rather than JPanel. Originally I had AnnoTable not only construct the table but also wrap it in a scroll pane and add this to a new JPanel. But because I wanted to fireTableDataChanged from a Class that only knew of AAView (which also features a setAnnoTable() method) I wanted to do it the correct way, whereas before it worked just fine. Murphy's law?
A rowIndex of -1 (==TableModelEvent.HEADER_ROW) indicates that the model's structure has changed completely. Such an event is fired internally by the JTable on setModel. Read the api doc of TableModelEvent to fully understand which types/values to expect in the listener's tableChanged.
BTW, #AKJ is right - no need to fire any TableModelEvents in your table code. Make the model fire the events as appropriate
This means that you are passing -1 as row or column. This is not permitted - make sure you pass a correct value.
I have a feeling that your problem is here:
TableModelEvent tme = new TableModelEvent(tableModel);
this.tableChanged(tme);
->
int column = e.getColumn();
AbstractTableModel model = (AbstractTableModel)e.getSource();
String columnName = model.getColumnName(column);
Because you haven't specified a row or column value, the getColumn() and getRow() calls will return -1, which you are then passing to getValueAt().
Try looking at the constructor for TableModelEvent. It has options for specifying those row/column values.
TableModelEvent tme = new TableModelEvent(tableModel);
this.tableChanged(tme);
I don't see any need of this call. As pointed by other posters this is the cause of your problem.
If you are implementing the table model correctly, whenever you update the table model the jtable will automatically get notifications and you don't need to write the tableChanged() method as well. So I am lost why you need to call tableChanged() explicitly.
Whenever you want to update the table just update the model. Also at first glance there does not seem to be any threading issue involved.
Your AnnoTable constructor is leaking references to the incompletely constructed "this" object. Also, registering Listeners from the constructor is not safe. enter link description here
Build your objects doing the least amount of work possible in the constructor and then operate on the fully built objects. Add listeners, adjust the models, fireEvents etc...