0

I want to insert an object (type Produto) inside of an ArrayList array_produtos of objects inside the Pedido_1 object as you can see inside itemClicked method. When i run the application, and i click an element of a gridview to run this method, the application just stops, showing me the error "unfortunately, the application stopped".

Produto.java

public class Produto {

    private final int cod_produto;
    private String nome;
    private double preco;
    private int quantidade;
    private int tempo;
    private int imagem;

    public Produto(int cod_produto, String nome, double preco,int quantidade, int tempo, int imagem) {
        super();
        this.cod_produto = cod_produto;
        this.nome = nome;
        this.preco = preco;
        this.quantidade = quantidade;
        this.tempo = tempo;
        this.imagem = imagem;
    }
}

Pedido.java

public class Pedido {

    private final int cod;
    private static int cod_aux=1;
    private int tempo_total;
    private double preco;
    private ArrayList<Produto> array_produtos;

    public Pedido() {
        this.cod = cod_aux;
        cod_aux++;
    }

    public void addProduto(Produto prodt){
        this.array_produtos.add(prodt);
        this.tempo_total += prodt.getTempo();
        this.preco += prodt.getPreco();
    }
}

MainActivity.java

public class MainActivity extends Activity {

    private ArrayList<Produto> produtos;
    private ArrayList<Pedido> produtos_pedidos;
    private int number_requests;
    private Produto produto_1;
    private Pedido pedido_1;
    private GridView gv;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        produtos = new ArrayList<Produto>();
        produtos_pedidos = new ArrayList<Pedido>();
        number_requests=0;

        //Products
        produto_1 = new Produto(1, "Café",0.60,4,4,R.drawable.cafe1);
        produtos.add(produto_1);

        //Requests
        pedido_1 = new Pedido(); //cria primeiro pedido
        produtos_pedidos.add(pedido_1); //Atribui à lista de pedidos

        gv = (GridView)findViewById(R.id.gv_produtos);
        lv = (ListView) findViewById(R.id.lv_produtos_pedidos);

        gv.setAdapter(new GridAdapter(this, array_produtos));

        ArrayAdapter<Pedido> arrayAdapter = new ArrayAdapter<Pedido>(this, android.R.layout.simple_list_item_1,produtos_pedidos);

        lv.setAdapter(arrayAdapter);
    }

    public void itemClicked(int position) {
        Toast.makeText(getBaseContext(), "Produto inserido!", Toast.LENGTH_LONG).show();
        produtos_pedidos.get(number_requests).addProduto(produtos.get(position));
    }
}
2
  • 1
    This is because you haven't initialized the array_produtos ArrayList Commented Dec 8, 2014 at 23:27
  • @Sal of course Sal, how could i forget it! Maybe i should rest. Btw, answer the question, to give you the pontuation. Commented Dec 8, 2014 at 23:32

1 Answer 1

1

I think what you need to do in your code is the following:

public class Pedido {

    private final int cod;
    private static int cod_aux=1;
    private int tempo_total;
    private double preco;
    private ArrayList<Produto> array_produtos;

    public Pedido() {
        this.cod = cod_aux;
        cod_aux++;
        array_produtos= new ArrayList<Produto>();
    }

    public void addProduto(Produto prodt){
        this.array_produtos.add(prodt);
        this.tempo_total += prodt.getTempo();
        this.preco += prodt.getPreco();
    }
}

This may help you, because you have not initialized your array in this class.

Sign up to request clarification or add additional context in comments.

1 Comment

the user Sal answered first, but he didn't do it like a real answer here. So, my upvote is going for you. Thank's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.