java - 无法从数组中获取实际值,它只是显示 "NULL"

标签 java arrays null

当我尝试从数组 VoteListVVD 中获取值时,它显示“NULL”,但我认为其中应该有一些东西,而不是 NULL。 Package Election中的Line72给出了问题! (只是为了便于概览,我将其他方排除在外)

出了什么问题?

package Elections;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Voting
{
    Scanner scanner = new Scanner(System.in);   

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

    public Stemmen()
    {
        System.out.println("Casting 50 random votes (Y/N)?");
        System.out.println();

        String vote = scanner.nextLine();   

        switch (vote) 
        {
            case ("Y"):
            {
                int candidate=0;

                for (int i=1; i<=50; i++)
                {
                    int voteparty = ThreadLocalRandom.current().nextInt(0,3);

                    switch (voteparty)
                    {
                    case (0):
                        candidate = ThreadLocalRandom.current().nextInt(0,5);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty]candidate]+1;

                    break;
                    case (1):   
                        candidate = ThreadLocalRandom.current().nextInt(0,4);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;

                    break;
                    case (2):   
                        candidate = ThreadLocalRandom.current().nextInt(0,6);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;
                    }
                }
            }                   
        }

        for (int j=0; j<6; j++)
        {
LINE72  System.out.format(VoteList.VoteListVVD[j][0]+"\n");  \\This results in NULL?????
        System.out.println(ReSult.result[2][j]+" ");         \\This works!!! (no question about this)
        }
    }   
}

package Elections;

public class Votelist
{
    static String[][] VoteListVVD  = new String [6][1];

    public VoteList()
    {
        VoteListVVD[0][0] = "Lubbers";
        VoteListVVD[1][0] = "Kok";
        VoteListVVD[2][0] = "Hans";
        VoteListVVD[3][0] = "Paula";
        VoteListVVD[4][0] = "Sientje";
        VoteListVVD[5][0] = "Martie";
    }
}

最佳答案

我认为问题在于您的 Stemmen 类中还没有 Votelist。在从中获取 VoteListVVD 之前,尝试执行 Votelist votelist = new Votelist(); 创建一个投票列表。

package Elections;

import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;

public class Voting
{
    Scanner scanner = new Scanner(System.in);   

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

    public Stemmen()
    {
        System.out.println("Casting 50 random votes (Y/N)?");
        System.out.println();

        String vote = scanner.nextLine();   
        Votelist votelist = new Votelist();

        switch (vote) 
        {
            case ("Y"):
            {
                int candidate=0;

                for (int i=1; i<=50; i++)
                {
                    int voteparty = ThreadLocalRandom.current().nextInt(0,3);

                    switch (voteparty)
                    {
                    case (0):
                        candidate = ThreadLocalRandom.current().nextInt(0,5);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty]candidate]+1;

                    break;
                    case (1):   
                        candidate = ThreadLocalRandom.current().nextInt(0,4);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;

                    break;
                    case (2):   
                        candidate = ThreadLocalRandom.current().nextInt(0,6);
                        ReSult.result[voteparty[candidat] = ReSult.result[voteparty][candidate]+1;
                    }
                }
            }                   
        }

        for (int j=0; j<6; j++)
        {
        System.out.format(votelist.VoteListVVD[j][0]+"\n");  
        System.out.println(ReSult.result[2][j]+" "); 
        }
    }   
}

关于java - 无法从数组中获取实际值,它只是显示 "NULL",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58973124/

相关文章:

java - 结束时打印第一个数组元素

MySQL:JOIN 查询获取所有具有可为空字段的项目

javascript - React 中的 getElementById

c++ - 为什么我不需要检查引用是否无效/空?

java - 在列表中的项目之间添加项目的最简单方法

java - Android:ArrayList旧条目被新条目覆盖

java - 如何在 Java、Android 上对我的 Web 服务进行 POST 查询?

java - 如何防止LinkedHashMap替换Key/Value?

PHP 数组搜索返回多个键

java - 队列圆形数组循环