0

次の 2 行でエラー ヌル ポインターを取得し続けていますが、それを修正する方法がわかりません。

NodeList textList = NameElement.getChildNodes();
String name = ((Node)textList.item(0)).getNodeValue().trim();




   import java.io.FileInputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;

public class PlayActivity extends Activity {
    private Spinner spntreasure;
    private static final String FILENAME = "data.xml";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_activity);
        ImageButton cancelBtn =  (ImageButton)findViewById(R.id.cancelBtn1);

        addItemsOnspnTreasure();

        cancelBtn.setOnClickListener(new View.OnClickListener() 
        {           
            @Override
            public void onClick(View v) 
            {
                finish();
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.play_activity, menu);
        return true;
    }

    // add items into spinner dynamically
      public void addItemsOnspnTreasure() 
      {

          ArrayList<String> List = new ArrayList<String>();

          spntreasure = (Spinner) findViewById(R.id.spntreasure);

          try {
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                FileInputStream fis = openFileInput(FILENAME);
                Document doc = docBuilder.parse(fis);
                fis.close();

                // normalize text representation
                doc.getDocumentElement ().normalize ();
                String myRoot = doc.getDocumentElement().getNodeName();

                NodeList listOfTreas = doc.getElementsByTagName("Treasure");
                int totalTreas = listOfTreas.getLength();
                int myTotalTreasure = totalTreas;

                for(int s=0; s<listOfTreas.getLength() ; s++){

                    Node firstTreasNode = listOfTreas.item(s);
                    if(firstTreasNode.getNodeType() == Node.ELEMENT_NODE)
                    {

                        Element firstTreasElement = (Element)firstTreasNode;

                        //-------
                        NodeList NameList = firstTreasElement.getElementsByTagName("Name");
                        Element NameElement = (Element)NameList.item(0);

                        NodeList textList = NameElement.getChildNodes();
                        String name = ((Node)textList.item(0)).getNodeValue().trim();

                        //-------
                        NodeList Clue1List = firstTreasElement.getElementsByTagName("Clue1");
                        Element Clue1Element = (Element)Clue1List.item(0);

                        NodeList textLNList = Clue1Element.getChildNodes();
                        String clue1 =((Node)textLNList.item(0)).getNodeValue().trim();

                        //----
                        NodeList Clue2List = firstTreasElement.getElementsByTagName("Clue2");
                        Element Clue2Element = (Element)Clue2List.item(0);

                        NodeList textClue2List = Clue2Element.getChildNodes();
                        String clue2 = ((Node)textClue2List.item(0)).getNodeValue().trim();

                        //------

                    }//end of if clause

                }//end of for loop with s var

          }catch (SAXParseException err) {
                System.out.println ("** Parsing error" + ", line " 
                     + err.getLineNumber () + ", uri " + err.getSystemId ());
                System.out.println(" " + err.getMessage ());

                }catch (SAXException e) {
                Exception x = e.getException ();
                ((x == null) ? e : x).printStackTrace ();

                }catch (Throwable t) {
                t.printStackTrace ();
                }


        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, List);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spntreasure.setAdapter(dataAdapter);
      }

}
4

2 に答える 2

1

XML に NameElement の子ノードがありません。NameElement.getChildNodes() にアクセスしようとすると、NULL が返され、textList.item(0) によって NullPointerException が発生します。

今のところ、この質問が StackOverflow にあるといいのですが、担当者を得ることができます。<_<

(この回答は、質問者が xml コードを投稿した G+ 投稿からの続きです)。

代わりに 88 行目でこれを試して、それが機能するかどうかを確認してから、Clue ノードに対して同じことを試してください。

String name = ((Element) NameList.item(0)).getAttribute("Name");

このドキュメントによると: http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html

Node が Element にキャストされると、明らかなマッピングがないため nodeValue が null になるか、Element の nodeValue が常に null (?) であると思われるため、そのドキュメントが言おうとしています。

于 2013-02-28T21:10:46.420 に答える
0

NodeList を Element にキャストしています:

NodeList fstNode = doc.getElementsByTagName("Name");
Element fstElmnt = (Element) fstNode;

これは間違っています。試す:

......
NodeList fstNode = doc.getElementsByTagName("Name");
int size = fstNode.getLength();
for(int i=0; i< size; i++){
   Node curr= nodeList.item(i);
   if (curr.getNodeType() == Node.ELEMENT_NODE) {
       Element elem = (Element) curr;
       .... 
   }
   ......
}
.......
于 2013-02-27T21:29:04.440 に答える