Unable to Overload toString for Object

Discussion in 'Java' started by Ojesh Dugar, Feb 11, 2014.

  1. Ojesh Dugar

    Ojesh Dugar Guest

    Code:

    package XMLRPC;

    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.xmlrpc.XmlRpcException;
    import org.apache.xmlrpc.client.XmlRpcClient;
    import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
    import org.apache.xmlrpc.client.XmlRpcCommonsTransportFactory;

    public class Search {


    @Override
    public String toString() {
    //System.err.println ("Ojesh");
    return String.format("oje"+"abx");
    }

    public static void main(String args[])
    throws MalformedURLException, XmlRpcException {


    HttpClient httpClient = new HttpClient();
    XmlRpcClient rpcClient = new XmlRpcClient();
    XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
    XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

    factory.setHttpClient(httpClient);
    rpcClient.setTransportFactory(factory);
    config.setServerURL(new URL("http://abc2/bugzilla/xmlrpc.cgi"));
    rpcClient.setConfig(config);

    //map of the login data
    Map loginMap = new HashMap();
    loginMap.put("login", "abc@bag");
    loginMap.put("password", "***");
    loginMap.put("rememberlogin", "Bugzilla_remember");




    // login to bugzilla
    Object loginResult = rpcClient.execute("User.login", new Object[]{loginMap});
    System.err.println ("loginResult=" + loginResult);

    // map of the bug data ok
    Map bugMap = new HashMap();

    bugMap.put("id", "350");

    //bugMap.put("status", "NEW");

    // create bug
    Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});
    //createResult.toString();

    System.err.println("createResult =" + createResult.toString());


    }


    }



    Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.
    Where am i going wrong.??
     
    Ojesh Dugar, Feb 11, 2014
    #1
    1. Advertising

  2. Ojesh Dugar

    Stefan Ram Guest

    Ojesh Dugar <> writes:
    >public class Search {
    >public String toString() {
    >Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});
    >System.err.println("createResult =" + createResult.toString());
    >Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.
    >Where am i going wrong.??


    You attempt to tackle a programming task without having
    learned the language fundamentals first. Otherwise, the
    error would be obvious to you.

    You are overwriting the toString() method for objects
    of the class »Search«, but the object referred to by
    »createResult« is not an instance of the class »Search«.

    In Java, one cannot modify the toString() method for
    the standard class »java.lang.Object« as in JavaScript:

    Object . prototype . toString = function(){ return "b" }
    var test = new Object();
    test.toString()
    "b"

    >return String.format("oje"+"abx");


    You cannot expect »OJEABX« when you return »ojeabx«.
     
    Stefan Ram, Feb 11, 2014
    #2
    1. Advertising

  3. Ojesh Dugar

    Ojesh Dugar Guest

    On Tuesday, February 11, 2014 2:10:22 PM UTC+5:30, Leif Roar Moldskred wrote:
    > Ojesh Dugar <> wrote:
    >
    > >

    >
    > > Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.

    >
    > > Where am i going wrong.??

    >
    > Thanks a lot Mr.Moldskred

    But now how can I access content of a object from some other class??
    >
    > The /toString()/ you implement in your /Search/ class will only have an effect for objects
    >
    > that are instances of this particular class. The result returned by the call to
    >
    > /rpcClient.execute()/ is not a /Search/ instance.
    >
    >
    >
    > Java isn't a prototype-based language. You can't modify or add methods to an existing object;
    >
    > you can only define a new class that overrides methods in the original class and then ensure
    >
    > that when you instantiate the relevant objects that you instantiate them from the new class
    >
    > and not the old. When you're not in control of the object instantiation (for instance,
    >
    > when the objects are created in a third party library) you will have to solve the problem
    >
    > in some other way.
    >
    >
    >
    > --
    >
    > Leif Roar Moldskred
     
    Ojesh Dugar, Feb 11, 2014
    #3
  4. Ojesh Dugar

    Ojesh Dugar Guest

    On Tuesday, February 11, 2014 2:28:22 PM UTC+5:30, Stefan Ram wrote:
    > Ojesh Dugar <> writes:
    >
    > >public class Search {

    >
    > >public String toString() {

    >
    > >Object createResult = rpcClient.execute("Bug.search", new Object[]{bugMap});

    >
    > >System.err.println("createResult =" + createResult.toString());

    >
    > >Its not returnig OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being diplayed.

    >
    > >Where am i going wrong.??

    >
    >
    >
    > You attempt to tackle a programming task without having
    >
    > learned the language fundamentals first. Otherwise, the
    >
    > error would be obvious to you.
    >
    >
    >
    > You are overwriting the toString() method for objects
    >
    > of the class »Search«, but the object referred to by
    >
    > »createResult« is not an instance of the class »Search«.
    >
    >
    >
    > In Java, one cannot modify the toString() method for
    >
    > the standard class »java.lang.Object« as in JavaScript:
    >
    >
    >
    > Object . prototype . toString = function(){ return "b" }
    >
    > var test = new Object();
    >
    > test.toString()
    >
    > "b"
    >
    >
    >
    > >return String.format("oje"+"abx");

    >
    >
    >
    > You cannot expect »OJEABX« when you return »ojeabx«.


    Yes now i see the problem. Thanks a lot.
    But my problem still remains how do i ACCESS the content of »createResult« which is not instance of my class??
    Thanks.
     
    Ojesh Dugar, Feb 11, 2014
    #4
  5. Ojesh Dugar

    Stefan Ram Guest

    Ojesh Dugar <> writes:
    >But my problem still remains how do i ACCESS the content of »createResult« which is not instance of my class??


    In Java and OOP, one prefers to hide the content. This is
    called »information hiding« or »encapsulation«. Objects
    instead have »behavior«.

    What comes most close to »access the content of an object«,
    would be to read the documentation of the class of the object.

    When »o« is an object, you can get its class with »o.getClass()«.
    When the class is being defined in a library or program, someone
    should have written a documentation for it, and you should read
    that documentation to learn how to get information from the object
    (using your point of view, although we prefer not to think of
    objects as information containers but rather of entities with behavior).
     
    Stefan Ram, Feb 11, 2014
    #5
  6. Ojesh Dugar

    Ojesh Dugar Guest

    On Tuesday, February 11, 2014 2:47:27 PM UTC+5:30, Stefan Ram wrote:
    > Ojesh Dugar <> writes:
    >
    > >But my problem still remains how do i ACCESS the content of »createResult« which is not instance of my class??

    >
    >
    >
    > In Java and OOP, one prefers to hide the content. This is
    >
    > called »information hiding« or »encapsulation«. Objects
    > Thanks a lot. I'll work on it.
    > instead have »behavior«.
    >
    >
    >
    > What comes most close to »access the content of an object«,
    >
    > would be to read the documentation of the class of the object.
    >
    >
    >
    > When »o« is an object, you can get its class with »o.getClass()«.
    >
    > When the class is being defined in a library or program, someone
    >
    > should have written a documentation for it, and you should read
    >
    > that documentation to learn how to get information from the object
    >
    > (using your point of view, although we prefer not to think of
    >
    > objects as information containers but rather of entities with behavior)..
     
    Ojesh Dugar, Feb 11, 2014
    #6
  7. Ojesh Dugar

    Lew Guest

    Ojesh Dugar wrote:
    > But now how can I access content of a object from some other class??


    (You only need one question mark at the end to indicate an interrogative sentence.)

    You access content from an instance of a class by using what methods it provides to reveal what it considers "content".

    What is the type whose content you are trying to reveal, and what is its API?

    --
    Lew
     
    Lew, Feb 11, 2014
    #7
  8. Ojesh Dugar

    Ojesh Dugar Guest

    On Wednesday, February 12, 2014 5:19:34 AM UTC+5:30, Lew wrote:
    > Ojesh Dugar wrote:
    >
    > > But now how can I access content of a object from some other class??

    >
    >
    >
    > (You only need one question mark at the end to indicate an interrogative sentence.)
    >
    >
    >
    > You access content from an instance of a class by using what methods it provides to reveal what it considers "content".
    >
    >
    >
    > What is the type whose content you are trying to reveal, and what is its API?
    >
    >
    >
    > --
    >
    > Lew


    Basically, I am able to access Bugzilla Webservice API through java code(xmlrpc) and in return i get a object of <<class java.util.HashMap>>.
    According to Bugzilla documentation two items are returned, bugs and faults, both are array of hashes and I want to access these.
    Thanks.
     
    Ojesh Dugar, Feb 12, 2014
    #8
  9. Ojesh Dugar

    markspace Guest

    On 2/12/2014 12:38 AM, Ojesh Dugar wrote:
    > Basically, I am able to access Bugzilla Webservice API through java
    > code(xmlrpc) and in return i get a object of <<class
    > java.util.HashMap>>. According to Bugzilla documentation two items
    > are returned, bugs and faults, both are array of hashes and I want to
    > access these. Thanks.
    >



    Cast to a HashMap and iterate over all of its keys.

    (not tested):

    // create bug
    Object createResult = rpcClient.execute("Bug.search", new
    Object[]{bugMap});
    //createResult.toString();

    HashMap bugs = (HashMap)createResult;
    for( Object key : bugs.keySet() ) {
    System.out.println( "Key "+key.getClass().getName()
    + "= "+key
    + ", value "+ bugs.get( key ).getClass().getName()
    + "= "+ bugs.get( key )
    );
    }

    More info here:

    http://www.mkyong.com/java/how-to-loop-a-map-in-java/
     
    markspace, Feb 12, 2014
    #9
  10. Ojesh Dugar

    Eric Sosman Guest

    On 2/12/2014 1:37 PM, markspace wrote:
    > On 2/12/2014 12:38 AM, Ojesh Dugar wrote:
    >> Basically, I am able to access Bugzilla Webservice API through java
    >> code(xmlrpc) and in return i get a object of <<class
    >> java.util.HashMap>>. According to Bugzilla documentation two items
    >> are returned, bugs and faults, both are array of hashes and I want to
    >> access these. Thanks.
    >>

    >
    >
    > Cast to a HashMap and iterate over all of its keys.
    >
    > (not tested):
    >
    > // create bug
    > Object createResult = rpcClient.execute("Bug.search", new
    > Object[]{bugMap});
    > //createResult.toString();
    >
    > HashMap bugs = (HashMap)createResult;
    > for( Object key : bugs.keySet() ) {
    > System.out.println( "Key "+key.getClass().getName()
    > + "= "+key
    > + ", value "+ bugs.get( key ).getClass().getName()
    > + "= "+ bugs.get( key )
    > );
    > }
    >
    > More info here:
    >
    > http://www.mkyong.com/java/how-to-loop-a-map-in-java/


    This probably won't work as written. The original post said
    createResult.toString() produced "[Ljava.lang.Object;@2ee5e48a",
    indicating that rpcClient.execute() returns an Object[], that
    is, an array of Object references. (To those surprised that an
    array of Objects is itself an Object: Someday it will all make
    sense, I promise.) So what he needs to do is cast the returned
    value to an Object[]:

    Object[] array = (Object[]) createResult;

    .... then iterate over the contents of the array:

    for (Object obj : array) {

    .... and then (assuming the Objects in the array are HashMaps as
    advertised, or in any case *some* kind of Maps):

    Map bugs = (Map) obj;

    .... and *then* he can iterate over the entries, either in the
    manner markspace shows or by using the key/value pairs directly:

    for (Map.Entry ent : bugs.entrySet()) {
    Object key = ent.getKey();
    Object val = ent.getValue();
    ...

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 12, 2014
    #10
  11. Ojesh Dugar

    Ojesh Dugar Guest

    On Thursday, February 13, 2014 12:27:59 AM UTC+5:30, Eric Sosman wrote:
    > On 2/12/2014 1:37 PM, markspace wrote:
    >
    > > On 2/12/2014 12:38 AM, Ojesh Dugar wrote:

    >
    > >> Basically, I am able to access Bugzilla Webservice API through java

    >
    > >> code(xmlrpc) and in return i get a object of <<class

    >
    > >> java.util.HashMap>>. According to Bugzilla documentation two items

    >
    > >> are returned, bugs and faults, both are array of hashes and I want to

    >
    > >> access these. Thanks.

    >
    > >>

    >
    > >

    >
    > >

    >
    > > Cast to a HashMap and iterate over all of its keys.

    >
    > >

    >
    > > (not tested):

    >
    > >

    >
    > > // create bug

    >
    > > Object createResult = rpcClient.execute("Bug.search", new

    >
    > > Object[]{bugMap});

    >
    > > //createResult.toString();

    >
    > >

    >
    > > HashMap bugs = (HashMap)createResult;

    >
    > > for( Object key : bugs.keySet() ) {

    >
    > > System.out.println( "Key "+key.getClass().getName()

    >
    > > + "= "+key

    >
    > > + ", value "+ bugs.get( key ).getClass().getName()

    >
    > > + "= "+ bugs.get( key )

    >
    > > );

    >
    > > }

    >
    > >

    >
    > > More info here:

    >
    > >

    >
    > > http://www.mkyong.com/java/how-to-loop-a-map-in-java/

    >
    >
    >
    > This probably won't work as written. The original post said
    >
    > createResult.toString() produced "[Ljava.lang.Object;@2ee5e48a",
    >
    > indicating that rpcClient.execute() returns an Object[], that
    >
    > is, an array of Object references. (To those surprised that an
    >
    > array of Objects is itself an Object: Someday it will all make
    >
    > sense, I promise.) So what he needs to do is cast the returned
    >
    > value to an Object[]:
    >
    >
    >
    > Object[] array = (Object[]) createResult;
    >
    >
    >
    > ... then iterate over the contents of the array:
    >
    >
    >
    > for (Object obj : array) {
    >
    >
    >
    > ... and then (assuming the Objects in the array are HashMaps as
    >
    > advertised, or in any case *some* kind of Maps):
    >
    >
    >
    > Map bugs = (Map) obj;
    >
    >
    >
    > ... and *then* he can iterate over the entries, either in the
    >
    > manner markspace shows or by using the key/value pairs directly:
    >
    >
    >
    > for (Map.Entry ent : bugs.entrySet()) {
    >
    > Object key = ent.getKey();
    >
    > Object val = ent.getValue();
    >
    > ...
    >
    >
    >
    > --
    >
    > Eric Sosman
    >
    > d


    Object[] array = (Object[]) createResult;
    this line itself gives an error.
    Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to [Ljava.lang.Object;
    at XMLRPC.Testing.main(Testing.java:64)
     
    Ojesh Dugar, Feb 13, 2014
    #11
  12. Ojesh Dugar

    Ojesh Dugar Guest

    On Thursday, February 13, 2014 12:07:00 AM UTC+5:30, markspace wrote:
    > On 2/12/2014 12:38 AM, Ojesh Dugar wrote:
    >
    > > Basically, I am able to access Bugzilla Webservice API through java

    >
    > > code(xmlrpc) and in return i get a object of <<class

    >
    > > java.util.HashMap>>. According to Bugzilla documentation two items

    >
    > > are returned, bugs and faults, both are array of hashes and I want to

    >
    > > access these. Thanks.

    >
    > >

    >
    >
    >
    >
    >
    > Cast to a HashMap and iterate over all of its keys.
    >
    >
    >
    > (not tested):
    >
    >
    >
    > // create bug
    >
    > Object createResult = rpcClient.execute("Bug.search", new
    >
    > Object[]{bugMap});
    >
    > //createResult.toString();
    >
    >
    >
    > HashMap bugs = (HashMap)createResult;
    >
    > for( Object key : bugs.keySet() ) {
    >
    > System.out.println( "Key "+key.getClass().getName()
    >
    > + "= "+key
    >
    > + ", value "+ bugs.get( key ).getClass().getName()
    >
    > + "= "+ bugs.get( key )
    >
    > );
    >
    > }
    >
    >
    >
    > More info here:
    >
    >
    >
    > http://www.mkyong.com/java/how-to-loop-a-map-in-java/


    The Code you have given gives output as:

    << Key java.lang.String= bugs, value [Ljava.lang.Object;= [Ljava.lang.Object;@78812862 >>

    Which means that object is a hashmap Having Key as "bugs" and value itself is another object.(As per my understanding)

    According to documentation,
    bugs- is an array of hashes that contains information about the bugs with the valid ids. Each hash contains the following items
    1.component
    string, The name of the current component of this bug.

    2.creation_time
    dateTime, When the bug was created.

    3.creator
    string, The login name of the person who filed this bug (the reporter).

    4.id
    int, The unique numeric id of this bug.

    Now i cant undersatnd how do i proceed to get these informations.

    Thanks.
     
    Ojesh Dugar, Feb 13, 2014
    #12
  13. Ojesh Dugar

    markspace Guest

    On 2/12/2014 8:58 PM, Ojesh Dugar wrote:
    > According to documentation, bugs- is an array of hashes that contains
    > information about the bugs with the valid ids. Each hash contains the
    > following items


    It sounds to me like the first property, with a key "bugs" is an array,
    probably of HashMap (or possibly just Map), which contains a the actual
    bug information.

    The chance that I'm going to mess something up here is increasing
    because I'm not compiling this, but try this.

    // create bug
    Object createResult = rpcClient.execute("Bug.search", new
    Object[]{bugMap});
    //createResult.toString();

    HashMap bugs = (HashMap)createResult;
    for( Object key : bugs.keySet() ) {
    System.out.println( "Key "+key.getClass().getName()
    + "= "+key
    + ", value "+ bugs.get( key ).getClass().getName()
    + "= "+ bugs.get( key )
    );
    Object value = bugs.get( key );
    if( value instanceof Object[] ) {
    Object[] valueArray = (Object[])value;
    for( Object v : valueArray ) {
    System.out.println( "-- "+v.getClass().getName() );
    }
    } else System.out.println( "not an array!" );
    }


    You should be able to run this and determine what to replace the
    v.getClass().getName() part with; probably a cast to a Map. I don't
    recall if collections normally override their to string -- they might,
    though the result normally isn't very pretty.
     
    markspace, Feb 13, 2014
    #13
  14. Ojesh Dugar

    Ojesh Dugar Guest

    On Thursday, February 13, 2014 10:59:50 AM UTC+5:30, markspace wrote:
    > On 2/12/2014 8:58 PM, Ojesh Dugar wrote:
    >
    > > According to documentation, bugs- is an array of hashes that contains

    >
    > > information about the bugs with the valid ids. Each hash contains the

    >
    > > following items

    >
    >
    >
    > It sounds to me like the first property, with a key "bugs" is an array,
    >
    > probably of HashMap (or possibly just Map), which contains a the actual
    >
    > bug information.
    >
    >
    >
    > The chance that I'm going to mess something up here is increasing
    >
    > because I'm not compiling this, but try this.
    >
    >
    >
    > // create bug
    >
    > Object createResult = rpcClient.execute("Bug.search", new
    >
    > Object[]{bugMap});
    >
    > //createResult.toString();
    >
    >
    >
    > HashMap bugs = (HashMap)createResult;
    >
    > for( Object key : bugs.keySet() ) {
    >
    > System.out.println( "Key "+key.getClass().getName()
    >
    > + "= "+key
    >
    > + ", value "+ bugs.get( key ).getClass().getName()
    >
    > + "= "+ bugs.get( key )
    >
    > );
    >
    > Object value = bugs.get( key );
    >
    > if( value instanceof Object[] ) {
    >
    > Object[] valueArray = (Object[])value;
    >
    > for( Object v : valueArray ) {
    >
    > System.out.println( "-- "+v.getClass().getName() );
    >
    > }
    >
    > } else System.out.println( "not an array!" );
    >
    > }
    >
    >
    >
    >
    >
    > You should be able to run this and determine what to replace the
    >
    > v.getClass().getName() part with; probably a cast to a Map. I don't
    >
    > recall if collections normally override their to string -- they might,
    >
    > though the result normally isn't very pretty.


    Yeah, This worked out for me.
    That was a great help. Thanks a lot.
     
    Ojesh Dugar, Feb 13, 2014
    #14
  15. Ojesh Dugar

    Eric Sosman Guest

    On 2/12/2014 11:24 PM, Ojesh Dugar wrote:
    > [... lots of ugly double-spaced googlegroup grunge ...]
    >
    > Object[] array = (Object[]) createResult;
    > this line itself gives an error.
    > Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to [Ljava.lang.Object;
    > at XMLRPC.Testing.main(Testing.java:64)


    Your original post said the code's output was

    bugs=[Ljava.lang.Object;@2ee5e48a

    .... which is what you'd get if it output "bugs=" and the string
    representation of an Object[] array. On closer inspection, though,
    I see that there's nothing in the code that could have produced
    the "bugs=" part. Conclusion: You misreported the output or you
    reported the output of some slightly different piece of code, and
    I was wrong to believe that "[Ljava.lang.Object;@2ee5e48a" was the
    string representation of `createResult'. Sorry about that -- but
    in the future, please make sure your code and your reported output
    belong together.

    If `createResult' refers to a HashMap (as the exception text
    indicates), you can proceed as markspace showed. If it's an array
    (in a follow-up you wrote that "two items are returned [...] both
    are array of hashes") you'll need to access its individual elements.

    --
    Eric Sosman
    d
     
    Eric Sosman, Feb 13, 2014
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    816
    Tony Morris
    Jan 25, 2005
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  3. Replies:
    0
    Views:
    5,623
  4. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    260
    Sherm Pendley
    Oct 11, 2004
  5. Fergus McMenemie

    LibXML element->toString vs document->toString

    Fergus McMenemie, Jul 12, 2012, in forum: Perl Misc
    Replies:
    5
    Views:
    633
    Fergus McMenemie
    Jul 17, 2012
Loading...

Share This Page