dpj997991 2014-12-10 00:23
浏览 131

另一个“错误转换结果java.lang.NullPointerException:lock == null”的情况

I wrote & ran this code but tried it again the next only to get an error. The PHP script still runs perfectly in the browser. I understand the server isn't parsing anything back but i can't understand why. I have looked through all (i think) of the questions with the same LogCat error here and loads of other online sources but in vain. I don't understand what could've changed when i shutdown my Mac that affected the code. Thank you

Here's the java code

public class downloadData extends AsyncTask<Void, Void, Boolean>    {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading list...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... voids) {
        updateJSONData();
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        Log.i("onPostExecute", "downloadData complete.");
        DBAdapter handler = new DBAdapter(getBaseContext());
    }
}

public void updateJSONData() {

  JSONArray listJsonArray;
  JSONParser jParser = new JSONParser();
  JSONObject json = jParser.getJSONFromUrl(READ_ENTRIES_URL);

  if (json != null) {
    Log.i("updateJSONData", "MySQL download complete.");
    DBAdapter handler = new DBAdapter(getBaseContext());
    handler.wipeMainTable();

    try {
      listJsonArray = json.getJSONArray(TAG_ENTRIES);
      Log.i("updateJSONData", listJsonArray.length() + " entries downloaded");
      handler.open();

      for (int i = 0; i < listJsonArray.length(); i++) {
        JSONObject listJsonArrayObject = listJsonArray.getJSONObject(i);

        int entry_id = listJsonArrayObject.getInt(TAG_ID);
        String title = listJsonArrayObject.getString(TAG_TITLE);
        String link = listJsonArrayObject.getString(TAG_LINK);
        String published = listJsonArrayObject.getString(TAG_PUBLISHED);

        handler.insertEntry(entry_id, title, link, published);
        Log.i("updateJSONData", "saved entry " + entry_id + " to SQLite database.");

      }
      handler.close();

    } catch (JSONException e) {
      e.printStackTrace();
    }

  } else {
    Log.i("JsonUpdate", "Nothing returned from server");
  }
}

My JSONParser class.

public class JSONParser {

  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";

  // constructor
  public JSONParser() {

  }

  public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
      // defaultHttpClient
      DefaultHttpClient httpClient = new DefaultHttpClient();
      HttpPost httpPost = new HttpPost(url);

      HttpResponse httpResponse = httpClient.execute(httpPost);
      HttpEntity httpEntity = httpResponse.getEntity();
      is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "
");
      }
      is.close();
      json = sb.toString();
    } catch (Exception e) {
      Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
      jObj = new JSONObject(json);
    } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

  }

  public JSONObject makeHttpRequest(String url, String method,
    List < NameValuePair > params) {

    // Making HTTP request
    try {

      // check for request method
      if (method == "POST") {
        // request method is POST
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

      } else if (method == "GET") {
        // request method is GET
        DefaultHttpClient httpClient = new DefaultHttpClient();
        String paramString = URLEncodedUtils.format(params, "utf-8");
        url += "?" + paramString;
        HttpGet httpGet = new HttpGet(url);

        HttpResponse httpResponse = httpClient.execute(httpGet);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
      }

    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8);
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "
");
      }
      is.close();
      json = sb.toString();
    } catch (Exception e) {
      Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
      jObj = new JSONObject(json);
    } catch (JSONException e) {
      Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
  }

}

PHP script

<?php

require("config.inc.php");

$query = "Select * FROM entry";

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die(json_encode($response));
}

$rows = $stmt->fetchAll();


if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["entries"]   = array();

foreach ($rows as $row) {
    $post             = array();
    $post["ID"] = $row["entry_id"];
    $post["title"]    = $row["title"];
    $post["link"]  = $row["link"];
    $post["published"]  = $row["published"];

    array_push($response["entries"], $post);
}

echo json_encode($response);


} else {
    $response["success"] = 0;
    $response["message"] = "No Post Available!";
    die(json_encode($response));
}

?>

My LogCat

12-09 22:54:00.156: I/mainActivity(1431): Starting ...
12-09 22:54:00.164: D/OpenGLRenderer(1431): Render dirty regions requested: true
12-09 22:54:00.165: D/(1431): HostConnection::get() New Host Connection established 0xa5c11170, tid 1431
12-09 22:54:00.184: D/Atlas(1431): Validating map...
12-09 22:54:00.219: D/libEGL(1431): loaded /system/lib/egl/libEGL_genymotion.so
12-09 22:54:00.220: D/(1431): HostConnection::get() New Host Connection established 0xa5c113a0, tid 1447
12-09 22:54:00.225: W/System.err(1431): org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.6:8888 refused
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-09 22:54:00.227: W/System.err(1431):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-09 22:54:00.227: W/System.err(1431):     at com.example.sine.JSONParser.getJSONFromUrl(JSONParser.java:43)
12-09 22:54:00.227: W/System.err(1431):     at com.example.sine.MainActivity.updateJSONData(MainActivity.java:67)
12-09 22:54:00.227: W/System.err(1431):     at com.example.sine.MainActivity$downloadData.doInBackground(MainActivity.java:293)
12-09 22:54:00.227: W/System.err(1431):     at com.example.sine.MainActivity$downloadData.doInBackground(MainActivity.java:1)
12-09 22:54:00.227: W/System.err(1431):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-09 22:54:00.227: W/System.err(1431):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-09 22:54:00.227: W/System.err(1431):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-09 22:54:00.227: W/System.err(1431):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-09 22:54:00.227: W/System.err(1431):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-09 22:54:00.227: W/System.err(1431):     at java.lang.Thread.run(Thread.java:818)
12-09 22:54:00.227: W/System.err(1431): Caused by: java.net.ConnectException: failed to connect to /192.168.0.6 (port 8888): connect failed: ENETUNREACH (Network is unreachable)
12-09 22:54:00.228: W/System.err(1431):     at libcore.io.IoBridge.connect(IoBridge.java:124)
12-09 22:54:00.228: W/System.err(1431):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
12-09 22:54:00.228: W/System.err(1431):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
12-09 22:54:00.228: W/System.err(1431):     at java.net.Socket.connect(Socket.java:882)
12-09 22:54:00.228: W/System.err(1431):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-09 22:54:00.228: W/System.err(1431):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
12-09 22:54:00.228: W/System.err(1431):     ... 16 more
12-09 22:54:00.228: W/System.err(1431): Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
12-09 22:54:00.228: W/System.err(1431):     at libcore.io.Posix.connect(Native Method)
12-09 22:54:00.228: W/System.err(1431):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
12-09 22:54:00.228: W/System.err(1431):     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
12-09 22:54:00.228: W/System.err(1431):     at libcore.io.IoBridge.connect(IoBridge.java:122)
12-09 22:54:00.228: W/System.err(1431):     ... 21 more
12-09 22:54:00.228: E/Buffer Error(1431): Error converting result java.lang.NullPointerException: lock == null
12-09 22:54:00.228: E/JSON Parser(1431): Error parsing data org.json.JSONException: End of input at character 0 of 
12-09 22:54:00.229: I/JsonUpdate(1431): Nothing returned from web service
12-09 22:54:00.230: D/libEGL(1431): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
12-09 22:54:00.233: D/libEGL(1431): loaded /system/lib/egl/libGLESv2_genymotion.so
12-09 22:54:00.239: I/OpenGLRenderer(1431): Initialized EGL, version 1.4
12-09 22:54:00.254: D/OpenGLRenderer(1431): Enabling debug mode 0
12-09 22:54:00.259: W/EGL_genymotion(1431): eglSurfaceAttrib not implemented
12-09 22:54:00.259: W/OpenGLRenderer(1431): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5c766a0, error=EGL_SUCCESS
12-09 22:54:00.269: W/EGL_genymotion(1431): eglSurfaceAttrib not implemented
12-09 22:54:00.269: W/OpenGLRenderer(1431): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa5c766c0, error=EGL_SUCCESS
12-09 22:54:00.365: I/onPostExecute(1431): downloadData complete.
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大