Why isn’t the object being instantiated?

Asked

Viewed 425 times

0

I have a Bean class (Casa.java)

public class Casa{
      //atribubutos

      private String parede;

      public Casa(){
      }
      //getters e setters
      public void setParede(String parede){
             this.parede = parede;
      }
      public String getParede(){
             return parede;
      }
}

I’m trying to instantiate her in another class like in the example

public class testeCasa(){

       public static void main(String[] args){

          String erro="";
           try{
              Casa casa = new Casa();

              casa.setParede("Parede Sul");
              System.out.println(casa.getParede());
              }
           }catch (Exception e{

                  erro = e.getMessage();
           }finally{
                  System.out.println(erro);
           }
}

Only what time I turn the debug, he arrives at the stretch Casa casa = new Casa();" and for the program does not continue to casa.getParede(); and goes straight to finally printing the error as "". What can be?

  • What error appears?

  • 2

    Maybe you need to make one import endereco.do.pacote.Casa. But it’s just a hunch, you have to post the mistake you’re getting.

  • made the import, it does not appear error, the program only stops and nothing else occurs @Math

  • 1

    If the code only does this even if you posted it, there’s no mistake, it’s not doing anything useful, that’s all. It performs well, only you didn’t have anything done that can be verified. We can’t help much with this information alone. If the code was complete, maybe help a little.

  • @mustache the code is right, the getters and setters are the same standards, it just ends the program without picking up the get or passing to the next line of the code after "House = new House()", tb to find strange this but is what is occurring

  • 1

    You’re saying the code is this, but it says that it has getters and setters, says that there are more lines later. This does not match. By your description nothing is wrong or the question cannot show what the problem is.

  • @bigown put the whole code and what happens, it won’t even set

Show 2 more comments

1 answer

4


Your code doesn’t even compile, so every description of the question and the comments are not true. When solving the problems that prevent the compilation the code works perfectly, although it is not doing anything very useful. But if the goal was to assign a value to the property and then print it taking value from it, this is occurring.

class Casa {
    private String parede;
    public void setParede(String parede) {
        this.parede = parede;
    }
    public String getParede() {
        return parede;
    }
}

class Ideone {
    public static void main(String[] args) {
        String erro="";
        try {
            Casa casa = new Casa();
            casa.setParede("Parede Sul");
            System.out.println(casa.getParede());
        } catch (Exception e) {
            erro = e.getMessage();
        } finally {
            System.out.println(erro);
        }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

The builder is absolutely unnecessary in this case. The try-catch also seems to be, but may be wanting to test or learn something from it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.