Question

I have an error when parsing image url to image and displaying it in my listview.. i have caught the error but im not able to rectify it .... (caught = know where the error is -_-)

First of all im using SAX. Second , im doing a variety of parsing's so i smashed up everything in one activity. third, i will send the log without the exception caught.

the error is the last piece of code... where there is try and catch.

    public class MainActivity extends Activity {
    URL url;
    ListView list;

    ArrayList<String> titles = new ArrayList<String>();
    ArrayList<String> artists = new ArrayList<String>();
    ArrayList<String> countries = new ArrayList<String>();
    ArrayList<String> companies = new ArrayList<String>();
    ArrayList<String> prices = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = (ListView) findViewById(R.id.listView1);
        MyAsync ma = new MyAsync();
        ma.execute();

        list.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    // getting values from selected ListItem

    if(position == 0){

    Intent a = new Intent(MainActivity.this , custom.class);
    tartActivity(a);

    }

    }
    });

        }

        class MyAsync extends AsyncTask<Void, Void, Void> {
        ProgressDialog pd;

        @Override
        protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        pd = ProgressDialog.show(MainActivity.this, "", "loading pc parts");
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
        try {
                url = new        URL("https://dl.dropboxusercontent.com/u/74662678/graphicscards.xml");

                // Create an instance for SAXParserFactory
                SAXParserFactory mySAXParserFactory = SAXParserFactory
                        .newInstance();

                // Create an instance for SAXParser
                SAXParser mySAXParser = mySAXParserFactory.newSAXParser();

                // Create an instance for XMLReader
                XMLReader myXMLReader = mySAXParser.getXMLReader();

                // Create an instance for customized handler class
                XMLHandler myXMLHandler = new XMLHandler();

                // apply handler to the XMLReader
                myXMLReader.setContentHandler(myXMLHandler);

                // open the connection
                InputSource is = new InputSource(url.openStream());



                // parse the data
                myXMLReader.parse(is);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SAXException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            pd.cancel();
            CustomAdapter ca = new CustomAdapter();
            list.setAdapter(ca);

        }

    }

    class XMLHandler extends DefaultHandler {

        boolean TITLE = false;
        boolean BRAND = false;
        boolean USE = false;
        boolean URL = false;
        boolean COST = false;


        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            // TODO Auto-generated method stub
            super.startElement(uri, localName, qName, attributes);

            if (localName.equals("TITLE")) {
                this.TITLE = true;
            } else if (localName.equals("BRAND")) {
                this.BRAND = true;
            } else if (localName.equals("USE")) {
                this.USE = true;
            } else if (localName.equals("URL")) {
                this.URL = true;
            } else if (localName.equals("COST")) {
                this.COST = true;
            } 
        }

        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            // TODO Auto-generated method stub
            super.characters(ch, start, length);
            if (this.TITLE) {
                titles.add(new String(ch, start, length));
            } else if (this.BRAND) {
                artists.add(new String(ch, start, length));

            } else if (this.USE) {
                countries.add(new String(ch, start, length));

            } else if (this.URL) {
                companies.add(new String(ch, start, length));

            } else if (this.COST) {
                prices.add(new String(ch, start, length));

            }

        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            // TODO Auto-generated method stub
            super.endElement(uri, localName, qName);
            if (localName.equals("TITLE")) {
                this.TITLE = false;
            } else if (localName.equals("BRAND")) {
                this.BRAND = false;
            } else if (localName.equals("USE")) {
                this.USE = false;
            } else if (localName.equals("URL")) {
                this.URL = false;
            } else if (localName.equals("COST")) {
                this.COST = false;
            } 
        }
    }

    class CustomAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return titles.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int index, View v, ViewGroup arg2) {
            // TODO Auto-generated method stub
            v = getLayoutInflater().inflate(R.layout.custom, null);

            TextView t1 = (TextView) v.findViewById(R.id.title);
            TextView t2 = (TextView) v.findViewById(R.id.artist);
            TextView t3 = (TextView) v.findViewById(R.id.textView1);
            TextView t4 = (TextView) v.findViewById(R.id.duration);

            String song = new String();
            song = titles.get(index);


            t1.setText(titles.get(index).toString());
            t2.setText(artists.get(index).toString());
            t3.setText(countries.get(index).toString());
            t4.setText(prices.get(index).toString());

            //loading list picture

            String a = companies.get(index).toString();
            ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);

            try{

            int loader = R.drawable.loader;
            ImageLoader imgLoader = new ImageLoader(getApplicationContext());
            imgLoader.DisplayImage(a, loader, lblThumb);

            }catch(Exception e){
                Log.d("error" , "lol");
            }


            return v;
        }

    }
}

LOG:-

03-19 08:28:34.635: E/AndroidRuntime(3635): FATAL EXCEPTION: main
03-19 08:28:34.635: E/AndroidRuntime(3635): java.lang.NullPointerException
03-19 08:28:34.635: E/AndroidRuntime(3635):     at com.ram.saxparserexample.ImageLoader.DisplayImage(ImageLoader.java:48)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at com.ram.saxparserexample.MainActivity$CustomAdapter.getView(MainActivity.java:273)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.ListView.fillDown(ListView.java:674)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.ListView.fillFromTop(ListView.java:735)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.ListView.layoutChildren(ListView.java:1652)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.os.Handler.handleCallback(Handler.java:725)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.os.Looper.loop(Looper.java:137)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at java.lang.reflect.Method.invoke(Method.java:511)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 08:28:34.635: E/AndroidRuntime(3635):     at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution 2

In your ImageLoader class change your context from getApplicationContext() to MainActivity.this

And also access your ImageView in context of your View as below in your getView method:

ImageView lblThumb = (ImageView)v.findViewById(R.id.thumbie);

OTHER TIPS

You have to change this from

 ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);

to

 ImageView lblThumb = (ImageView) v.findViewById(R.id.thumbie);

because whenever you have find ID for your UI control in getView() method at that time you need to pass reference of your View's object to find of IDs.

Try this..

ImageView lblThumb = (ImageView) v.findViewById(R.id.thumbie);

Instead of this..

ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top