2017-07-25 4 views
-4

Also, wenn ich diese verwenden, um einen String von zufälligen Ereignissen holen es ein Null zurückgibt:Java Array liefert Null

public static List<String> pickNRandom(List<String> lst, int n) { 
    List<String> copy = new LinkedList<String>(lst); 
    Collections.shuffle(copy); 
    return copy.subList(0, n); 
} 

static List<String> randomP; 
public List<String> items(){ 
    List<String> teamList = new LinkedList<String>(); 
    teamList.add("team1"); 
    teamList.add("team2"); 
    teamList.add("team3"); 
    teamList.add("team4"); 
    teamList.add("team5"); 
    teamList.add("team6"); 
    List<String> randomPicks = pickNRandom(teamList, 3); 
    randomPicks = randomP; 
    return randomPicks; 
} 

public static void Store() { 
    Random rand = new Random(); 
    int people = rand.nextInt(50) + 1; 
    List<String> itemsIn = randomP; 
    System.out.println("People in store: "+people + "\nItems in store: "+itemsIn); 
} 

public static void main(String[] args) { 
    Store(); 

} 

Warum es null zurückkehrt und was kann ich tun, um dieses Problem beheben?

+0

Ja, mir ist gar nicht klar, was Sie hier machen wollen! –

+0

Warum weisen Sie 'randomPicks = randomP;' zu, bevor Sie 'randomPicks' zurückgeben? – 4castle

+0

items() Methode wird nicht einmal aufgerufen, npe passiert bei sysout – Zeromus

Antwort

1

In dieser Zeile List<String> itemsIn = randomP; weisen Sie eine Liste zu, die nicht initialisiert wurde. Der Standardwert ist null. Ich denke, so sollte Ihre Zeile aussehen: List<String> itemsIn = items(); Denken Sie daran, die Methode items() zu statisch zu ändern.

+0

Danke das macht viel mehr Sinn. –

0

Sie haben List<String> randomP an keiner Stelle initiiert.

0

Es gibt keine Null zurück. Sie überschreiben sofort, was die Funktion mit einem nicht initialisierten Variable liefert hier:

randomPicks = randomP; 

Es ist nicht klar, warum Sie, dass ... aber tun das nicht.

0

randomP wird deklariert, aber nicht initialisiert, wird items() nie genannt wird, gleich wie pickNRandom

so können Sie die Liste initialisiert werden (aber das bedeutet nicht, dass es bevölkert wird ...)

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

Sie eine leere Liste anstelle von null

Menschen in Speicher erhalten: 12

Angebote im Shop: []

1

Ausgabe ist klar, aber nicht in Frage (es verschiedene Lösungen haben) ... Ich habe kleine Änderungen hinzugefügt.

public static List<String> pickNRandom(List<String> lst, int n) { 
    List<String> copy = new LinkedList<String>(lst); 
    Collections.shuffle(copy); 
    return copy.subList(0, n); 
} 

public static List<String> items(){ 
    List<String> teamList = new LinkedList<String>(); 
    teamList.add("team1"); 
    teamList.add("team2"); 
    teamList.add("team3"); 
    teamList.add("team4"); 
    teamList.add("team5"); 
    teamList.add("team6"); 
    return pickNRandom(teamList, 3); 
} 

public static void Store() { 
    Random rand = new Random(); 
    int people = rand.nextInt(50) + 1; 
    List<String> itemsIn = items(); 
    System.out.println("People in store: "+people + "\nItems in store: "+itemsIn); 
} 

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

Menschen im Speicher: 10

Angebote im Shop: [Team6, team2, team3]


In Ihrem Code randomP nutzlos war (nie nie initialisiert, gefüllt in)

Verwandte Themen