Aync Task doInBackground方法NullPointerException错误

时间:2019-08-28 14:56:17

标签: java php android mysql nullpointerexception

使用asynctask网址“ http://10.0.2.2/giveaways/tickets.php”和php脚本从Mysql数据库加载图像。 asynctask,在Android模拟器上构建/运行后,执行doInBackground()方法时发生错误,提示NullPointerException错误。

php脚本签出正常,它将使用多个浏览器通过localhost从数据库返回条目。

我需要在哪里重新编辑代码块?

重做了php脚本,检查了我的wamp设置,并重新编辑了来自tut的asynctask。 回到同一个错误doInBackground()方法会提示NullPointerException

asynctask doInBackground()方法 FetchDataTask类扩展了AsyncTask {

    ArrayList<Model> mdl;
    ProductsAdapter mProductsAdapter;
    private static final String sAssetPath = "puzzles";


    @Override
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url, "GET", params);

        // Check your log cat for JSON reponse

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                mdl = new ArrayList<Model>();
                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String type = c.getString(TAG_TYPE);
                    String name = c.getString(TAG_NAME);
                    Model model = new Model(
                            "http://10.0.2.2/giveaways/" + name, type);

                    Log.d(name, "hint");
                    model.setmUrl("http://10.0.2.2/giveaways/" + name);

                    model.setmCaption(type);
                    mdl.add(model);

                }

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

        return null;
    }

php脚本

    <?php

    $dbHost = "localhost";

    $dbUsername = "root";

    $dbPassword = "";

    $dbName = "android_im";

    $con = @mysqli_connect("localhost","root","","android_im");


    $response = array();

    // get all products from products table

    @$result = mysqli_query($con,"SELECT * FROM freebies ORDER By id") or 

    die(mysqli_error($con));

    // check for empty result

    if (mysqli_num_rows($result) > 0) {

    // looping through all results#003366

    // products node

    $response["products"] = array();

    while ($row = mysqli_fetch_array($result)) {

    // temp user array

    $product = array();

    $product["image_name"] = $row["image_name"];

    $product["image_type"] = $row["image_type"];

    // push single product into final response array

    array_push($response["products"], $product);
    }

    // success

    $response["success"] = 1;

    // echoing JSON response

    echo json_encode($response);

    } else {

    // no products found

    $response["success"] = 0;

    $response["message"] = "No products found";

    // echo no users JSON

    echo json_encode($response);
    }

    ?>

错误

2019-08-28 16:58:30.950 19797-19822/com.example.navigationswipe E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.navigationswipe, PID: 19797
java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:354)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference
    at com.example.navigationswipe.activity.blur.SimpleViewPagerActivity$FetchDataTask.doInBackground(SimpleViewPagerActivity.java:91)
    at com.example.navigationswipe.activity.blur.SimpleViewPagerActivity$FetchDataTask.doInBackground(SimpleViewPagerActivity.java:60)
    at android.os.AsyncTask$2.call(AsyncTask.java:333)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
    at java.lang.Thread.run(Thread.java:764) 

0 个答案:

没有答案