0

I have a Java class that is going to have a number of inner classes. This is done for organization and to keep things in a separate file.

public class PUCObjects
{

    public static class PUCNewsItem
    {

        public String title;
        public String summary;
        public String body;
        public String url;
        public String imageUrl;

    }

}

I am then trying to create a new instance of that inner class (doing this in another class that parses some remote XML), but for some reason it doesn't seem to get created:

public static ArrayList<PUCObjects.PUCNewsItem> getPUCNews() throws IOException {

        String url = "http://api.puc.edu/news/list?key="+API_KEY+"&count=30";
        InputStream is = downloadUrl(url);
        XmlPullParserFactory pullParserFactory;

        try {
            pullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = pullParserFactory.newPullParser();
            parser.setInput(is, null);

            ArrayList<PUCObjects.PUCNewsItem> items = null;
            int eventType = parser.getEventType();
            PUCObjects.PUCNewsItem item = null;
            Log.d("Debug: ", "Start: "+url);
            while (eventType != XmlPullParser.END_DOCUMENT){
                String name = null;
                switch (eventType){
                    case XmlPullParser.START_DOCUMENT:
                        items = new ArrayList<PUCObjects.PUCNewsItem>();
                        break;
                    case XmlPullParser.START_TAG:
                        name = parser.getName();
                        //Log.d("Start Tag Name: ", parser.getName()+"  ===  "+name);
                        if (name == "item"){
                            Log.d("Debug: ", "Item");
                            item = new PUCObjects.PUCNewsItem();
                        } else if (item != null){
                            Log.d("Debug: ", "Item is not NULL 2");
                            if (name == "title"){
                                Log.d("Title: ", parser.nextText());
                                item.title = parser.nextText();
                            } else if (name == "summary"){
                                item.summary = parser.nextText();
                            } else if (name == "body_text"){
                                item.body = parser.nextText();
                            }  
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase("item") && item != null) {
                            Log.d("Debug: ", "ADD ITEM");
                            items.add(item);
                        }
                        break;
                }//end switch

                eventType = parser.next();

            }//end while

            Log.d("Debug: ", "Done");
            return items;

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

        return null;

    }//end

I am trying to create the object like item = new PUCObjects.PUCNewsItem(); but it seems to always be null.

Is there a reason why this is object isn't getting created?

1 Answer 1

5

Problem is String comparison. Your if statement is not resulting to true due to == check.

if (name == "item"){

You need to use equals() method instead of == when comparing Objects/Strings. Read this thread for more information on eqauals() vs ==

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.