Here is my working marshalling code:
For starters I have an Entity class with something like the following:
#XmlRootElement
#XmlAccessorType(XmlAccessType.NONE)
public final class Entity implements {
private Map<String, Component> components = new HashMap<String, Component>();
#XmlElementWrapper(name="components")
#XmlAnyElement
public List<Component> getComponentList() {
return new ArrayList<Component>(components.values());
}
public void setComponentList(List<Component> comps) {
for(Component c : comps) {
components.put(c.getComponentType(), c);
}
}
...
}
Component is a very varied interface but all implementations are nice and round beans carefully annotated.
Now, marshalling produces some good looking XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<entity>
<components>
<containerComponent>
<children>
<entity>
<components>
<containerComponent>
<children>
<entity>
<components>
<directionComponent>
<direction x="0.0" y="1.0"/>
</directionComponent>
<sizeComponent>
<size x="52.0" y="12.0"/>
</sizeComponent>
...
As you can see, each component type has its very own tag name but that doesn't seem to be enough for JAXB because
Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to tobacco.core.components.Component
at tobacco.core.components.Entity.setComponentList(Entity.java:121)
at tobacco.core.components.Entity$JaxbAccessorM_getComponentList_setComponentList_java_util_List.set(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.endPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.endPacking(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.finish(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endScope(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ItemsLoader.leaveElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at tobacco.core.serialization.XmlEntityConverter.toEntity(XmlEntityConverter.java:32)
at tobacco.core.loader.XmlLoader.loadEntityTree(XmlLoader.java:43)
at tobacco.game.test.main.Main.main(Main.java:75)
At the other end of the process I receive ElementNSImpl objects.
I though about using an ObjectFactory but after reading and testing I'm almost convinced it's no use: I couldn't find a way to access tag name on the creator method, and even if I could I'm not so sure JAXB would just accept my object and fill its properties happily or if I would have to fill them myself, which absolutely impractical.
I think this guy had the same problem. Does it work out of the box with MOXy? Is there a way to do this without switching implementations?
You can use #XmlElements annotation instead of #XmlAnyElement and list all possible subclasses like in example:
#XmlElementWrapper(name = "components")
#XmlElements(
{
#XmlElement(name = "directionComponent", type = DirectionComponent.class),
#XmlElement(name = "sizeComponent", type = SizeComponent.class),
})
public List<Component> getComponentList()
{
return new ArrayList<Component>(components.values());
}
Also, in this case you don't need to use #XmlRootElement annotation on DirectionComponent and SizeComponent classes.
PS. Also, now you can use List of components instead of Map
#XmlRootElement
#XmlAccessorType(XmlAccessType.NONE)
public final class Entity
{
private List<Component> components = new ArrayList<Component>();
#XmlElementWrapper(name = "components")
#XmlElements(
{
#XmlElement(name = "directionComponent", type = DirectionComponent.class),
#XmlElement(name = "sizeComponent", type = SizeComponent.class),
})
public List<Component> getComponentList()
{
return components;
}
public void setComponentList(List<Component> comps)
{
this.components = comps;
}
//...
}
Related
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;
}
In my application I am working with a JTable containing a DefaultTableModel. Everything is working fine until the point I want to clear my table and start with a new DefaultTableModel. The clearing process is done like this:
public static JTable table = new JTable;
public static CustomTableModel model = new CustomTableModel();
[...]
table.setModel(new DefaultTableModel());
model = new CustomTableModel();
model.createEmptyModel();
table.setModel(model);
// model.save(config.getDatabaseFile(), table); // <<< Reference A
model.initColumModel(table);
So far so good. Now my CustomTableModel extends from DefaulTableModel and adds some of my custom methods. One of them is createEmptyModel() which looks like this:
public void createEmptyModel() {
model = new DefaultTableModel(new Object[][] {},
new String[] { "Lfd. Nr.", "FB Nr.", "Auftr. / Meld. Nr.", "Betra Nr.", "Datum", "Bahnhof", "Str. Km.",
"Sprz.", "Arb. Zeit", "Mitarbeiter", "Auftrag / Objekt(e)", "Anmerkungen", "Fertig" }) {
private static final long serialVersionUID = -4325766838779239822L;
#SuppressWarnings("rawtypes")
Class[] columnTypes = new Class[] { Integer.class, String.class, Long.class, Long.class, String.class,
String.class, String.class, String.class, String.class, String.class, String.class, String.class, Boolean.class };
#SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
};
// this sequentialNumber is just some object which contains a counter. Not important here!
sequentialNumber.reset();
}
With this method I initialize my model with all the needed vectors and types.
When model.initColumModel(table) get's called my application crashes with this exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source)
at somepacket.CustomTableModel.initColumModel(CustomTableModel.java:36)
at somepacket.DBDatabaseSystem.createNewDatabase(DBDatabaseSystem.java:416)
at somepacket.DBDatabaseSystem$4.actionPerformed(DBDatabaseSystem.java:155)
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$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$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)
My initColumModelmethod is declared like this:
public void initColumModel(JTable table) {
// doing this here to avoid calling table.getColumnModel multiple times
TableColumnModel columnModel = table.getColumnModel();
columnModel.getColumn(0).setResizable(false);
columnModel.getColumn(0).setPreferredWidth(50);
columnModel.getColumn(1).setResizable(false);
columnModel.getColumn(1).setPreferredWidth(50);
columnModel.getColumn(2).setResizable(false);
columnModel.getColumn(2).setPreferredWidth(81);
columnModel.getColumn(3).setResizable(false);
columnModel.getColumn(3).setPreferredWidth(81);
columnModel.getColumn(4).setPreferredWidth(81);
columnModel.getColumn(5).setPreferredWidth(134);
columnModel.getColumn(6).setPreferredWidth(123);
columnModel.getColumn(7).setPreferredWidth(95);
columnModel.getColumn(8).setPreferredWidth(95);
columnModel.getColumn(9).setPreferredWidth(226);
columnModel.getColumn(10).setResizable(true);
columnModel.getColumn(10).setPreferredWidth(300);
columnModel.getColumn(11).setResizable(true);
columnModel.getColumn(11).setPreferredWidth(102);
columnModel.getColumn(12).setResizable(false);
columnModel.getColumn(12).setMinWidth(45);
columnModel.getColumn(12).setMaxWidth(45);
}
From what I can the problem is that my CustomTableModel has no columns after it's creation. So what I tried was adding a new row to the table and call initColumModel afterwards, but this did not help. The application still crashes. The only way to prevent the crash is saving the model before calling my initColumModel method. But this makes no sense, see for yourself, this is my save method, that changes nothing on the model nor add any column:
[...]
try {
// undo all selections to prevent user from editing while saving
table.clearSelection();
// prevent saving when user is editing a cell
if (table.isEditing()) {
table.getCellEditor().stopCellEditing();
}
fos = new FileOutputStream(filename);
out = new ObjectOutputStream(fos);
table.setModel(new DefaultTableModel());
// detach model from table prior serializing
out.writeObject(model);
// re-attach table model
table.setModel(model);
// set layout again
initColumModel(table);
out.writeObject(sequentialNumber);
[...]
After saving my model, it appears in the JTable and everything works perfect. How could this be? I tried detaching and reattaching the model from and to the table manually before calling initColumModel but this did not help either.
Basically I am lost now, how can I solve my problem? Or am I doing something obviously wrong?
Thanks in advance!
If you just want to clear the data in the model then you just use:
model.setRowCount(0);
if you want to replace the TableModel with a new structure (ie. new column headings and data) then you just create a new TableModel:
TableModel model = new DefaultTableModel(...);
table.setModel( model );
From what I can the problem is that my CustomTableModel has no columns after it's creation
Sure it has columns if you implement the getColumnCount() and getColumnNames() method correctly.
The TableColumnModel of the JTable is not created until you actuallyl set a model to the table. When you use new DefaultTableModel() you are creating a TableColumnModel with zero columns. So add a proper TableModel to the table if you want columns to exist.
// detach model from table prior serializing
Why are you detaching the model from the table? This is unnecessary.
Instead of serializing the DefaultTableModel you might want to look at using an XMLEncoder which is the recommend approach for long term storage of objects. See: Saving content of Interactive JTable to .txt file to read it upon next run for a working example of this approach.
You are accessing columns that the model does not have actually. Before each of your columnModel.get()... call add
columnModel.addColumn(new TableColumn(int index));
This question is helpful:
java.lang.ArrayIndexOutOfBoundsException: 0 >= 0 attempting to populate JTable
There is also noted that the same error is thrown if you edit the columns from a non GUI thread, check that this is not the issue
Running my program throws A Null Pointer Exception every time I create a new token of a specific type.
Here's the error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Pattern.draw(Pattern.java:98)
at GameToken.draw(GameToken.java:68)
at GameTokenPanel.paintComponent(GameTokenPanel.java:83)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1100(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
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)
Here are the parts in the class which are affected by utoken0.draw(g2) and utoken1.draw(g2);
Here's my pattern class
public class Pattern
{
public static final int pattern0=0;
public static final int pattern1=1;
public static final int pattern2=2;
//private CrossPattern pattern1;
//private LineMid pattern3;
//private CircPattern pattern2;
private Rectangle bbox;
private int type;
private Random random = new Random();
private CrossPattern cross;
private CircPattern circ;
private LineMid linemid;
public Pattern(int type, Rectangle bbox)
{
this.bbox=bbox;
type=type;
if (type == 0)
{
cross= new CrossPattern(bbox);
}
if( type == 1)
{
circ=new CircPattern(bbox);
}
if(type ==2)
{
linemid=new LineMid(bbox);
}
}
public int getType()
{
return type;
}
public void draw(Graphics2D g2)
{
if(type==pattern0)
cross.draw(g2);
else if(type==pattern1)
circ.draw(g2);
else if (type==pattern2)
linemid.draw(g2);
}
}
Here is my GameToken Class:
public class GameToken implements VisibleShape
{
private boolean visible;
public Rectangle bbox;
private Pattern pattern;
private Color color;
Random random = new Random();
public GameToken(int patternType, int x, int y, int width, int height)
{
bbox = new Rectangle( x,y,width,height);
if(patternType==0)
{
pattern = new Pattern(patternType,bbox);
}
else if(patternType==1)
{
pattern= new Pattern(patternType,bbox);
}
else if(patternType==2)
{
pattern = new Pattern(patternType,bbox);
}
}
public void draw(Graphics2D g2)
{
g2.draw(bbox);
pattern.draw(g2);
}
}
Here is my panel:
public class GameTokenPanel extends JPanel {
private GameToken token1;
private final int TOKEN_WIDTH=35;
private Random random = new Random();
private ArrayList <GameToken> tokenarr;
private GameToken utoken0 = new GameToken(0, 0,0,TOKEN_WIDTH,TOKEN_WIDTH);
private GameToken utoken1 = new GameToken(1, 0,0,TOKEN_WIDTH,TOKEN_WIDTH);
private GameToken utoken2 = new GameToken(2, 0,0,TOKEN_WIDTH,TOKEN_WIDTH);
private GameToken currentToken= utoken0;
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2= (Graphics2D) g;
g2.setColor(Color.BLUE);
utoken0.draw(g2);
utoken1.draw(g2);
//utoken2.draw(g2);
g2.setColor(Color.RED);
//token1.draw(g2);
for (int i =0; i<=10;i++)
{
tokenarr.get(i).draw(g2);
}
}
}
So basically, I use the panel to create the Game tokens which would create the Pattern and the rectangle. The Pattern class would then call the specific pattern class and draw it using the rectangle dimensions. Which would then appear on a frame.
The issue is that I would create utoken0 in the panel and draw it in the draw method. But when I would draw utoken1 the same way. It would lead to errors. I initiated them the same way and drew the same way . But I am getting error. Also , if I attempt to draw utoken2, it would also give me an error.
Also,when I generate the random tokens at the beginning of panel, It creates the 3 types. But when I would run the utoken and pick a specific type. Only the 0 pattern works, however 1 and 2 does not.
I don't have testing environment, simply dry run on your code.
Your code failure to set correct value into class variable type in Pattern. Hence, the type variable will have default value 0.
In draw method of Pattern class, it will call cross.draw(g2); since the type is 0. However, the code never initiate cross for pattern 1 or 2. It results in NullPointerException.
You should fix line in Pattern class from:
type=type;
to
this.type=type;
My XML and Bean has perfect match for property "CostCenter" but still am having thiss issue .Please help to solve this.
Exception :
Sep 13, 2013 4:25:23 PM org.apache.commons.digester3.Digester endElement
SEVERE: End event threw exception
java.lang.NoSuchMethodException: Bean has no property named CostCenter
at org.apache.commons.digester3.BeanPropertySetterRule.end(BeanPropertySetterRule.java:206)
at org.apache.commons.digester3.Digester.endElement(Digester.java:1097)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.commons.digester3.Digester.parse(Digester.java:1588)
at org.apache.commons.digester3.Digester.parse(Digester.java:1557)
at ecnet.yes.po.digester.DigestPO.digest(DigestPO.java:80)
at ecnet.yes.po.digester.DigestPO.main(DigestPO.java:16)
Sep 13, 2013 4:25:23 PM org.apache.commons.digester3.Digester parse
SEVERE: An error occurred while parsing XML from 'file:/D:/workspace/Digester/src/ecnet/yes/po/digester/YES_PO.xml', see nested exceptions
DigestPO.java
Digester digester = new Digester();
//Push the current object onto the stack
digester.push(this);
digester.addSetProperties("ECnetPO");
//Creates a new instance of the YESBarePO class
digester.addObjectCreate("ECnetPO/Header", YESBarePO.class );
//Uses setName method of the YESBarePO instance
//Uses tag name as the property name
digester.addBeanPropertySetter("ECnetPO/Header/PONumber","PONumber");
digester.addBeanPropertySetter("ECnetPO/Header/CostCenter","CostCenter");
xml :
<ECnetPO>
<Header>
<PONumber>PONUMB0116</PONumber>
<CostCenter>Cost Center 20</CostCenter>
</Header>
<Header>
<PONumber>PONUMB0116</PONumber>
<CostCenter>Cost Center 20</CostCenter>
</Header>
</ECnetPO>
YESBarePO.java
private String PONumber;
private String CostCenter;
public String getPONumber()
{
return PONumber;
}
public void setPONumber(String pONumber)
{
PONumber = pONumber;
}
public String getCostCenter()
{
return CostCenter;
}
public void setCostCenter(String costCenter)
{
CostCenter = costCenter;
}
Just a guess, but does it make a difference if you rename the instance variable YESBarePO.CostCenter to be YESBarePO.costCenter instead? Unlike .NET, for instance, the standard Java idiom is to use lowerCamelCase for instance variables and my experience suggests that some bean mappers require this convention to be honoured in order to work.
My program seems to work quite well, but I keep getting "IllegalStateExceptions: RunnableQueue not started or has exited" from time to time, when I try to resize my component. I have set the documentState to ALWAYS_DYNAMIC and I have read that you are supposed to use the JSVGCanvas' UpdateManager and call invokelater(). I understand that it is available after the first time that
gvtBuildCompleted(GVTTreeBuilderEvent e)
is called, so I check whether it is running before I use it but I still get the exception.
The following method is called by a thread repeatedly and seems to cause the exception:
private void updateDomTree(final SVGComponent component, final Document doc)
{
if(component.getSvgCanvas().getUpdateManager() != null && component.getSvgCanvas().getUpdateManager().isRunning())
{
component.getSvgCanvas().getUpdateManager().getUpdateRunnableQueue().invokeLater(new Runnable()
{
public void run()
{
final Node newNode = doc.getChildNodes().item(0).getFirstChild();
//could be easier to get this value, but ... it works.
String newNodeId = newNode.getAttributes().getNamedItem("id").getFirstChild().getNodeValue();
NodeList nodes = component.getSvgDocument().getDocumentElement().getChildNodes();
Node updateNode = findElementById(nodes, newNodeId);
resizeComponent(component, doc);
component.getSvgCanvas().getSVGDocument().adoptNode(newNode);
component.getSvgCanvas().getSVGDocument().getDocumentElement().replaceChild(newNode, updateNode);
component.refreshSVGCanvas();
}
});
}
}
The actual resizing is done here:
protected void resizeComponent(SVGComponent component, Document doc)
{
Element svgRoot = doc.getDocumentElement();
final int svgWidth = Integer.parseInt(svgRoot.getAttribute("width"));
final int svgHeight = Integer.parseInt(svgRoot.getAttribute("height"));
String[] viewBox = svgRoot.getAttribute("viewBox").split(" ");
int viewBoxLeft = Integer.parseInt(viewBox[0]);
int viewBoxTop = Integer.parseInt(viewBox[1]);
final float factor = component.getScaleFactor();
String[] viewBoxOld = component.getSvgDocument().getDocumentElement().getAttribute("viewBox").split(" ");
int viewBoxLeftOld = Integer.parseInt(viewBoxOld[0]);
int viewBoxTopOld = Integer.parseInt(viewBoxOld[1]);
int xDiff = (int) ((viewBoxLeftOld - viewBoxLeft)*factor);
int yDiff = (int) ((viewBoxTopOld - viewBoxTop)*factor);
if ( viewBoxLeftOld != viewBoxLeft ) //If there is additional content left
{
component.setLocation(component.getLocation().x - xDiff, component.getLocation().y);
}
if ( viewBoxTopOld != viewBoxTop ) //If there is additional content right)
{
component.setLocation(component.getLocation().x, component.getLocation().y - yDiff);
}
component.getSvgDocument().getDocumentElement().setAttribute("width",""+svgWidth);
component.getSvgDocument().getDocumentElement().setAttribute("height",""+svgHeight);
component.getSvgDocument().getDocumentElement().setAttribute("viewBox", ""+viewBoxLeft+" "+viewBoxTop+" "+svgWidth+" "+svgHeight);
component.setSize((int)(svgWidth*factor),(int)(svgHeight*factor));
}
The method
refreshJSVGCanvas()
calls
JSVGCanvas.setDocument(Document);
JSVGCanvas.setSize(int, int);
Here's the full stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: RunnableQueue not started or has exited
at org.apache.batik.util.RunnableQueue.invokeLater(RunnableQueue.java:277)
at org.apache.batik.swing.svg.AbstractJSVGComponent.updateRenderingTransform(AbstractJSVGComponent.java:1057)
at org.apache.batik.swing.gvt.AbstractJGVTComponent$1.componentResized(AbstractJGVTComponent.java:237)
at java.awt.AWTEventMulticaster.componentResized(Unknown Source)
at java.awt.Component.processComponentEvent(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.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)
Thanks in advance, I have searched everywhere and tried a lot, but could not find a solution.
Edit:
This is the invokeLater-Method of Batik where the Exception is actually thrown:
public void invokeLater(Runnable r) {
if (runnableQueueThread == null) {
throw new IllegalStateException
("RunnableQueue not started or has exited");
}
synchronized (list) {
list.push(new Link(r));
list.notify();
}
}
runnableQueueThrad is set inside that class' run()-Method and set to null at the end.
So I guess I have to do some kind of synchronization.
Hazard a guess, the "public void run()" code should not be inside another method and really is a thread class/interface objects so called constructor(interface version actually).
Remove it to its own class(e.g. nested subclass to preserve scope) and implement the "thread runnable" interface on the class to place the "run()" method in it to use.
Stack trace says the run method is not available because it does not actually have such a method(or at least not properly declared) so it is in an "illegal state".