1

I have a code like this:

Element name = doc.createElement("firstname");
name.appendChild(doc.createTextNode(lastname));
extension.appendChild(name);

Now, if I want to make multiple Element names, how do I do then? I tried to simply make "name" into an array but it didnt work:

Element name[] = null;
name[0] = doc.createElement("firstname");
name[0].appendChild(doc.createTextNode(lastname));
extension.appendChild(name[0]);

any suggestions?

6
  • 7
    That looks a lot like JavaScript. JavaScript is not Java. Java is to JavaScript like Car is to Carpet. Commented Jun 11, 2013 at 10:00
  • Intialize Element name[] Commented Jun 11, 2013 at 10:00
  • @BenjaminGruenbaum It look like perfectly valid Java to me. What makes you think it is JavaScript? Commented Jun 11, 2013 at 10:04
  • 2
    @Buurman The usage of the DOM API createElement , createTextNode , appendChild Commented Jun 11, 2013 at 10:05
  • I do work in JAVA, not JavaScript -_- Commented Jun 11, 2013 at 10:42

2 Answers 2

2
Element name[] = new Element[10]; //or any size

name[] is a reference to an object (Element[] is an object as well). If you set the reference to null you can't dereference it with name[0]=.

Keep in mind that arrays are fixed-size. You might want to look into ArrayList or Vector if you'd like a variable-size datastructure.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice, I get it now :) Thx for the reply
1

try this

Element name[] = new Element[expected_element_number];
name[0] = doc.createElement("firstname");
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.