Error calling a class method inside main

Asked

Viewed 653 times

0

I’m trying to call a method in main, but the class won’t allow me to do it. Error appears:

"Cannot Make aStiatic References to the non-static method"

I own the class Gerenciamento, where the method is contained:

public class Gerenciamento {

public double totalCaixa;

private ArrayList<Bicicleta> bicicletas = new ArrayList<Bicicleta>();


Scanner leitor = new Scanner(System.in);

public void cadastrarBicicleta()
{
    String cor = null, marca = null, modelo = null, acessorio = null;
    int numIdBici = (bicicletas.size());
    boolean ok = false;

    System.out.print("\n----- Cadastro de Bicicleta -----\n\nInsira cor: ");
    do {
        try {
            cor = leitor.nextLine();
        } catch (Exception x) {
            ok = true;
        }
    } while (ok == true);

    System.out.print("\nInsira marca: ");
    do {
        try {
            marca = leitor.nextLine();
        } catch (Exception x) {
            ok = true;
        }
    } while (ok == true);

    System.out.print("\nInsira modelo: ");
    do {
        try {
            modelo = leitor.nextLine();
        } catch (Exception x) {
            ok = true;
        }
    } while (ok == true);

    System.out.print("\nInsira acessorios: ");
    do {
        try {
            acessorio = leitor.nextLine();
        } catch (Exception x) {
            ok = true;
        }
    } while (ok == true);

    boolean disponivel = true;
    bicicletas.add(new Bicicleta (numIdBici,cor, marca, modelo, acessorio, disponivel));
    numIdBici = (bicicletas.size());
    System.out.println("\n##### Bicicleta criada com sucesso! #####");
}

And I own the main, where I can’t call the method

public class TesteCicloTurismo extends Gerenciamento
{

    public static void main(String[] args)
    {
        Scanner leitor = new Scanner(System.in);
        int opcao;
        do
        {

            switch(opcao)
            {
            case 1:
                cadastrarBicicleta();//metodo nao pode ser chamado aqui
                break;
}
}

1 answer

0


You are trying to call an instance method within the main, which is static. The method will only exist when there is an instance of Gerenciamento or class TesteCicloTurismo.

Adapting to your code:

public class TesteCicloTurismo extends Gerenciamento
{
    public static void main(String[] args)
    {
        TesteCicloTurismo teste = new TesteCicloTurismo();
        Scanner leitor = new Scanner(System.in);
        int opcao;
        do
        {

            switch(opcao)
            {
            case 1:
                teste.cadastrarBicicleta();//metodo nao pode ser chamado aqui
                break;
            }
      }
}
  • Thank you so much! I was even ashamed now, to ask something so easy hah

  • @Dackr. Hey, don’t be embarrassed. We all go through this phase of learning :)

  • 1

    @gabrielfalieri ?? If it was pro OP, you can quote it so that the message is directed to it by putting "@"+the name used by it :)

Browser other questions tagged

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