-2

This is the code I wrote in Java and accessing Bugzilla and getting the object. But I am unable to get object Values. I am trying to override toString() function but overrided one is not working.

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());
         }
     } 

It's not returning OJEABX as expected. Instead bugs=[Ljava.lang.Object;@2ee5e48a being displayed. Where am I going wrong? I want to print value of Object but not working fine.

Cœur
  • 32,421
  • 21
  • 173
  • 232
O D
  • 21
  • 1
  • 7
  • 3
    is createResult is an instance of your Search class? – rokonoid Feb 11 '14 at 07:11
  • 2
    [Overloading != Overriding](http://stackoverflow.com/questions/12374399/difference-between-method-overloading-and-overriding-in-java) – PakkuDon Feb 11 '14 at 07:12
  • you are overriding toString() method of your search class. – kai Feb 11 '14 at 07:16
  • If you actually call `toString` on an instance of your `Search` class, it will output what you expect - I don't see anywhere that you're doing that in your code though. Where do you think it should be doing it? – Krease Feb 11 '14 at 07:16
  • @rokonoid createResult is an Object that is returned by an API method i am calling(Bug.seacrh). – O D Feb 11 '14 at 08:11
  • @Chris public String toString() { //System.err.println ("Ojesh"); return String.format("oje"+"abx"); so when i call create.toString() it should print ojeabx – O D Feb 11 '14 at 08:14
  • I can see the _definition_ of the method - what I don't see is where you're _using_ it... You call `toString` in a few places, but not on an instance of your `Search` class. – Krease Feb 11 '14 at 08:16
  • @Chris yes you are right. createResult is not an instance of my class. so how can i get contents of this object?? – O D Feb 11 '14 at 08:44

2 Answers2

1

You indeed created overridden version of toSrintg() in your Search class. But you do not use it.

Try System.out.println(new Search().toString())); and see how your toString() is working. I do not understand which code line does not work from your point of view.

There are 2 lines that print something:

System.err.println ("loginResult=" + loginResult);

and

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

The first line prints array because loginResult is and array. The second line prints object returned by your rpcClient that in fact is array too. So, you see the toString implementation of array that cannot be changed.

If you want to create string representation of array use Arrays.toString(arr).

AlexR
  • 109,181
  • 14
  • 116
  • 194
  • System.out.println(new Search().toString()) is behaving fine as it should. But what i want is to get te content of createResult object which have two parametres in it.So i was trying to use createResult.toString() butu not working. even java.util.Arrays.toString(createResult)) not working. ERROR:- The method toString(long[]) in the type Arrays is not applicable for the arguments (Object) – O D Feb 11 '14 at 08:40
  • if i want to access the content of object that is not instance of my class, how do i go about it?? – O D Feb 11 '14 at 09:00
  • @OD, you should use services of this class directly or indirectly. For example if you have class `Person` that implements `getName()` you can call this method to print name. If you are dealing with array (as in your case) you can either iterate over array and print each element or use utility liek `Arrays.toString(arr)` – AlexR Feb 11 '14 at 09:56
  • I already tried Arrays.toString(arr) but its not working. Its a object of class java.util.HashMap. – O D Feb 11 '14 at 10:06
1

First, you are using String.format which is used to format strings bases on a format as the name implies. See http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

What you need is to use StringBuilder to create the string that you want by appending them and give it to toString()

ex : http://docs.oracle.com/javase/tutorial/java/data/buffers.html

Anand
  • 116
  • 1
  • 8
  • enen if i directy use return "ABC", itys not working, i mean my toString() method is not being called in line System.err.println("createResult =" + createResult.toString()); – O D Feb 11 '14 at 08:22