0

when i try to store my CreateNode object in a xml file it gives me the following error

java.lang.InstantiationException: GraphPanel$CreateNode
Continuing ...
java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject(GraphPanel$CreateNode);
Continuing ...

here is my node class

public static class CreateNode {
  private Point p;
  private int r;
  private String NAME;
  private String nodeid;
  private boolean selected = false;
  private boolean fnode;
  private Rectangle b = new Rectangle();
  /**
   * Construct a new node.
   */
  public CreateNode(String nodeid, Point p, int r, String NAME, boolean fnode) {
    this.nodeid = nodeid;
    this.p = p;
    this.r = r;
    this.NAME = NAME;
    setBoundary(b);
    System.out.print(p.toString());
  }
  /**
   * Calculate this node's rectangular boundary.
   */
  private void setBoundary(Rectangle b) {
    b.setBounds(p.x - r, p.y - r, 2 * r, 2 * r);
  }
  /**
   * Select each node in r.
   */
  public static void selectRect(List<CreateNode> list, Rectangle r) {
    for (CreateNode n: list) {
      n.setSelected(r.contains(n.p));
    }
  }
  /**
   * Toggle selected state of each node containing p.
   */
  public static void selectToggle(List<CreateNode> list, Point p) {
    for (CreateNode n: list) {
      if (n.contains(p)) {
        n.setSelected(!n.isSelected());
      }
    }
  }
}

and an object is created in the following class

private class NewNodeAction extends AbstractAction {
  String Name;
  boolean firstnode = false;
  public NewNodeAction(String name) {
    super(name);
    this.Name = name;
  }
  public void actionPerformed(ActionEvent e) {
    CreateNode.selectNone(nodes);
    Point p = mousePt.getLocation();
    String Nodeid = ID.giveid();
    CreateNode n = new CreateNode(Nodeid, p, radius, Name, firstnode);
    n.setSelected(true);
    nodes.add(n);
    repaint();
  }
}

And i try to serialize/store the object in xml file here but it gives me error

private class Serialize extends AbstractAction {
  public Serialize(String name) {
    super(name);
  }
  public void actionPerformed(ActionEvent e) {
    ListIterator<CreateNode> Fiter = nodes.listIterator();
    while (Fiter.hasNext()) {
      CreateNode n = Fiter.next();
      FileOutputStream os;
      try {
        os = new FileOutputStream("D:/DM1/pert.xml");
        XMLEncoder encoder = new XMLEncoder(os);
        encoder.writeObject(n);
        encoder.close();
      }
      catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
    }
  }
}
4

0 回答 0