• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

215
Views
UPDATE: re Java ArrayList and method return

Below is my code and the print method is in the first class (Hyrestagare) as "public String address()".

I keep reading similar questions and answers in Stackoverflow. But I just get more confused. I make changes to the code, rewrite parts, but just can't solve the problem. :(

almost 3 years ago · Santiago Trujillo
2 answers
Answer question

0

In Hyrestagare class, address() method calls getAddress() on Hus object, however, there is no setAddress() call in Demo class and hence, address remains null, try the following:

    Hyrestagare a1 = new Hyrestagare();
    a1.setNamn("Donald");
    a1.setPersonnummer(111);

    Hyrestagare a2 = new Hyrestagare();
    a2.setNamn("Ivanka");
    a2.setPersonnummer(222);

    Hyrestagare b1 = new Hyrestagare();
    b1.setNamn("Barack");
    b1.setPersonnummer(333);

    Hyrestagare b2 = new Hyrestagare();
    b2.setNamn("Michelle");
    b2.setPersonnummer(444);

    Lagenhet l1 = new Lagenhet();
    l1.setHyra(5000);
    l1.setNummer(1);
    l1.setYta(200);

    Lagenhet l2 = new Lagenhet();
    l2.setHyra(2000);
    l2.setNummer(2);
    l2.setYta(50);

    Hus hus1 = new Hus();
    hus1.setAddress("Test Address");//sets the address

    // HYRESTAGARE KOPPLA TILL LAGENHET
    a1.setLagenhet(l1);
    a2.setLagenhet(l1);
    b1.setLagenhet(l2);
    b2.setLagenhet(l2);

    // LAGENHET KOPPLA HYRESTAGARE TILL LAGENHET, KOPPLA LISTA TILL LAGENHET
    ArrayList<Hyrestagare> renters = new ArrayList<Hyrestagare>();
    l1.laggTillHyrestagare(a1);
    l1.laggTillHyrestagare(a2);
    l2.laggTillHyrestagare(b1);
    l2.laggTillHyrestagare(b2);

    l1.setRenter(renters);
    l2.setRenter(renters);

    renters.add(a1);
    renters.add(a2);
    renters.add(b1);
    renters.add(b2);

    // HUS KOPPLA LAGENHET TILL LISTA
    ArrayList<Lagenhet> flats = new ArrayList<Lagenhet>();
    hus1.laggTillLagenhet(l1);
    hus1.laggTillLagenhet(l2);

    hus1.setApartments(flats);
    flats.add(l1);
    flats.add(l2);

    // KOPPLA HUS TILL HUS LISTA
    ArrayList<Hus> buildings = new ArrayList<Hus>();
    buildings.add(hus1);

    // PRINT METODER
    for (Hyrestagare temp : hus1.printHyrestagare(1)){
        System.out.println("Hyrestagare: " + temp.getNamn() + " (" + temp.getPersonnummer() + ").");
    }

    for (Hyrestagare temp : hus1.printHyrestagare(2)){
        System.out.println("Hyrestagare: " + temp.getNamn() + " (" + temp.getPersonnummer() + ").");
    }

    System.out.println("Address: " + a1.address());
almost 3 years ago · Santiago Trujillo Report

0

First of all, I would not recommend returning null ever, it just makes dealing with NullPointerExceptions even harder.

regarding your problem:

public String address(){
    for (Hus h : houses){
        return h.getAddress();
    }
    return null;
}

The problem here is in your example above, you create an instance of Hyrestagare called a1 but you never add any houses to this class.

Thats why when you later call a1.address(), your array of houses is empty, so it just returns null;.

As you use an ArrayListof Hus you will have multiple addresses. The problem with your approach, you will only get the element of the first address, because you return (escape out of the functions) the value.

Depending on what your goal with this function is, i could offer you some solutions:

1) This would return all addresses as a new list. If there where no addresses, than you will get an empty list and don't have to deal with null.

public List<String> getAddresses(List<Hus> list){
    return list.stream()
            .map(Hus::getAddress)
            .collect(Collectors.toList());
}

If this solution is to complex for you, this gives you the same result with simpler code:

public List<String> getAddresses(List<Hus> list){
    List<String> addresses = new ArrayList<>();
    for(Hus h: list){
        addresses.add(h.getAddress());
    }
    return addresses;
}

But if you just want the first address, like in your code (If intended or not):

public static String getAddress(List<Hus> list){
    if(list.isEmpty()){
        return "no address";
    } else {
        return list.get(0).getAddress();
    }
}
almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error