1

I am trying to print a list of entries from a JSONarray in a .json file, but somehow it seems that it is printing something else (I am guessing it is some kind of address based on the thing that is printed out). Could someone tell me what is wrong with my code? or that is what should be printed.

My .json file looke likes this: enter image description here

So, I used the following code:

private void printThingies() {
    List<Thingy> thingies = workRoom.getThingies();
    for (Thingy t : thingies) {
        System.out.println("Why is this getting printed: " + t);
        System.out.println(t.getName());
        System.out.println(t.getCate());
    }

and this is the output: enter image description here

The thing I don't understand is why model.Thingy@b4c966a is getting printed? I was expecting the output to look like this as I was able to use the same code to output something like this in another project, so I am not sure why the "address like" thing is getting printed, could someone suggest a fix or possible source of error: enter image description here

Clancinio
  • 734
  • 6
  • 21
  • 40
john_w
  • 693
  • 1
  • 6
  • 25
  • `Thingy t` is an object, without overriding `toString` method, you'll get something like `model.Thingy@234234`. See https://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java – josejuan Aug 22 '21 at 09:17
  • This has nothing to do with JSON. Duplicate of [How do I print my Java object without getting “SomeType@2f92e0f4”?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – Mark Rotteveel Aug 22 '21 at 10:56

4 Answers4

2

Well you need to override toString method inside Thingy class. When you try to print it to console java will call this toString method.

For example, modify your Thingy class like this

class Thingy {
    private String name;
    private String cate;

    @Override
    public String toString() {
        return name + " : " + cate;
    }

    // getters and setters    
}

Then you can print to console like following,

List<Thingy> thingies = workRoom.getThingies();
for (Thingy t : thingies) {
    System.out.println(t);
}

ray
  • 1,512
  • 4
  • 11
1

The Model.Thingy@4e50df2e is a String representation of the object and printed by toString() method from Object Class.

As per the documentation public String toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())
 

Returns: a string representation of the object.

To get around this you will have to override the toString() method and provide your own implementation.

For your specific case, the toString() implementation can be like.

    @Override
    public String toString() {
        return cate+": "+name;
    }

considering cate and name are the class fields of Thingy

Most of the IDE's support toString() generation. You can also use lombok, with annotations @ToString or @Data to generate toString() for you.

Here is a more generic example of toString() implementation.

    @Override
    public String toString() {
        return "Thingies {" +
                "name='" + name + '\'' +
                ", category='" + cate + 
                '}';
    }
0

Try to use lombok https://projectlombok.org/ Just put @Data to your class and you have available all getters / setters and toString method.

0

t in foreach has type of Thingy which is object and adress of object .

List<Thingy> thingies = List.of(new Thingy("metal2","METALWORK"),new Thingy("knit2", "KNITTING"));
    for (Thingy t : thingies) {
        System.out.println("Why is this getting printed: " + t);
        System.out.println(t.getName());
        System.out.println(t.getCate());
    }

t just print out adrress through toString method of object. Therefore, you need to override toString method of object.

public class Thingy {
    String name;
    String cate;

    public Thingy(String name, String cate) {
        this.name = name;
        this.cate = cate;
    }
    public String getName() {
        return name;
    }

    public String getCate() {
        return cate;
     }

    @Override
    public String toString() {
        return getCate()+":"+getName();
    }


}
bittap
  • 518
  • 1
  • 6
  • 15
  • so t actually has two fields: object and address of object ? is it correct? So any thing of type Object will have these two fields? and when the default print is invoked (i.e. without Override), then that is what get printed out? – john_w Aug 22 '21 at 18:48
  • t hasn't two fields. t is object. object has address for referencing it. i.e You have seen NullpointerException. it is occurred for object Because It hasn't address for referencing.the default print is invoked by toString of object class.Because All class inheritance object class, All classes have method of object class. i.e) toString.equals,hashcode. – bittap Aug 22 '21 at 23:16