0

I am trying to auto generate string values from array and add them to another array but i have a nullPointer exception.

protected static Random generator = new Random();

protected static String[] townNames = { "Paris", "Sofia", "Wiena", "Amsterdam" };
protected static String[] arr;

public static void main(String[] args) {

    for (int i = 0; i < towns.size(); i++) {
        arr[i] = townNames[generator.nextInt(townNames.length)];
    }

    for (String i : arr) {
        System.out.println(i);
    }

}

EDIT

I have edit the code and now it is working as i expected.

protected static Random generator = new Random();
protected static ArrayList<String> townNames = new ArrayList<>();

protected static ArrayList<String> generatedTowns = new ArrayList<String>();

public static void main(String[] args) {
    generatedArr(generatedTowns);
}

public static void generatedArr(ArrayList<String> a) {
    townNames.add("Paris");
    townNames.add("Sofia");
    townNames.add("Wiena");
    townNames.add("Amsterdam");
    townNames.add("Chikago");
    townNames.add("Vienna");
    townNames.add("Miami");
    townNames.add("Boston");
    townNames.add("New Orleans");
    for (int i = 0; i < 4; i++) {
        String value = townNames.get(generator.nextInt(townNames.size()));
        generatedTowns.add(value);
        townNames.remove(value);
    }

    for (String i : a) {
        System.out.println(i);
    }
}
5
  • Please have a look at: stackoverflow.com/help/mcve. You see, the problem is that if one wants to help you, then one first has to get your code running. Find all the missing imports, come up with a Town-class etc.... You should provide the code such that I can copy paste it and run it... like this one first has to put half an hour of work in it just to get to the point where you are now....... Also, you could tell us what you get and what you expect... Commented Nov 23, 2015 at 13:03
  • @dingalapadum i edited my post. Commented Nov 23, 2015 at 13:14
  • 1
    And still I can't compile it.... there is still stuff missing: class, imports... anyway: from a quick look I think the problem is that you never initialize arr Commented Nov 23, 2015 at 13:18
  • If you are going to show code, you should tag what language that the question is about. It helps others to find the question and it also affects the syntax coloring of code blocks, Commented Nov 23, 2015 at 13:33
  • I edited my answer and posted code which you should be able to copy-paste and run... Commented Nov 23, 2015 at 14:03

2 Answers 2

1
  • You have to create the array before to use it and add Random element with the same size as a townNames. Like that arr=new String[townNames.length];

You can try this code:

protected static Random generator = new Random();  
protected static String[] townNames = { "Paris", "Sofia", "Wiena", "Amsterdam" };
protected static String[] arr;

public static void main(String[] args) {
// We create an array with same size as a townNames 
arr=new String[townNames.length];

for (int i = 0; i < townNames.length; i++) {
arr[i] = townNames[generator.nextInt(townNames.length)];
}

for (String i : arr) {
System.out.println(i);
}
}
Sign up to request clarification or add additional context in comments.

3 Comments

Does your answer add something I did not mention in mine? Just trying to understand why you felt that you have to point out the exact same thing I already did half an hour ago... because you do do townNames.length instead of 4 or what? To me it feels a bit like you are hijacking what I told OP in the comments and in my own answer...
@dingalapadum Because I did my answer before I watched your answer, But I can't see what's the problem
It's not a real problem. It's just as you say: "you did not even bother to see what the other answer was saying before posting your own". I prefer things being DRY: en.wikipedia.org/wiki/Don%27t_repeat_yourself Clearly, for you it was not about improving the quality of SO but rather getting your answer out there... anyway, nevermind.
1

There is several stuff wrong with your code, there might be more, depending on what's "around there" and you are not showing us. Anyway, some of the problems I can spot right away are:

  • Where is towns? do you mean townNames? Does your code even compile like this?
  • In java you there is no size for arrays, but rather length
  • You never initialize the array arr. (This is probably where your NullPointer-Exception comes from.

Try something along the line.

arr = new String[4];

This is your code which I edited until I got it running:

In a file called myApp.java:

import java.util.Random;

public class myApp{

    protected static Random generator = new Random();

    protected static String[] townNames = { "Paris", "Sofia", "Wiena", "Amsterdam" };
    protected static String[] arr;

    public static void main(String[] args) {

        arr = new String[townNames.length];

        for (int i = 0; i < townNames.length; i++) {
             arr[i] = townNames[generator.nextInt(townNames.length)];
        }

        for (String i : arr) {
            System.out.println(i);
        }

    }

}

Hopefully you find everything what you need in there. I suggest you diff this against your code.

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.