-2

I recently tried my hand at a bit of coding to make a trial search function that then reads an RSS feed and lists it.

package com.example.owner.jabexam;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ListActivity
{
    private EditText mEditText;
    List headlines;
    List links;
    List discription;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        new MyAsyncTask().execute();
        mEditText = (EditText) findViewById(R.id.SearchTB);
    }

    class MyAsyncTask extends AsyncTask<Object,Void,ArrayAdapter>
    {
        private String urlLink;
        @Override
        protected void onPreExecute(){
            urlLink = mEditText.getText().toString();
        }

        @Override
        protected ArrayAdapter doInBackground(Object[] params)
        {
            headlines = new ArrayList();
            links = new ArrayList();
            discription = new ArrayList();

            try
            {
                URL url = new URL(urlLink);
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(false);
                XmlPullParser xpp = factory.newPullParser();

                // We will get the XML from an input stream
                xpp.setInput(getInputStream(url), "UTF_8");
                boolean insideItem = false;

                // Returns the type of current event: START_TAG, END_TAG, etc..
                int eventType = xpp.getEventType();
                while (eventType != XmlPullParser.END_DOCUMENT)
                {
                    if (eventType == XmlPullParser.START_TAG)
                    {
                        if (xpp.getName().equalsIgnoreCase("item"))
                        {
                            insideItem = true;
                        }
                        else if (xpp.getName().equalsIgnoreCase("title"))
                        {
                            if (insideItem)
                                headlines.add(xpp.nextText()); //extract the headline
                        }
                        else if (xpp.getName().equalsIgnoreCase("link"))
                        {
                            if (insideItem)
                                links.add(xpp.nextText()); //extract the link of article
                        }
                        else if (xpp.getName().equalsIgnoreCase("discription"))
                        {
                            if (insideItem)
                                discription.add(xpp.nextText()); //extract the discription of article
                        }
                    }
                    else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item"))
                    {
                        insideItem=false;
                    }
                    eventType = xpp.next(); //move to next element
                }

            }
            catch (MalformedURLException e)
            {
                e.printStackTrace();
            }
            catch (XmlPullParserException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        protected void onPostExecute(ArrayAdapter adapter)
        {
            adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, headlines);
            setListAdapter(adapter);
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id)
    {
        Uri uri = Uri.parse((links.get(position)).toString());
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    public InputStream getInputStream(URL url)
    {
        try
        {
            return url.openConnection().getInputStream();
        }
        catch (IOException e)
        {
            return null;
        }
    }
}

With that being my code and all the errors i get that i know of are

 Unable to start activity
 ComponentInfo{com.example.owner.jabexam/com.example.owner.jabexam.MainActivity}:
 java.lang.NullPointerException: Attempt to invoke virtual method
 'android.text.Editable android.widget.EditText.getText()' on a null
 object reference"

 "java.lang.NullPointerException: Attempt to invoke virtual method
 'android.text.Editable android.widget.EditText.getText()' on a null
 object reference"

I for the life of me can't figure it out. If anyone can help i'd appreciate it immensely.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
JBell
  • 25
  • 3

2 Answers2

1

1.) Initialize your mEditText before executing MyAsyncTask

2.) Create an XML file containing an edittext and ListView where ListView must have id as @android:id/list e.g added below.

3.) Call setContentView(R.layout.your_layout) after super.onCreate..

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    // set layout ^^^^
    mEditText = (EditText) findViewById(R.id.SearchTB);
    new MyAsyncTask().execute();
}

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
    android:id="@+id/SearchTB"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="anything"/>
<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/something"/>
</RelativeLayout>
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • Thanks, this mostly helped i still however get the same two errors. `java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.owner.jabexam/com.example.owner.ja‌​bexam.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference` @PavneetSingh – JBell Feb 01 '17 at 15:22
  • @JBell you need to set your layout using `setContentView` and make sure you have edit text and listview in it and `id` of listview should be `@android:id/list` – Pavneet_Singh Feb 01 '17 at 15:26
  • @JBell add the above xml under you `res/layout` and use `setContentView` with this layout – Pavneet_Singh Feb 01 '17 at 15:30
  • 1
    Thank you. It no longer crashes upon start up. Which is a relief now just to get my actual code to function and i'll be good to go. Thanks again. – JBell Feb 01 '17 at 15:39
  • @JBell i am glad that i could help , good luck for happy coding – Pavneet_Singh Feb 01 '17 at 15:42
0

you need to call setContentView(R.layout.your_layout) before doing findViewById(), call this method before and pass your layout's xml file that has editText with id SearchTB and listview with id @android:id/list.

Other observation: Switch positioning of these two lines from

new MyAsyncTask().execute();
mEditText = (EditText) findViewById(R.id.SearchTB);

to

mEditText = (EditText) findViewById(R.id.SearchTB);
new MyAsyncTask().execute();

Explanation You need to initialize objects before accessing them and in myAsyncTask you are accessing exittext so it needs to be initialized before.

Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • Thanks, this mostly helped i still however get the same two errors. " java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.owner.jabexam/com.example.owner.jabexam.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference" – JBell Feb 01 '17 at 15:19
  • @JBell I have updated my answer – Nayan Srivastava Feb 01 '17 at 15:24