Error when instantiating class

Asked

Viewed 304 times

1

I’m trying to read class data dados in the Main and he gives me mistake in mine array: cadastro[i] = new dados();

Data class:

class dados {
    private int numero;
    private String nome;
    private String sexo;

public dados(int nr,String n,String s, int idd,String m,String prv,String rl) //**********
    { 
    numero = nr;
    nome = n;
    sexo = s;
    idade = idd;
    moradia = m;
    provincia = prv;
    relegiao = rl;
    }

    public int getNumero(){
        return this.numero;
    }
    public void setNumero(int nr){
        this.numero = nr;
    }

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

    public String getSexo(){
        return sexo;
    }
    public void setSexo(String s){ 
        this.sexo = s;
    }

main class:

public class Main {
    public static void main(String[]args){
          dados cadastro[] = new dados[100];
          for(int i = 0; i< cadastro.length; i++){

             cadastro[i] = new dados(); //esta dando erro nessa linha

              cadastro[i].getNumero(); // Recuperando o numero
                                cadastro[i].setNumero(Integer.parseInt(//habilitando a insercao
                                        JOptionPane.showInputDialog("Atribua um Numero ao Cidadao")));//Atribuindo um valor

                                cadastro[i].getNome();// Recuperando o nome
                                cadastro[i].setNome( //habilitando a insercao
                                        JOptionPane.showInputDialog
                                        ("Digite o Nome Completo do Cidadao"));//Atribuindo um valor ao nome
  • I’m sorry I failed is yes java

  • 1

    More about language conventions: https://answall.com/a/153555/132

1 answer

8

The problem is that you have created a constructor that takes arguments to start class properties, but is trying to start an object of this class without passing any. Either you create a constructor with no alternative arguments or correctly start the class by passing all the values you defined in the constructor.

It’s another thing to always follow the java convention correctly, where class names should always start with uppercase letter.

Without tampering with the code, the simplest solution would be to create the constructor without parameters, as below:

class Dados {
    private int numero;
    private String nome;
    private String sexo;

public Dados(){}

public Dados(int nr,String n,String s, int idd,String m,String prv,String rl) //**********
    { 
    numero = nr;
    nome = n;
    sexo = s;
    idade = idd;
    moradia = m;
    provincia = prv;
    relegiao = rl;
    }

    public int getNumero(){
        return this.numero;
    }
    public void setNumero(int nr){
        this.numero = nr;
    }

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

    public String getSexo(){
        return sexo;
    }
    public void setSexo(String s){ 
        this.sexo = s;
    }
  • Right! Roger that....

  • 1

    Thank you very much Articuno, I managed to solve the error

  • @If the answer helped you, you can accept it as right by clicking on v next door. :)

  • Honestly, when I saw the tiny class, I stopped reading! I don’t get too attached to the theories of OO that some are killing themselves out there in discussion on the forums, but it is likely that the guy didn’t even read at least the concepts of OO, mainly in relation to Java language.

Browser other questions tagged

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