当前位置: 动力学知识库 > 问答 > 编程问答 >

php - VolleyError: java.lang.NullPointerException

问题描述:

Creating a Search function that retrieves specific data from PHP then puts that data in a list view. I'm having trouble accessing the data from the PHP, it returns a Null Point Exception. Here's the code

finder.java

 public class finder extends AppCompatActivity{

public static final String JSON_URL = "insert url here";

public static final String KEY_SEARCH = "search";

private SearchView searchView;

private ListView listView;

private String search;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_finder);

searchView = (SearchView) findViewById(R.id.sv);

listView = (ListView) findViewById(R.id.lv);

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override

public boolean onQueryTextSubmit(String query) {

sendRequest();

return true;

}

@Override

public boolean onQueryTextChange(String newText) {

return false;

}

});

}

private void sendRequest(){

StringRequest stringRequest = new StringRequest(Request.Method.POST, JSON_URL,

new Response.Listener<String>() {

@Override

public void onResponse(String response) {

showJSON(response);

}

},

new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

Toast.makeText(finder.this,error.toString(),Toast.LENGTH_LONG).show();

}

}){

@Override

protected Map<String,String> getParams(){

Map<String,String> params = new HashMap<>();

params.put(KEY_SEARCH,search);

return params;

}

};

RequestQueue requestQueue = Volley.newRequestQueue(this);

requestQueue.add(stringRequest);

}

private void showJSON(String json){

ParseJSON pj = new ParseJSON(json);

pj.parseJSON();

CustomList cl = new CustomList(this, ParseJSON.course,ParseJSON.school,ParseJSON.address);

listView.setAdapter(cl);

}

}

ParseJSON.java

 public class ParseJSON {

public static String[] course;

public static String[] school;

public static String[] address;

public static final String JSON_ARRAY = "Result";

public static final String KEY_COURSE = "Course";

public static final String KEY_SCHOOL = "School";

public static final String KEY_ADDRESS = "Address";

private String json;

public ParseJSON(String json){

this.json = json;

}

protected void parseJSON(){

JSONObject jsonObject=null;

try {

jsonObject = new JSONObject(json);

JSONArray users = jsonObject.getJSONArray(JSON_ARRAY);

course = new String[users.length()];

school = new String[users.length()];

address = new String[users.length()];

for(int i=0;i< users.length();i++){

JSONObject jo = users.getJSONObject(i);

course[i] = jo.getString(KEY_COURSE);

school[i] = jo.getString(KEY_SCHOOL);

address[i] = jo.getString(KEY_ADDRESS);

}

} catch (JSONException e) {

e.printStackTrace();

}

}

}

CustomList.java

 public class CustomList extends ArrayAdapter<String> {

private String[] course;

private String[] school;

private String[] address;

private Activity context;

public CustomList(Activity context, String[] course, String[] school, String[] address) {

super(context, R.layout.list_view_layout, course);

this.context = context;

this.course = course;

this.school = school;

this.address = address;

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();

View listViewItem = inflater.inflate(R.layout.list_view_layout, null, true);

TextView ViewCourse = (TextView) listViewItem.findViewById(R.id.textViewCourse);

TextView ViewSchool = (TextView) listViewItem.findViewById(R.id.textViewSchool);

TextView ViewAddress = (TextView) listViewItem.findViewById(R.id.textViewAddress);

ViewCourse.setText(course[position]);

ViewSchool.setText(school[position]);

ViewAddress.setText(address[position]);

return listViewItem;

}

}

search.php

 <?php

$DBserver = 'localhost';

$DBuser = 'root';

$DBpassword = '';

$DBname = 'finder';

[email protected]_connect($DBserver, $DBuser, $DBpassword, $DBname);

if ($conn->connect_error)

{

trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);

}

$output= array();

if(isset($_POST['search']))

{

$searchq = $_POST['search'];

//$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

$sql = "SELECT * FROM search WHERE search_course LIKE '%$searchq%'";

$query = mysqli_query($conn, $sql);

$count = mysqli_num_rows($query);

if ($count == 0)

{

$output = 'There was no search result';

}

else

{

while ($row = mysqli_fetch_array($query))

{

array_push ($output, array("Course"=>$row['search_course'],

"School"=>$row['search_school'], "Address"=>$row['search_location']

));

echo json_encode(array("Result"=>$output));

}

}

}

mysqli_close($conn)

?>

Logcat

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: [208] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at libcore.net.UriCodec.encode(UriCodec.java:132)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at java.net.URLEncoder.encode(URLEncoder.java:57)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.Request.encodeParameters(Request.java:476)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.Request.getBody(Request.java:462)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)

02-14 12:12:03.479 3450-3626/com.tesedfinder.user.tesedfinder E/Volley: at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

02-14 12:12:03.539 3450-3486/com.tesedfinder.user.tesedfinder W/EGL_emulation: eglSurfaceAttrib not implemented

02-14 12:12:03.539 3450-3486/com.tesedfinder.user.tesedfinder W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe99946c0, error=EGL_SUCCESS

02-14 12:12:06.991 3450-3486/com.tesedfinder.user.tesedfinder E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf2caa5e0

分享给朋友:
您可能感兴趣的文章:
随机阅读: