JAVA NullPointerException error (School project) : <pre><code>import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

/**
 * Created by Tim on 17-8-2016.
 */
public class ReadRss extends AsyncTask&lt;Void, Void, Void&gt; {

    Context context;

    //The URL where the app needs to get the information from
    String address = "http://www.sciencemag.org/rss/news_current.xml";
    ProgressDialog progressDialog;
    ArrayList&lt;FeedItem&gt;feedItems;
    RecyclerView recyclerView;
    URL url;

    //The loading (Loading feed...) message
    public ReadRss(Context context, RecyclerView recyclerView){
        this.recyclerView = recyclerView;
        this.context = context;
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Loading feed...");
    }
    @Override

    //Shows the user that the page is loading (Loading feed...)
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressDialog.dismiss();
        MyAdapter adapter = new MyAdapter(context, feedItems);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new VerticalSpace(50));
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        processXml(Getdata());

        return null;
    }

    //!!!!!This part ensures all the data is collected from the feed and it is made ready
    //so it can be used by recycler view
    private void processXml(Document data) {
        if (data!=null) {
            //Add all feedItems to an ArrayList
            feedItems = new ArrayList&lt;&gt;();
            //Object that will store root elements
            Element root = data.getDocumentElement();
            //Node object that will show channel
            Node channel = root.getChildNodes().item(1);
            //Store all child of channel element
            NodeList items = channel.getChildNodes();
            //Loop trough each child element of items
            for (int i=0; i&lt;items.getLength(); i++){
                Node currentchild = items.item(i);
                //Check that node is item node by using an if statement
                if (currentchild.getNodeName().equalsIgnoreCase("item")){
                    FeedItem item = new FeedItem();
                    //Store childs in NodeList of current item
                    NodeList itemchilds = currentchild.getChildNodes();
                    //For loop to loop through all childs of item tag
                    for (int j=0; j&lt;itemchilds.getLength(); j++){
                        //Store current node
                        Node current = itemchilds.item(j);
                        //Check node is title, description, pubDate, link or thumbnail node by if else conditions
                        if (current.getNodeName().equalsIgnoreCase("title")){
                            //If node is title, description, pubDate, link or thumbnail then set textcontent of our current node
                            item.setTitle(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("description")){
                            item.setDescription(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("pubDate")){
                            item.setPubDate(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("link")){
                            item.setLink(current.getTextContent());
                        }else if (current.getNodeName().equalsIgnoreCase("media:thumbnail")){
                            //This will return a thumbnail url
                            String url = current.getAttributes().item(0).getTextContent();
                            item.setThumbnailUrl(url);
                        }
                    }
                    //feedItems to be added to the ArrayList&lt;FeedItem&gt;
                    feedItems.add(item);

                }
            }
        }
    }

    public Document Getdata() {
        try {
            url = new URL(address);
            //Open connection by using Http url connection object
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //Sets request method as GET
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = builderFactory.newDocumentBuilder();
            Document xmlDoc = builder.parse(inputStream);
            return xmlDoc;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
</code></pre>

<p>When adding this code for a RSS Reader to our final project (for school) we are getting this error:</p>

<pre><code>08-21 16:53:27.583 2734-2734/com.example.codru.stendensocial E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.codru.stendensocial, PID: 2734
                                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                                   at com.example.codru.stendensocial.ReadRss.onPostExecute(ReadRss.java:61)
                                                                                   at com.example.codru.stendensocial.ReadRss.onPostExecute(ReadRss.java:28)
                                                                                   at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                   at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                                   at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
</code></pre>

<p>We don't know how to fix this. We read something about adding final in front of setLayoutManager, but could not figure out how to do this.</p>

<p>Any help would be highly appreciated!</p>

<p>~ Tim</p>
