Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In my application i got string values dynamically. I want to assign these values to string array then print those values.But it shows an error(Null pointer exception) EX:

String[] content = null;
for (int s = 0; s < lst.getLength(); s++) {
    String st1 = null;
    org.w3c.dom.Node nd = lst.item(s);
    if (nd.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
        NamedNodeMap nnm = nd.getAttributes();

        for (int i = 0; i < 1; i++) {
            st1 = ((org.w3c.dom.Node) nnm.item(i)).getNodeValue().toString();
        }
    }

    content[s] = st1;
    //HERE it shows null pointer Exception.
}  

Thanks

share|improve this question
    
Your for loop (int i = 0; i < 1; i++) is telling it to do this loop once. That is a bit unusual. You could delete the loop and leave the contents inside and achieve the same results. – Chad Bingham Sep 29 '13 at 18:52

This is because your string array is null. String[] content=null;

You declare your array as null and then try to assign values in it and that's why it is showing NPE.

You can try giving initial size to your string array or better to use ArrayList<String>. ie:

String[] content = new String[10]; //--- You must know the size or array out of bound will be thrown.

Better if you use arrayList like

List<String> content = new ArrayList<String>(); //-- no need worry about size.

For list use add(value) method to add new values in list and use foreach loop to print the content of list.

share|improve this answer
    
thank you it's usefull for me – user735855 May 19 '11 at 6:16

Use ArrayList or Vector for creating collection (or array) of strings in a dynamic fashion.

List<String> contents = new ArrayList<String>();
Node node = (org.w3c.dom.Node) nnm.item(i)).getNodeValue();
if (null != node)
    contents.add(node.toString());

Outside the loop you can do as follows

for(String content : contents) {
     System.out.println(content) // since you wanted to print them out
share|improve this answer

It's a little hard to understand what you're after because your example got munged. However, your String array is null. You need to initialize it, not just declare it. Have you considered using an ArrayList instead? Arrays in java are fixed length (unless they changed this since my university days).

ArrayList is a lot simpler to work with.

E.g.:

List<String> content = new ArrayList<String>();
for (int i = 0; i < limit; i++){
   String toAdd;
   //do some stuff to get a value into toAdd
   content.add(toAdd)
}

There's also something weird with one of your for loops.

for(int i=0;i<1;i++)

The above will only ever iterate once. To clarify:

for(int i=0;i<1;i++){
   System.out.println("hello");
}

is functionally identical to:

System.out.println("hello");

They both print out "hello" once, adn that's it.

share|improve this answer

Use

 content[s] = new String(st1);

Now it creates new instance for that particular array index.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.