将对象插入SQLite DB时出现空例外

时间:2014-11-11 00:45:16

标签: java android sqlite android-fragments

我创建一个包含2个EditText字段和一个提交按钮的非常基本的片段。 提交时,应将值作为一行插入到我的SQLite DB中 我得到一个NullPointerException我只是没有得到..

在片段的onCreateView方法中,我获取我的字段值并设置一个点击监听器,如下所示:

View rootView = inflater.inflate(R.layout.fragment_add_customer, container, false);
    customerName = (EditText) rootView.findViewById(R.id.editCompanyName);
    customerAddress = (EditText) rootView.findViewById(R.id.editCompanyAddress);
    addButton = (Button) rootView.findViewById(R.id.submitCustomerBtn);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String sCustomerName = customerName.getText().toString();
            String sCustomerAddress = customerAddress.getText().toString();
            if(sCustomerName.trim().length() > 0 && sCustomerAddress.trim().length() > 0) {
                Customer customer = new Customer();
                customer.setName(sCustomerName);
                customer.setAddress(sCustomerAddress);
                customer.setLocation("Not set");
                customer.setImage("None");
                customer = dataSource.createCustomer(customer);
                if(customer.getId() != 0) {
                    Toast.makeText(getActivity(), customer.getName() + " with id: " + customer.getId() + " was created!", Toast.LENGTH_LONG).show();
                    Log.i(LOGTAG, "Customer created with id " + customer.getId());
                }
            }else{
                Toast.makeText(getActivity(), "Please fill all fields.", Toast.LENGTH_LONG).show();
            }
        }
    });

    return rootView;

在我的dataSource类中,我控制了我的数据库查询和语句,我调用了createCustomer方法,如下所示:

public Customer createCustomer(Customer customer) {
    ContentValues values = new ContentValues();
    values.put(BusinessDBOpenHelper.CUSTOMER_NAME, customer.getName());
    values.put(BusinessDBOpenHelper.CUSTOMER_ADDRESS, customer.getAddress());
    values.put(BusinessDBOpenHelper.CUSTOMER_LOCATION, customer.getLocation());
    values.put(BusinessDBOpenHelper.CUSTOMER_IMAGE, customer.getImage());
    long insertid = database.insert(BusinessDBOpenHelper.TABLE_CUSTOMER, null, values);
    customer.setId(insertid);
    return customer;
}

我的客户模型也只包含长ID,字符串名称,字符串地址,字符串位置,带有getter和setter的字符串图像。
null异常是:

java.lang.NullPointerException: Attempt to invoke virtual method 'org.example.app.model.Customer org.example.app.db.BusinessDataSource.createCustomer(org.example.app.model.Customer)' on a null object reference
        at org.example.app.AddCustomerFragment$1.onClick(Fragment.java:55)

3 个答案:

答案 0 :(得分:2)

你的

dataSource.createCustomer(customer);

为null,您必须初始化

dataSource 

某处

答案 1 :(得分:1)

错误堆栈表示您正在调用null dataSource上的createCustomer。

你在哪里初始化它?找到它并确保在调用onClick()时它不为空。

 java.lang.NullPointerException: Attempt to invoke virtual method 'org.example.app.model.Customer
 org.example.app.db.BusinessDataSource.createCustomer(org.example.app.model.Customer)'
 on a null object reference

答案 2 :(得分:0)

我有同样的问题,我发现了一个问题。如果有其他人需要,我会尽力详细解释:

首先,您必须检查对象'dataSource'是否为typed = SQLiteDatabase,就像那样:

private SQLiteDatabase dataSource;

并在使用之前初始化对象,如下所示:

dataSource = this.getWritableDatabase();