Hello there,

In my program I have an object of a class with some values, and I am trying to create another object, populate it to array and add it to my object.

 class AdvertisementType {

        public String getWebAdvert() {
            return webAdvert;
        }

        public String getEmailAdvert() {
            return emailAdvert;
        }

        private String webAdvert;
        private String emailAdvert;

        AdvertisementType(String webAdvert, String emailAdvert){
        this.webAdvert = webAdvert;    
        this.webAdvert = emailAdvert;

        }
    }


public class ProjectManager {

    DisplayClientRequest b;               

   public void viewRequest(){

   Date date = new Date();             
   SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMMM-yyyy");         
   String printDate = sdf.format(date);   

    AdvertisementType ad = new AdvertisementType("web", "newspaper");
    AdvertisementType[] first = new AdvertisementType[1];
    first[0] = ad;     



    DisplayClientRequest request1 = new DisplayClientRequest("Bartosz", "Mirowski", printDate, first );
    System.out.println(request1.getName() + " " + request1.getSurname() + " " + request1.getDate() + Arrays.toString(request1.getAdvertisementType()));

    }

}



    package busypointltd;

    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Date;


     /**
         *
         * @author Bartosz
         */
        public class DisplayClientRequest {

            private String name;
            private String surname;
            private String date;

            private AdvertisementType[] advertisementType;

            public DisplayClientRequest(String name, String surname, String date, AdvertisementType[] advertisementType) {

                this.name = name;
                this.surname = surname;
                this.date = date;
                this.advertisementType = advertisementType;

            }

            public String getName() {
                return name;
            }

            public String getSurname() {
                return surname;
            }

            public String getDate() {
                return date;
            }

            public AdvertisementType[] getAdvertisementType() {      

                return advertisementType;
            }

        }

In DisplayClient Request i have an array passed to constructor of type AdvertisementType, and in AdvertisementType I have some Strings passed to constructor as well, then in ProjectManager class I am creating an object of it and trying to put array in other object. When i try to print out, it doesnt work.. I tried using algorithms for printing and Arrays.toString.. any suggestions ??

Thank you in advance

"It doesn't work" tells us nothing.
Would you take your car into the workshop and just say "there's something wrong with it" and expect the mechanic to fix it?
Did it compile? Was there an error message? Did the output differ from what you expected, if so in what way exactly? Did the computer catch fire? Did you get output that looked like AdvertisementType@123456?

The error I am getting is : [null, busypointltd.AdvertisementType@75b84c92]

I was actually expecting this array to be set in DisplayClientRequest Class, but of type AdvertisementType, and I wanned to get this array in ProjectManager class

Thanks a lot

Arrays don't overload tostring() as far as I am aware in any language. They probably should but they don't. You have to loop through the array in order to print out the array. If they do overload, it is just info about hash, and name, etc, not the actual content.

Your use of Arrays.toString is correct (no need to loop) it converts each element of the array to strings and lists them all.
The problem is that the first element is null - displayed as the string "null" and the second is an instance of the busypointltd.AdvertisementType class, which is displayed as the string "busypointltd.AdvertisementType@75b84c92"
That's because every Java class inherits a toString() method from the Object class, which is used to convert the object you want to print into a String. The inherited method returns the class name, an @, and the object's hash (normally the same as its address).
If you want to display something more helpful, override the toString method in your class to return a useful string. Eg

class Person {
    private String givenName, familyName;
    ...
    @Override
    public String toString() {
        return "Person: " + givenName + " " + familyName;
    }

You should do this for every class you create so when you print an instance you get useful output.

I done exactly like you advised to me but i am getting following:

[null, kkknull]
Bartosz Aja 29-October-2015[null, kkknull]

it seems like it just took second parameter of object, becasue I am giving array's index as [1]. But I still don't understand why there is null..

Thank you for your time

Java arrays start wil element [0], so element [1] is the second element. A new array of objects will initially have all its values as null. If you haven't initialised element [0] then it will still be null.

Yeah I created composition between DisplayClientRequest and AdvertisementType classes, and now everything seems to be working as expected. I have had use toString method to override.

Thank you for your help

Kind Regards

Arrays don't overload tostring() as far as I am aware in any language.

Off topic: Try Ruby, Perl, or JavaScript. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.