mercredi 6 avril 2016

Null Pointer Exception When Retrieving Data

Hello I am new to android, I am trying to build a simple cart in my application however when I try to load the data from my cart a Null pointer Exception error gets thrown, my cart is stored in an SQLite database, I have verified that the table contains at least one row of data this is my Cart Class/Object:

package com.xxxxxx.vendor.objects;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by djoks on 04/04/2016.
 */
public class Cart {

    private JSONObject info;
    private JSONObject customer;
    private JSONArray tickets;
    private JSONArray extras;

    public Cart(JSONObject info, JSONObject customer, JSONArray tickets, JSONArray extras) {
        super();

        this.info = info;
        this.customer = customer;
        this.tickets = tickets;
        this.extras = extras;
    }

    public Cart(JSONObject info, JSONObject customer, JSONArray tickets) {
        super();

        this.info = info;
        this.customer = customer;
        this.tickets = tickets;
    }

    public Cart() {
        super();
    }

    public JSONObject getInfo() {
        return info;
    }

    public void setInfo(JSONObject info) {
        this.info = info;
    }

    public JSONObject getCustomer() {
        return this.customer;
    }

    public void setCustomer(JSONObject customer) {
        this.customer = customer;
    }

    public JSONArray getTickets() {
        return tickets;
    }

    public void setTickets(JSONArray tickets) {
        this.tickets = tickets;
    }

    public JSONArray getExtras() {
        return extras;
    }

    public void setExtras(JSONArray extras) {
        this.extras = extras;
    }

    public static class Info {
        private String type;
        private int id;
        private String name;

        public Info(String type, int id, String name) {
            super();

            this.type = type;
            this.id = id;
            this.name = name;
        }

        public Info() {
            super();
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

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

        public JSONObject toJSON() {
            JSONObject jObj = new JSONObject();
            try {
                jObj.put("type", getType());
                jObj.put("id", getId());
                jObj.put("name", getName());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return jObj;
        }
    }

    public static class Customer {

        private String fullname;
        private String gender;
        private String phone;
        private String email;
        private String address;

        public Customer(String fullname, String gender, String phone, String email, String address) {
            super();

            this.fullname = fullname;
            this.gender = gender;
            this.phone = phone;
            this.email = email;
            this.address = address;
        }

        public Customer(String fullname, String gender, String phone, String email) {
            super();

            this.fullname = fullname;
            this.gender = gender;
            this.phone = phone;
            this.email = email;
            this.address = "";
        }


        public Customer() {
            super();
        }

        public String getFullname() {
            return fullname;
        }

        public void setFullname(String fullname) {
            this.fullname = fullname;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getPhone() {
            return phone;
        }

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

        public String getEmail() {
            return email;
        }

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

        public String getAddress() {
            return address;
        }

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

        public JSONObject toJSON() {
            JSONObject jObj = new JSONObject();
            try {
                jObj.put("fullname", getFullname());
                jObj.put("gender", getGender());
                jObj.put("phone", getPhone());
                jObj.put("email", getEmail());
                jObj.put("address", getAddress());

            } catch (JSONException e) {
                e.printStackTrace();
            }
            return  jObj;
        }
    }

    public static class Ticket {
        private int id;
        private String tclass;
        private String description;
        private float price;
        private int quantity;

        public Ticket(int id, String ticketClass, String description, float price, int quantity) {
            super();

            this.id = id;
            this.tclass = ticketClass;
            this.description = description;
            this.price = price;
            this.quantity = quantity;
        }

        public Ticket() {
            super();
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getTclass() {
            return tclass;
        }

        public void setTclass(String tclass) {
            this.tclass = tclass;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public float getPrice() {
            return price;
        }

        public void setPrice(float price) {
            this.price = price;
        }

        public int getQuantity() {
            return quantity;
        }

        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }

        public JSONObject toJSON() {
            JSONObject jObj = new JSONObject();
            try {
                jObj.put("id", getId());
                jObj.put("class", getTclass());
                jObj.put("price", getPrice());
                jObj.put("description", getDescription());
                jObj.put("quantity", getQuantity());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return  jObj;
        }

    }

}

This is the function that gets the cart data:

public Cart get(int cartId){
    db = LocalDBDataSource.getHelper(context).getWritableDatabase();
    Cursor c = null;

    String whereClause = "cart_id=?";
    String[] whereParams = {String.valueOf(cartId)};
    String[] columns = {"*"};

    try{
        c = db.query(true, "cart", columns, whereClause, whereParams, null, null, null, null);
        if(c.getCount() > 0) {
            if (c.moveToFirst()) {
                return new Cart(
                        new JSONObject(c.isNull(c.getColumnIndex("info")) ? "{}" : c.getString(c.getColumnIndex("info"))),
                        new JSONObject(c.isNull(c.getColumnIndex("customer")) ? "{}" : c.getString(c.getColumnIndex("customer"))),
                        new JSONArray(c.isNull(c.getColumnIndex("tickets")) ? "[]" : c.getString(c.getColumnIndex("tickets"))),
                        new JSONArray(c.isNull(c.getColumnIndex("extras")) ? "[]" : c.getString(c.getColumnIndex("extras")))
                );
            }

        }
    } catch (JSONException e) {
        e.printStackTrace();
    } finally{
        if(c !=null && !c.isClosed()){
            c.close();
        }

        if(db != null && db.isOpen()){
            db.close();
        }
    }

    return null;
}

And in my activity the error gets thrown on the following line:

co = cm.get(1);
Log.i("Customer", co.getCustomer().toString());

Aucun commentaire:

Enregistrer un commentaire