2014-12-05 53 views
0
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); 
     } 
    } 
} 

当我编译这段代码时,我在最后一个catch中得到了“java.lang.nullpointerexception”!我不明白为什么!有人可以帮忙吗?java.lang.nullpointerexception从XML文档中获取属性文件

我的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/[email protected]_V1_SX300.jpg" 
metascore="N/A" imdbRating="8.6" imdbVotes="393" imdbID="tt1628582" type="episode"/> 
</root> 
+0

当你执行你的程序时,你的意思是吧?编译代码时,您不会遇到异常... – 2014-12-05 15:52:38

+1

而且,您能告诉我们应该有NullPointer的LINE吗? – 2014-12-05 15:54:04

+0

createDisplay有很多潜在的空值。 doc可以为null,nodeList可以为null,rootElement可以为null,movieList可以为null,或者e可以为null,如果您没有在布局中引用有效的ID,那么这些TextView中的任何一个都可以为null。没有行号,很难说。 – zgc7009 2014-12-05 15:59:08

回答

0

的DOM是大小写敏感的,因此呼叫rootElement.getElementsByTagName("Movie")将始终返回null,因为XML包含“电影”用小写。

+0

我应该怎么做才能解决这个问题? – mch25 2014-12-06 11:34:19