0

I'm creating cart button and cart inside the food order app. I get error after clicking on cart floatingaction button,following is the log cat and CartaAdapter.java. In the logcat I get error on the line 72 and 89. Also the screenshot of expected output is given and error output.i again got error on clicking the Place order button and after giving the addrress.unfortunately stops after clicking on order. error pic1 expected ouput after clicking yes on order pic2

Logcat error

  java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.example.sharma.digimenu.Model.User.getPhone()' on a null object reference
        at com.example.sharma.digimenu.Cart$2.onClick(Cart.java:95)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5292)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

Cart.java

public class Cart extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseDatabase database;
    DatabaseReference requests;

    TextView txtTotalPrice;
    Button btnPlace;

    List<Order> cart = new ArrayList<>();

    CartAdapter adapter;
    private DialogFragment dialogInterface;

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

        database = FirebaseDatabase.getInstance();
        requests = database.getReference("Requests");

        recyclerView = (RecyclerView) findViewById(R.id.listCart);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        txtTotalPrice = (TextView) findViewById(R.id.total);
        btnPlace = (Button) findViewById(R.id.btnPlaceOrder);

        btnPlace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });

        loadListFood();

    }

    private void showAlertDialog() {

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
        alertDialog.setTitle("One more step..");
        alertDialog.setMessage("Enter your Address");

        final EditText editAddress = new EditText(Cart.this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
        );
        editAddress.setLayoutParams(lp);
        alertDialog.setView(editAddress);
        alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);

        alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Request request = new Request(
                        Common.currentUser.getPhone(),
                        Common.currentUser.getName(),
                        editAddress.getText().toString(),
                        txtTotalPrice.getText().toString(),
                        cart
                );

                requests.child(String.valueOf(System.currentTimeMillis()))
                        .setValue(request);
                new Database(getBaseContext()).cleanCart();
                Toast.makeText(Cart.this,"Thankyou ,Order Placed",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
        alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) { 

                dialogInterface.dismiss();

            }
        });
        alertDialog.show();
    }
    private void loadListFood() {

        cart = new Database(this).getCarts();
        adapter = new CartAdapter(cart,this);
        recyclerView.setAdapter(adapter);

        int total= 0;
        for (Order order:cart)
            total+=(Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));

        Locale locale = new Locale("en", "IN");
        NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);

        txtTotalPrice.setText(fmt.format(total));
    }
}

Getting the error on this Common.currentUser.getPhone()

Common.java

  package com.example.sharma.digimenu.Common;

import com.example.sharma.digimenu.Model.Category;
import com.example.sharma.digimenu.Model.User;

public class Common
{
    public static User currentUser;
    public static Category currentuser;
}

Request.java

import java.util.List;

public class Request {

    private String phone;
    private String name;
    private String address;
    private String total;

    private List<Order>foods;

    public Request() {

    }

    public Request(String phone, String name, String address, String total, List<Order> foods) {
        this.phone = phone;
        this.name = name;
        this.address = address;
        this.total = total;
        this.foods = foods;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public List<Order> getFoods() {
        return foods;
    }

    public void setFoods(List<Order> foods) {
        this.foods = foods;
    }
}

User.java

public class User {

    private String email, password, name, phone;

    public User(String email, String password, String name,String phone) {
        this.email = email;
        this.password = password;
        this.name = name;
    }

    public User() {

    }

    public String getEmail() {
        return email;
    }
    public void setEmail(String email){
        this.email = email;
    }

    public String getPassword() {
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String password){
        this.phone = phone;
    }
    public String getName() {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
}
Isha Singh
  • 15
  • 6
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jun 30 '18 at 15:15

1 Answers1

0

You are returning an instance of CardViewHolder whereas your ViewHolder is named CartViewHolder.

    @NonNull
    @Override
    public CardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View itemView = inflater.inflate(R.layout.cart_layout, parent, false);

        return new CardViewHolder(itemView); <------- here's the problem
    }

Also change CardViewHolder to CartViewHolder everywhere in you CartAdapter class.

Badar Khan
  • 60
  • 1
  • 7
  • "currentUser" is a static memeber of the Common class and it has not been initialized anywhere in the code that you posted above, hence the NullPointerException. I'm not sure what you're trying to achieve here but if you want to store the user instance in persistent storage then read the [docs for sharedpreferences](https://developer.android.com/reference/android/content/SharedPreferences) – Badar Khan Jun 30 '18 at 16:27
  • In cart.java can u tell me what changes should b done.can u rewrite? – Isha Singh Jun 30 '18 at 16:48