Stack Exchange
How do i return or convert String to object in java
I am a beginner to Java and I am having some issue with this code. I made this adres Object and I need it to return the adres string. It gives me an error message saying that it cannot convert String to Adres. Can anyone help me solve this problem? Thanks alot
private Adres maakAdres() {



   String straat = adresScherm.getTfStraat();
   String plaats = adresScherm.getTfPlaats();
   String huisnr = adresScherm.getTfHuisNr();
   int IntHuisNr;
   try {
       IntHuisNr = Integer.parseInt(huisnr);
       String adres = plaats + ", " + straat + ", " + IntHuisNr;
       return adres;
   }
   catch(NumberFormatException ex) {
        adresScherm.getMelding().toonWaarschuwing("Het huisnummer moet een geheel getal zijn!");
   }

   if (Util.nullOrEmpty(straat) || Util.nullOrEmpty(plaats) || Util.nullOrEmpty(huisnr)) {
         adresScherm.getMelding().toonWaarschuwing("Niet alle gegevens zijn ingevuld");
         return null;
   }

    return null;
}


Can you share your Adres model object?

In your code, you are returning an Adres object. You need to create an instance of that and return that. Try return new Adres(string) assuming Adres has a string constructor.

If all you are returning is a String why not private String maakAdres() ?

Looking at your method suggests that the methods is expected to return an Adres object, but you are trying to return a String object, which will generate a compile error. So you should create an Adres object , assign the values and return.
