0

I want to change the variable name with each iteration. Since the number of nodes created is dynamically changing.

I tried using one dimensional array but its returning a null pointer. My code is as follow

    GenericTreeNode<String> **root1[]** = null;
    for(int i=0;i<10;i++)
    {
        String str="child"+i;
        System.out.println(str);

        **root1[i]** =new GenericTreeNode<String>(str);
    }

I am using already built datastructure

    public class GenericTree<T> {

private GenericTreeNode<T> root;

public GenericTree() {
    super();
}

public GenericTreeNode<T> getRoot() {
    return this.root;
}

public void setRoot(GenericTreeNode<T> root) {
    this.root = root;
}

Is there some other way in java or JSP to change the variable name dynamically inside the loop.

3
  • 2
    I have never seen a time when variable name changing is a good idea, nor do I think it possible, let alone in Java. Commented Feb 3, 2011 at 16:24
  • Of course it's a null pointer! What do you think root1[] = null; does? Anyway, besides being impossible, "change the variable name with each iteration" makes no sense. You should tell us what you're trying to do. Commented Feb 3, 2011 at 16:27
  • You can't do it. What's your real problem? Commented Feb 3, 2011 at 16:28

5 Answers 5

3
GenericTreeNode<String> root1[] = null;

This line is equivalent to this one:

GenericTreeNode<String>[] root1 = null;

so you create an array variable and initialize it to null

root1[i] =new GenericTreeNode<String>(str);

but here you assign a value to the array's index.

This must throw a NullPointerException!!.

Here's how to do it:

GenericTreeNode<String>[] root1 = new GenericTreeNode<String>[10];
Sign up to request clarification or add additional context in comments.

Comments

2

No, you can't change variable names in Java.

You got a NullPointerException when using an array because you tried to put a value in the array, and the array was null. You have to initialize the array, with the right number of elements :

int length = 10;
GenericTreeNode<String>[] root1 = new GenericTreeNode<String>[length];
for (int i = 0; i < length; i++) {
    String str = "child" + i;
    System.out.println(str);

    root1[i] = new GenericTreeNode<String>(str);
}

Comments

2

You probably mean to do this:

GenericTreeNode<String> root1[] = new GenericTreeNode<String>[10];
for(int i=0;i<10;i++)
{
    String str="child"+i;
    System.out.println(str);

    root1[i] = new GenericTreeNode<String>(str);
}

There's no need to "change a variable name".

Comments

1

No, a variable name can't be changed. Try another method like a 2-dimensional array to create another "variable" as you're iterating.

Comments

1

I not able to initiate GenericTree as array. Later I used just vector to solve the problem.

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.