user3393926
user3393926

Reputation: 1209

Can't determine the cause of NullPointerException

I have this following class which is a shopping cart class and is coded to display the contents of the shopping cart. The products are displayed in a linear layout with product infos in textViews and a corresponding button to remove the product and are arranged horizontally in linearlayout. View elements are created dynamically. My apps runs well until this class is called. Then the app crashes and LogCat gives a Java.lang.NullPointerException error at "lb.addView(ll)" line.I don't know why this is happening. Someone please help me out to solve This.

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Secondscreen extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondscreen); 

        //TextView showCartContent          = (TextView) findViewById(R.id.showCart);
        final    Button       thirdBtn    = (Button) findViewById(R.id.third);
        final    LinearLayout lb          = (LinearLayout) findViewById(R.id.linearMain);
        final    Controller   aController = (Controller) getApplicationContext();
        final    int          cartSize    = aController.getCart().getCartSize();
        //String                showString  = "";

        if(cartSize > 0){   
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

            for(int i = 0; i < cartSize; i++) {
                // Get product data from product data arraylist
                String pName  = aController.getCart().getProducts(i).getProductName();
                int    pPrice = aController.getCart().getProducts(i).getProductPrice();
                String pDisc  = aController.getCart().getProducts(i).getProductDesc();

                // Create LinearLayout to view elements
                LinearLayout ll = new LinearLayout(this);
                ll.setOrientation(LinearLayout.HORIZONTAL);             

                TextView product = new TextView(this);
                product.setText(" " + pName + "    ");

                // Add textView to LinearLayout
                ll.addView(product);

                TextView productdesc = new TextView(this);
                product.setText(" " + pDisc + "    ");

                // Add textView to LinearLayout
                ll.addView(productdesc);

                TextView price = new TextView(this);
                price.setText("  $" + pPrice + "     ");

                // Add textView to LinearLayout
                ll.addView(price);

                final Button btn = new Button(this);
                btn.setId(i + 1);
                btn.setText("Remove from Cart");

                // set the layoutParams on the button
                btn.setLayoutParams(params);
                final int index = i;

                //Create click listener for dynamically created button
                btn.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        // Get product instance for index
                        ModelProducts tempProductObject = aController.getCart().getProducts(index);
                        aController.getCart().removeProducts(tempProductObject);
                        Toast.makeText(getApplicationContext(),"Products Removed \n Now Cart size: "+ aController.getCart().getCartSize(), Toast.LENGTH_LONG).show();
                    }
                });

                // Add button to LinearLayout
                ll.addView(btn);

                // Add LinearLayout to XML layout
                lb.addView(ll);
            }
        }

        thirdBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if(cartSize > 0) {
                    Intent i = new Intent(getBaseContext(), Thirdscreen.class);
                    startActivity(i);
                } else
                    Toast.makeText(getApplicationContext(), "Shopping cart is empty.", Toast.LENGTH_LONG).show();
            }
        });
    } 

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

Upvotes: 1

Views: 116

Answers (2)

akash yadav
akash yadav

Reputation: 349

Please check you have not created ambiguity while creating views this could cuase a problem if you tried to define view with same ids

Upvotes: 0

Alexey Malev
Alexey Malev

Reputation: 6533

Most likely your lb is null because the call of

final LinearLayout lb          = (LinearLayout) findViewById(R.id.linearMain);

didn't find it by id as you expected.

Upvotes: 2

Related Questions