提问人:LookItsCashew 提问时间:3/3/2020 最后编辑:LookItsCashew 更新时间:3/3/2020 访问量:82
为什么我在 HttpURLConnection 中收到 NullPointerException
Why am I receiving a NullPointerException in HttpURLConnection
问:
我正在开发一个 Android 应用程序,该应用程序使用请求来检索响应中的过滤器列表。应用程序的这一部分从首次实现到最近一直在工作。当我注意到启动应用程序并打开检索过滤器的活动停止工作并会产生异常时,我正在处理项目的其他部分,尽管项目的这一部分没有受到任何直接代码更改的影响。GET
JSON
我调用服务器并接收筛选器列表的活动是,并且有一个调用,可确保此任务不会在 UI 层中执行。我使用一个调用来进行 HttpURLConnection 调用。正是在这个类中发生了异常。当我调试并单步执行时,当我尝试单步执行该行并收到以下错误时,会发生异常:。我不知道为什么会发生这种情况,因为我没有接触过这段代码,端点仍然发送响应(在浏览器中测试),我不知道这个值来自哪里。这是我和方法分别:FindMeARestaurantActivity
AsyncTask
GetFiltersTask
DAO
restaurantDAO
restaurantDAO.getFilters()
connection.connect()
java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
JSON
null
AsyncTask
getFilters()
DAO
public class GetFiltersTask extends AsyncTask<String, Integer, List<CheckboxDTO>> {
@Override
protected void onPreExecute() {
pbProgressBar.setVisibility(View.VISIBLE);
pbProgressBar.setProgress(0);
}
@Override
protected void onProgressUpdate(Integer... integers) {
pbProgressBar.setProgress(integers[0]);
}
@Override
protected List<CheckboxDTO> doInBackground(String... strings) {
String serverResponse = restaurantDAO.getFilters();
try
{
// Create JSON Object with contents of server response
JSONObject jsonResponseObject = new JSONObject(serverResponse);
// Grab the JSON Array of filters from the JSON Object
JSONArray jsonResponseArray = jsonResponseObject.getJSONArray("filters");
// For loop to iterate through each returned filter from server, generate a checkbox for them,
// then append those to the linear layout
for (int i = 0; i < jsonResponseArray.length(); i++)
{
// Create CheckboxDTO object
CheckboxDTO checkboxDTO = new CheckboxDTO();
// Grab JSON Object out of JSON Array
JSONObject objects = jsonResponseArray.getJSONObject(i);
// Grab the ID and Value from JSON Object
int id = objects.getInt("id");
String name = objects.getString("name");
// Assign JSON Object's key/value pair to CheckboxDTO's private fields
checkboxDTO.setId(id);
checkboxDTO.setValue(name);
// Add the CheckboxDTO to the checkboxes array
checkboxes.add(checkboxDTO);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return checkboxes;
}
protected void onPostExecute(List<CheckboxDTO> result) {
super.onPostExecute(result);
pbProgressBar.setVisibility(View.GONE);
makeCheckboxes(result);
}
}
和
@Override
public String getFilters() {
String inputLine;
String result;
try
{
// Create URL object to hold URL
URL filterURL = new URL(filtersURL);
// Create connection to server
HttpURLConnection connection = (HttpURLConnection) filterURL.openConnection();
// Set connection methods and timeouts
connection.setRequestMethod(FILTERS_REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
// Connect to server
connection.connect();
// Create InputStreamReader to read response from server
InputStreamReader streamReader = new InputStreamReader(connection.getInputStream());
// Create BufferedReader to read through InputStream
BufferedReader reader = new BufferedReader(streamReader);
StringBuilder stringBuilder = new StringBuilder();
// Check if the line being read is not null
while ((inputLine = reader.readLine()) != null){
stringBuilder.append(inputLine);
}
// Close out InputStream and BufferedReader
reader.close();
streamReader.close();
// Set result to our inputLine with stringBuilder
result = stringBuilder.toString();
connection.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
return result;
}
错误堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.chooseurfood, PID: 4108
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference
at java.net.CookieManager.get(CookieManager.java:214)
at com.android.okhttp.internal.http.HttpEngine.networkRequest(HttpEngine.java:714)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:223)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:126)
at dao.FindMeARestaurantDAO.getFilters(FindMeARestaurantDAO.java:146)
at com.chooseurfood.ui.FindMeARestaurantActivity$GetFiltersTask.doInBackground(FindMeARestaurantActivity.java:219)
at com.chooseurfood.ui.FindMeARestaurantActivity$GetFiltersTask.doInBackground(FindMeARestaurantActivity.java:204)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
任何帮助解决此问题将不胜感激。
答: 暂无答案
评论
android:name=ClassThatExtendsApplication