Error Passing Object To Other

Asked

Viewed 35 times

-4

Eai personal. I am new in java and an error is happening where I want to pass an object from one class to another object from another class and keeps giving the following error.

incompatible types : Patient cannot be converted to String. I know what it means but I don’t know how to fix the mistake. My test class is as follows.

import java.util.ArrayList;
import model.Hospital;
import model.Medico;
import model.Paciente;
import model.Visitante;
import model.Doenca;


public class Teste {

public static void main(String[] args){


   Paciente novoP = new Paciente("Joao da Silva", "174526", "Jardin das   flores", "1745181");
   novoP.setAlergia("Alergia a agulha");

   Visitante novoV = new Visitante("Maria do Rosario", "14751", "Jardin das Flores", "1475145");
   novoV.setEntrada("12:30");
   novoV.setSaida("2:00");

   Visitante novoV2 = new Visitante("Marta do Rosario", "14758", "Jardin das Flores", "1475125");
   novoV2.setEntrada("12:30");
   novoV2.setSaida("2:00");

   ArrayList<Visitante>visitantes = new ArrayList<>();
   visitantes.add(novoV);
   visitantes.add(novoV2);

   novoP.setVisitas(visitantes);



   Doenca novaD = new Doenca();
   novaD.setNome("Tubercolose");
   novaD.setInternado(novoP); //aqui é onde ocorre o erro, "internado" é o atributo que eu passo o objeto "novoP".



hospital(novo);
hospital(mNovo);

}    
public static void hospital(Hospital hos){
  System.out.println(hos);

}   


}

Class Patient

package model;

import java.util.ArrayList;



public class Paciente extends Pessoa {
private String alergia;
private String idade;
private ArrayList<Visitante>visitas;

public Paciente(String nome, String cpf, String endereco, String  telefone) {
    super(nome, cpf, endereco, telefone);
}


public String getAlergia() {
    return alergia;
}

public void setAlergia(String alergia) {
    this.alergia = alergia;
}

public String getIdade() {
    return idade;
}

public void setIdade(String idade) {
    this.idade = idade;
}

public ArrayList<Visitante> getVisitas() {
    return visitas;
}

public void setVisitas(ArrayList<Visitante> visitas) {
    this.visitas = visitas;
}

@Override
public String toString() {
    return "Paciente: "+super.toString()+"{" + "alergia:" + alergia + ", idade:" + idade + ", visitas:" + visitas + '}';
}







}

Class Disease

package model;

 import java.util.ArrayList;


 public class Doenca {
 private String nome;
 private String internado;
 private String medicoNome;

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getInternado() {
    return internado;
}

public void setInternado(String internado) {
    this.internado = internado;
}

public String getMedicoNome() {
    return medicoNome;
}

public void setMedicoNome(String medicoNome) {
    this.medicoNome = medicoNome;
}



@Override
public String toString() {
    return "Doenca{" + "nome:" + getNome() + ", internado:" + getInternado() + ", medicoNome:" + getMedicoNome() + '}';
}






}
  • Adds all the classes involved in the question, it seems to be an error of inheritance ma without seeing the classes has no way to confirm.

  • Where does that mistake come from?

  • I would need to check the other classes, but probably the object Doenca is with the variable Hospitalized as String type, when it should be the type Patient.

  • this in the Test class. This commented

1 answer

0


The error is quite clear, as you yourself noted:

Here is the method that arrow the variable internado:

public void setInternado(String internado) {...}

He gets a String. Now, what you’re passing on to this method is a Paciente:

novaD.setInternado(novoP);

novoP, in this case, it is of the type Paciente, as it happens here: Paciente novoP = new Paciente(...);

To solve this error, it would be better to modify your class Doenca to deal directly with an object of the type Paciente, which I imagine is your intention (to have a patient associated with the disease), correct?

1) Instead of String internado, have a Paciente internado.

2) The method of setInternado() shall then receive a Paciente.

3) The method of getInternado() now returns a Paciente.

Browser other questions tagged

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