JSON解析期间的空指针异常

时间:2014-02-28 17:47:57

标签: android json exception pointers null

这是我的代码,它会抛出java null指针异常。

private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{

    @Override
    protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

        InputStream iStream=null;
        String imgUrl = (String) hm[0].get("flag_path");
        int position = (Integer) hm[0].get("position");

        URL url;
        try {
            url = new URL(imgUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            // Getting Caching directory
            File cacheDirectory = getBaseContext().getCacheDir();

            // Temporary file to store the downloaded image
            File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+position+".png");

            // The FileOutputStream to the temporary file
            FileOutputStream fOutStream = new FileOutputStream(tmpFile);

            // Creating a bitmap from the downloaded inputstream
            Bitmap b = BitmapFactory.decodeStream(iStream);

            // Writing the bitmap to the temporary file as png file
            b.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

            // Flush the FileOutputStream
            fOutStream.flush();

           //Close the FileOutputStream
           fOutStream.close();

            // Create a hashmap object to store image path and its position in the listview
            HashMap<String, Object> hmBitmap = new HashMap<String, Object>();

            // Storing the path to the temporary image file
            hmBitmap.put("flag",tmpFile.getPath());

            // Storing the position of the image in the listview
            hmBitmap.put("position",position);

            // Returning the HashMap object containing the image path and position
            return hmBitmap;

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


        return null;
    }

    @Override
    protected void onPostExecute(HashMap<String, Object> result) {
        // Getting the path to the downloaded image
        String path = (String) result.get("flag");

        // Getting the position of the downloaded image
        int position = (Integer) result.get("position");

        // Getting adapter of the listview
        SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();

        // Getting the hashmap object at the specified position of the listview
        HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);

        // Overwriting the existing path in the adapter
        hm.put("flag",path);

        // Noticing listview about the dataset changes
        adapter.notifyDataSetChanged();



    }

这是LogCat

02-28 12:39:48.575: D/AndroidRuntime(1657): Shutting down VM
02-28 12:39:48.575: W/dalvikvm(1657): threadid=1: thread exiting with uncaught exception (group=0xb1a63ba8)
02-28 12:39:48.595: E/AndroidRuntime(1657): FATAL EXCEPTION: main
02-28 12:39:48.595: E/AndroidRuntime(1657): Process: com.xavier.hh, PID: 1657
02-28 12:39:48.595: E/AndroidRuntime(1657): java.lang.NullPointerException
02-28 12:39:48.595: E/AndroidRuntime(1657):     at com.xavier.hh.MainActivity$ListViewLoaderTask.onPostExecute(MainActivity.java:378)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at com.xavier.hh.MainActivity$ListViewLoaderTask.onPostExecute(MainActivity.java:1)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.os.AsyncTask.finish(AsyncTask.java:632)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.os.Looper.loop(Looper.java:136)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at java.lang.reflect.Method.invoke(Method.java:515)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-28 12:39:48.595: E/AndroidRuntime(1657):     at dalvik.system.NativeStart.main(Native Method)
02-28 12:39:52.925: I/Process(1657): Sending signal. PID: 1657 SIG: 9

0 个答案:

没有答案