Problems assigning values to variables in an object

4

I need to pass variable data to another class called Fila.java. I have a vector of type that receives objects, so in the main class I created an object that I initialized as follows:

public class Dados{
    int menu = 20,i=0; 
    String produto;
    int Quantidade=0;
    float ValorUn=0,Desconto=0,AliquotaICMS=0;
    String Obs;     
}

public static void main(String[] args) {        
        BancoDeDados db = null;
        Dados da = new Dados;
        da.menu = 20;
}

But steward of the way I boot it, it generates error at compile time

If I initialize like this:

Dados da;

It says that the variable was not initialized, if it assigns the value NULL , says that it is pointing to a null pointer, how to solve?

Classe Fila.java        
public class Fila {
    public Fila() {     

    }

    int inicio, fim, numelem,tamanho;
    Object array[];
    String elem;//string

    Fila(int tam){
        this.inicio = 0;
        this.fim = 0;
        this.numelem = 0;
        this.tamanho=tam;
        this.array = new Object[tam];
    }

    public boolean vazia(){
       if(numelem==0)
           return true;
           return false;    
    }

    public void inserir(Object elem){       
        array[fim]=elem;
        numelem++;
        fim++;
        if(fim==tamanho)
        fim=0;
        System.out.println(array[0]);
    }
    public Object remover(){
        Object temp=null;
        if(!vazia()){
            temp=array[inicio];
            array[inicio]=null;
            inicio++;
            numelem--;
            if(inicio==tamanho)
                inicio=0;
        }
        else
            System.out.println("Fila vazia");
        return temp;
    }


    public void AumentarVetor() {       
        tamanho = (tamanho/2)*3;                
    }                               
}

Main class:

import BancoDeDados.Dados;

public class Main {

    public static class Dados{

        int menu = 20,i=0; 
        String produto;
        int Quantidade=0;
        float ValorUn=0,Desconto=0,AliquotaICMS=0;
        String Obs;

        public Dados(){
              //também á uma boa idéia inicializar os valores das variáveis dentro do construtor
            }

    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub      
        //BancoDeDados db = null;
        //BancoDeDados.main(args);
        Dados da = new Dados();
        da.menu = 20;
        da.produto = "Teste";
        Fila f = new Fila();

        da.menu = 10;

        f.inserir(da);              
    }
}
    
asked by anonymous 24.10.2017 / 04:16

2 answers

1

You need to create a constructor method, constructors are responsible for creating the object in memory, that is, instantiating the class that has been defined. They are required and are declared as follows:

public class Dados{

    int menu = 20, i = 0; 
    String produto;
    int quantidade = 0;
    float valorUn = 0, desconto = 0, aliquotaICMS = 0;
    String obs;

    public Dados(){
        //também é uma boa idéia inicializar os valores das variáveis dentro do construtor
    }
}

A class can have multiple constructors and they can get no or multiple parameters, and in the class main you should instantiate the object like this:

Dados da = new Dados();
    
24.10.2017 / 05:22
0

Let's break the problem into parts.

  • The BancoDeDados class is not relevant to the problem. Nothing is done with it in the Main.
    So the problem is in the following 2 lines:
  • Error code:

    Dados da = new Dados;
    da.menu = 20;
    
  • The Data class is initialized to Dados da = new Dados(); There is a syntax error. I also assume that the Dados class has only 1 constructor, the empty constructor (no arguments).
  • Somewhere in the Data class code, I assume:

    public Dados()
    {
        /*Faz alguma coisa aqui? Talvez aqui inicialize a Fila?*/
    }
    
  • Confirm that the Queue is initialized with the correct constructor. Fila testeFila = new Fila(5); . Note that new Fila(5); calls the constructor of 1 argument:
  • The builder you created:

    Fila(int tam){
        this.inicio = 0;
        this.fim = 0;
        this.numelem = 0;
        this.tamanho = tam;
        this.array = new Object[tam];
    }
    

    If you call Fila testeFila = new Fila(); , the called constructor is:

    public Fila() {
    }
    

    And array is not initialized.

    Any of these steps was the problem?

    Fila is a Class. This class has a Object array[]; field.

    This object is not initialized by default. The line that the instance is in the constructor:

    Fila(int tam){
        this.inicio = 0;
        this.fim = 0;
        this.numelem = 0;
        this.tamanho = tam;
        **this.array = new Object[tam];**
    }
    

    That is, it is necessary for the code to reach this line so that the array object is initialized.

    Fila(int tam) is a constructor of class Fila . It receives as an integer parameter, and executes the 5 lines of code that are in the excerpt above.

    On your Main, declare the queue as

    Fila f = new Fila();
    

    This will not call the constructor you created above, but the empty constructor .

    Still in your class Fila :

    public Fila() {     
            /*Este é o código que executa ao invocar Fila f = new Fila();*/
    }
    
        
    24.10.2017 / 11:33