问题描述
林创建一个包含2的EditText领域一个非常基本的片段和一个提交按钮。
在提交的值应该被插入一个一行到我的SQLite数据库。结果
即时得到一个NullPointerException异常,我只是不明白..
在我的片段IM的onCreateView方法让我的字段值,并建立一个点击监听,是这样的:
查看rootView = inflater.inflate(R.layout.fragment_add_customer,集装箱,FALSE);
客户名称=(EditText上)rootView.findViewById(R.id.editCompanyName);
customerAddress =(EditText上)rootView.findViewById(R.id.editCompanyAddress);
Add按钮=(按钮)rootView.findViewById(R.id.submitCustomerBtn);
addButton.setOnClickListener(新View.OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
字符串sCustomerName = customerName.getText()的toString()。
字符串sCustomerAddress = customerAddress.getText()的toString()。
如果(sCustomerName.trim()长度()方式> 0&放大器;&放大器; sCustomerAddress.trim()长度()方式> 0){
客户客户=新客户();
customer.setName(sCustomerName);
customer.setAddress(sCustomerAddress);
customer.setLocation(未设置);
customer.setImage(无);
顾客= dataSource.createCustomer(客户);
如果(customer.getId()!= 0){
Toast.makeText(getActivity(),customer.getName()+ID为:+ customer.getId()+被创造!,Toast.LENGTH_LONG).show();
Log.i(LOGTAG,客户ID为创建+ customer.getId());
}
}其他{
Toast.makeText(getActivity(),请填写所有字段。Toast.LENGTH_LONG).show();
}
}
}); 返回rootView;
在我的DataSource类,其中i CONTROLE我的数据库查询和报表IM调用createCustomer方法,它看起来像这样:
公共客户createCustomer(客户的客户){
ContentValues值=新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());
长insertid = database.insert(BusinessDBOpenHelper.TABLE_CUSTOMER,空,价值);
customer.setId(insertid);
回报客户;
}
我的客户模型也只是containg长的ID,字符串名称,地址字符串,字符串位置,字符串图像的getter和setter ..结果
空的例外是:
显示java.lang.NullPointerException:尝试调用虚拟方法org.example.app.model.Customer org.example.app.db.BusinessDataSource.createCustomer(org.example.app在一个空对象引用.model.Customer)
在org.example.app.AddCustomerFragment $ 1.onClick(Fragment.java:55)
您
dataSource.createCustomer(客户);
为空,你必须初始化
数据源
地方
Im creating a very basic fragment containing 2 EditText fields and a submit button.On submit the values should be inserted as a row to my SQLite DB.
Im getting a NullPointerException i just dont get..
Inside my onCreateView method of the fragment im getting my field values and setting up a click listener like this:
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;
Inside my dataSource class where i controle my DB query and statement im calling the createCustomer method, which looks like this:
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;
}
My Customer model is also just containg long id, String name, String address, String location, String image with getters and setters..
The null exception is:
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)
Your
dataSource.createCustomer(customer);
is null, you have to initialize
dataSource
somewhere
这篇关于在插入对象的SQLite数据库空例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!