7 Replies Latest reply: Mar 8, 2010 4:19 PM by EJP RSS

    XMLEncoding an ArrayList - InstantiationException

    843790
      Hi all. I am trying to XMLEncode an ArrayList for another class to decode and use, but I keep getting:
      java.lang.InstantiationException: Locations.Location
      Continuing ...
      java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Location);
      Continuing ...

      The ArrayList stores objects of type Location, and the way these errors look indicates to me that there is a problem with the objects I am trying to store in the ArrayList and then XMLEncode.

      Can anyone drop me a couple of hints here? Or advise a better way of making the contents of an ArrayList in one class accessible to another?

      Many thanks.
        • 1. Re: XMLEncoding an ArrayList - InstantiationException
          843790
          This is being done in NetBeans IDE 6.8, by the way
          • 2. Re: XMLEncoding an ArrayList - InstantiationException
            843790
            DHD wrote:
            This is being done in NetBeans IDE 6.8, by the way
            The IDE you use is pretty irrelevant in most cases. Much more important would be the Java version you're using.

            Is that everything you get? Don't you get stack traces or more detailed error messages?

            Do you catch those exceptions yourself? If so, how do you print them?
            • 3. Re: XMLEncoding an ArrayList - InstantiationException
              843790
              The red lines:

              java.lang.InstantiationException: Locations.Location
              Continuing ...
              java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Location);
              Continuing ...

              are all I get, then the program keeps running.

              My methods for encoding and decoding are wrapped in basic try/catch blocks.

              try{...}
              catch(Exception e){e.printStackTrace()}
              • 4. Re: XMLEncoding an ArrayList - InstantiationException
                843790
                DHD wrote:
                The red lines:

                java.lang.InstantiationException: Locations.Location
                Continuing ...
                java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(Location);
                Continuing ...

                are all I get, then the program keeps running.

                My methods for encoding and decoding are wrapped in basic try/catch blocks.

                try{...}
                catch(Exception e){e.printStackTrace()}
                This sounds as if something else prints those messages and not your catch-block. Unfortunately I'm not an expert with XMLEncoding. I can try to guess, 'though: does your Location class have a public no-argument constructor?
                • 5. Re: XMLEncoding an ArrayList - InstantiationException
                  843790
                  Thanks for trying to help!

                  My Location class is minimal at the moment:
                  public class Location {

                  private String userID;
                  private String city;
                  private String region;

                  public Location(String id, String cty, String reg){
                  userID = id;
                  city = cty;
                  region = reg;
                  }

                  }
                  • 6. Re: XMLEncoding an ArrayList - InstantiationException
                    843790
                    Please use the code tags when posting code, it makes it much easier to read your code. Just copy-and-paste your code, select it and push "CODE" just above the text area.

                    I think (and I'm partially guessing, as I said I don't know XMLEncoder very well) that you need either getter/setters to make your class a valid bean or call the arguments in your constructor the same as the local variables (which is a good idea anyway, you'll need to use "this.foo = foo" then of course).
                    • 7. Re: XMLEncoding an ArrayList - InstantiationException
                      EJP
                      Your Location class isn't a Bean. Everything encoded/decoded by XMLEncoder/Decoder has either be a Bean, i.e. havea no-args constructor and public getters and setters for all attributes, or else have a PersistenceDelegate.