01/10/2018, 12:12

Cách Sửa lỗi FATAL EXCEPTION: AsyncTask #1 ,java.lang.NullPointerException?

Xin mọi người chỉ dạy giúp em cách khắc phục lỗi này với ạ, em đã tham khảo một số cách trên mạng hơn một tuần nay mà không được. Dưới là code và báo lỗi của em, mong mọi người giúp đỡ.

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

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());  // BÁO LỖI Ở DÒNG NÀY

            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);

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

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, name);

                        // adding HashList to ArrayList
                        productsList.add(map);
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            NewProductActivity.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

   ---------
class LoadAllProducts extends AsyncTask<String, String, String> { // BÁO LỖI Ở DÒNG NÀY

        /**
         * Before starting background thread Show Progress Dialog
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AllProductsActivity.this);
            pDialog.setMessage("Loading products. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();                                          // BÁO LỖI Ở DÒNG NÀY
        }
--------

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.all_products);

        // Hashmap for ListView
        productsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();                                // BÁO LỖI Ở DÒNG NÀY

        // Get listview
        ListView lv = getListView();

        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem
                String pid = ((TextView) view.findViewById(R.id.pid)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        EditProductActivity.class);
                // sending pid to next activity
                in.putExtra(TAG_PID, pid);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });
Lê Vũ Huy viết 14:27 ngày 01/10/2018

Hỏi mấy cái kiểu này mình nghĩ phải teamview mới fix được

Bảo Ngọc Nguyễn viết 14:26 ngày 01/10/2018

Các anh có giải pháp thử nào không để em thử với ạ.

Lê Hùng viết 14:23 ngày 01/10/2018

Lỗi dòng nào vậy bạn ? bạn post cả code lỗi lên đây nhé

Đỗ Trung Quân viết 14:25 ngày 01/10/2018

Check URL: url_all_products

url_all_products 

If you are referring to a localhost from your device than use the http://10.0.2.2:8080/ instead of the http://127.0.0.1/ or http://localhost/.

Because your Android emulator is running on a Virtual Machine(QEMU) and you can not connect to a server directly running on your PC.

And If you running your app from Physical android device then please use your network ip from your PC. for example http://198.10.12.21:80/

thanggun99 viết 14:13 ngày 01/10/2018

Dịch cái đoạn báo lỗi đấy ra sơ sơ là do chạy trên máy ảo không kết nối được với máy chủ nội bộ (localhost) thay localhost thành địa chỉ ip network của máy. Ấn window + r gõ ncpa.cpl rồi chọn cái virtual network mà virtual box nó tạo cho thằng máy ảo đấy để lấy địa chỉ ip

Bài liên quan
0