java.lang.NullPointerException:尝试调用接口方法' boolean java.util.List.add(java.lang.Object)'在空对象引用上
这是我的文件
Stitle.java
的java
package com.desktop.app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Stitle extends AppCompatActivity {
EditText searchtitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stitle);
searchtitle = (EditText) findViewById(R.id.searchtitle);
searchtitle.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
List<list> productList;
//the recyclerview
RecyclerView recyclerView;
public void onCreate(Bundle savedInstanceState) {
Stitle.super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smain);
//getting the recyclerview from xml
recyclerView = findViewById(R.id.recylcerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(Stitle.this));
//initializing the productlist
productList = new ArrayList<>();
//this method will fetch and parse json
//to display it in recyclerview
performSearch();
}
private void performSearch() {
String url= "http://192.168.0.136/fyp/stitle.php";
final RequestQueue requestQueue = Volley.newRequestQueue(Stitle.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Log.d("Response", response.toString());
try {
//converting the string to json array object
JSONObject array = new JSONObject(response);
Log.i("test"," value : " + array.getString("status"));
if (array.getString("status").equals("true")){
JSONArray jsonArray =array.getJSONArray("search");
Log.i("test"," value : " + jsonArray);
for (int i = 0; i < jsonArray.length(); i++) {
//getting product object from json array
JSONObject product = jsonArray.getJSONObject(i);
//adding the product to product list
boolean add = productList.add(new list(
product.getInt("isbn"),
product.getString("title"),
product.getString("authors"),
product.getInt("accession"),
product.getString("publisher"),
product.getInt("pubyear"),
product.getInt("pages"),
product.getInt("rak"),
product.getInt("hr"),
product.getInt("vr"),
product.getInt("barcode")
));
}
}else{
Log.i("test","else error");
}
//traversing through all the object
//creating adapter object and setting it to recyclerview
listAdapter adapter = new listAdapter(Stitle.this, productList);
recyclerView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
Log.i("test",e.toString());
}
catch (Exception e){
Log.i("test",e.toString());
}
/*if(response.matches(toString())){
} else {
Toast.makeText(getApplicationContext(), "Unsuccessfull" ,Toast.LENGTH_LONG).show();
}*/
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "error:" +error.toString() ,Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Title", searchtitle.getText().toString());
return params;
}
};
requestQueue.add(stringRequest);
}
});
}
}
list.java
的java
package com.desktop.app;
public class list {
private long isbn;
private String title;
private String authors;
private int accession;
private String publisher;
private int pubyear;
private int pages;
private int rak;
private int hr;
private int vr;
private long barcode;
public list(long isbn, String title, String authors, int accession, String publisher, int pubyear, int pages, int rak, int hr, int vr, long barcode) {
this.isbn = isbn;
this.title = title;
this.authors = authors;
this.accession = accession;
this.publisher = publisher;
this.pubyear = pubyear;
this.pages = pages;
this.rak = rak;
this.hr = hr;
this.vr = vr;
this.barcode = barcode;
}
public long getIsbn() {
return isbn;
}
public String getTitle() {
return title;
}
public String getAuthors() {
return authors;
}
public int getAccession() {
return accession;
}
public String getPublisher() {
return publisher;
}
public int getPubyear(){
return pubyear;
}
public int getPages(){
return pages;
}
public int getRak(){
return rak;
}
public int getHr(){
return hr;
}
public int getVr(){
return vr;
}
public long getBarcode() {
return barcode;
}
}
listAdapter
的java
package com.desktop.app;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
//import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;
public class listAdapter extends RecyclerView.Adapter<listAdapter.ProductViewHolder> {
private Context mCtx;
private List<list> productList = new ArrayList<>();
listAdapter(Context mCtx, List<list> productList) {
this.mCtx = mCtx;
this.productList = productList;
}
@Override
public ProductViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mCtx);
@SuppressLint("InflateParams") View view = inflater.inflate(R.layout.slist, null);
return new ProductViewHolder(view);
}
@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
list product = productList.get(position);
holder.textviewisbn.setText(String.valueOf(product.getIsbn()));
holder.textviewtitle.setText(product.getTitle());
holder.textviewauthors.setText(product.getAuthors());
holder.textviewacc.setText(String.valueOf(product.getAccession()));
holder.textviewpublisher.setText(product.getPublisher());
holder.textviewpubyear.setText(String.valueOf(product.getPubyear()));
}
@Override
public int getItemCount() {
return productList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView textviewisbn, textviewtitle, textviewauthors, textviewacc, textviewpublisher, textviewpubyear;
ProductViewHolder(View itemView) {
super(itemView);
textviewisbn = itemView.findViewById(R.id.textviewisbn);
textviewtitle = itemView.findViewById(R.id.textviewtitle);
textviewauthors = itemView.findViewById(R.id.textviewauthors);
textviewacc = itemView.findViewById(R.id.textviewacc);
textviewpublisher = itemView.findViewById(R.id.textviewpublisher);
textviewpubyear = itemView.findViewById(R.id.textviewpubyear);
}
}
}
当我运行我的应用程序时没有错误,但在调试选项卡上: 价值:[{&#34; isbn&#34;:195472462,&#34; title&#34;:&#34; Oxford Practice Grammer&#34;,&#34;作者&#34;:&#34; john Eastwood& #34;,&#34;加入&#34;:1,&#34;出版商&#34;:&#34; Ameena Saiyid牛津大学&#34;,&#34; pubyear&#34;:2014,&#34 ;网页&#34;:432,&#34; RAK&#34;:1,&#34; HR&#34;:1,&#34; VR&#34;:1,&#34;条形码&#34 ;: 195472462}] 05-23 23:51:24.012 780-780 / com.desktop.app I / test:java.lang.NullPointerException:尝试调用接口方法&#39; boolean java.util.List.add(java.lang.Object )&#39;在空对象引用上