简体   繁体   English

从XML文档获取属性文件中的java.lang.nullpointerException

[英]java.lang.nullpointerexception in getting attribute file from XML document

public class ResultsTitre extends ActionBarActivity {

org.w3c.dom.Document doc = null;
EditText resultActors;
EditText resultAnnee;
EditText resultGenre;
EditText resultPlot;
EditText resultRating;
EditText resultReleased;

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

    resultActors=(EditText)findViewById(R.id.resultActors);
    resultActors.setEnabled(false);

    resultAnnee=(EditText)findViewById(R.id.resultAnnee);
    resultAnnee.setEnabled(false);

    resultGenre=(EditText)findViewById(R.id.resultGenre);
    resultGenre.setEnabled(false);

    resultPlot=(EditText)findViewById(R.id.resultPlot);
    resultPlot.setEnabled(false);

    resultRating=(EditText)findViewById(R.id.resultRating);
    resultRating.setEnabled(false);

    resultReleased=(EditText)findViewById(R.id.resultReleased);
    resultReleased.setEnabled(false);


    new xmlFromHttpAsyncTask().execute("http://www.omdbapi.com/?t="+ titleTyped +"&r=xml");

}

private class xmlFromHttpAsyncTask extends AsyncTask<String, Void, Void> {


    protected Void doInBackground(String... query) {
        URL url;
        try {
            // La on crÈe la requete HTTP
            url = new URL(query[0]);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();

            // Et on rÈcupËre la rÈponse de OMDBAPI
            doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();


            //Traitement des exceptions au cas o˘ ca plante, on s'en fout
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Void n) {
        createDisplay();
    }

}

    private void createDisplay(){

        try {


            NodeList nodeList = doc.getElementsByTagName("root");

            Element rootElement = (Element) nodeList.item(0);

            NodeList movieList = rootElement.getElementsByTagName("Movie");

            Element e = (Element)movieList.item(0);



                    resultActors.setText(e.getAttribute("actors"));
                    resultAnnee.setText(e.getAttribute("year"));
                    resultGenre.setText(e.getAttribute("genre"));
                    resultPlot.setText(e.getAttribute("plot"));
                    resultRating.setText(e.getAttribute("imdbRating"));
                    resultReleased.setText(e.getAttribute("released"));
                    e.getAttribute("imdbID");


        }
        catch (Exception ex){
            resultReleased.setText("ERROR : " + ex);
        }
    }
}

When I compile this code I get "java.lang.nullpointerexception" in my last catch ! 当我编译此代码时,我在上一个捕获中得到“ java.lang.nullpointerexception”! I don't understand why ! 我不明白为什么! Can someone help please ? 有人可以帮忙吗?

My XML file looks like this 我的XML文件如下所示

<root response="True">
<movie title="Icarus" year="2010" rated="TV-14" released="10 Dec 2010" runtime="42 min" 
genre="Adventure, Drama, Romance" director="Mairzee Almas" 
writer="Jerry Siegel (character created by: Superman), Joe Shuster (character created by: Superman), Alfred Gough (developed for television by), Miles Millar (developed for television by), Genevieve Sparling"
actors="Tom Welling, Erica Durance, Cassidy Freeman, Justin Hartley" 
plot="As the VRA threat intensifies, Clark takes initiative by closing down watchtower and declaring the League go officially underground, but will this be enough to stop trotter and Slade Wilson..." 
language="English" country="USA" awards="N/A" 
poster="http://ia.media-imdb.com/images/M/MV5BMTQ0MTg0MTc0Ml5BMl5BanBnXkFtZTgwNjk5ODEyMjE@._V1_SX300.jpg" 
metascore="N/A" imdbRating="8.6" imdbVotes="393" imdbID="tt1628582" type="episode"/>
</root>

DOM是区分大小写的,因此对rootElement.getElementsByTagName("Movie")的调用始终为null,因为xml包含小写的“电影”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM