如何将对象放入数组中

时间:2016-01-21 05:52:34

标签: java

如何将对象放入我的数组?这是我的阵容。

public static Player [] playerArray;
Player[] playerArray = new Player [2];

播放器

public class Player {
    private String wpm;
    private String mistakes;
    private String time;

    public Player (String nwpm,String nmistakes, String ntime){
        wpm = nwpm;
        mistakes = nmistakes;
        time = ntime;
    }   

    public String getWPM(){
        return wpm;
    }
    public String getMistakes(){
        return mistakes;
    }
    public String getTime(){
        return time;
    }
}

我一直收到此错误

  

线程中的异常" main"显示java.lang.NullPointerException

每当我尝试使用播放器时。我做错什么了吗?你需要什么吗?

编辑:添加错误发生的位置

public void setPlayer1(Player p){
    p1WPM.setText("8");
    p1Mis.setText(p.getMistakes());
    p1Time.setText(p.getTime());
}

6 个答案:

答案 0 :(得分:2)

例如,要在数组中添加Player对象,您可以执行以下操作:

Player[] playerArray = new Player [2];

//Create new Player object using the defined constructor
p = new Player("a","b","c");

// Assign p to 1st index of array
playerArray[0] = p;

答案 1 :(得分:2)

您已声明playerArray两次错误。此外,您尚未显示获取NullPointerException的代码。

如果您是新手,请尝试以下代码:

Player[] playerArray = new Player [2]; //declare array of size 2
Player p1 = new Player("nwpm","nmistakes","ntime"); // first player object
Player p2 = new Player("nwpm","nmistakes","ntime"); // second player object

playerArray[0] = p1; // first player added in the array
playerArray[1] = p2; // second player added in the array

答案 2 :(得分:0)

Player[] playerArray = new Player [2];正在创建数组,而不是对象。 你应该添加

 playerArray[0] = new Player(param1, param2, param3);
 playerArray[1] = new Player(param1, param2, param3);

让所有项目都有自己的对象

答案 3 :(得分:0)

试试这个:

  Player a = new Player("a","b","c");
            Player[] playerArray = new Player [2];
            playerArray = new Player[2];
            playerArray[0]=a;
            System.out.println(playerArray[0].getTime()+" "+playerArray[0].getMistakes()+" "+playerArray[0].getWPM());

结果: c b a

答案 4 :(得分:0)

  

线程“main”中的异常java.lang.NullPointerException

Player[] playerArray = new Player [2];

使用此语句,您只需创建一个大小为2的数组,其所有元素均为null。但你不是在这里创造元素。

您必须实例化每个元素。

for(int i=0; i< playerArray.length;i++)
    playerArray[i] = new Player();//with appropriate constructor

如果您尝试在playerArray playerArray[0].mistakes上执行任何操作,请获取NullPointerException。您应该使用Player对象初始化数组中的每个元素。

答案 5 :(得分:0)

这是适合您的工作代码。

<script>


$( "#becomesignup" ).submit(function( event ) {
        $.ajax({
            url : 'login', // Your Servlet mapping or JSP(not suggested)
            data : {"email":$("#becomeemail").val()},
            type : 'GET',
            dataType : 'html', // Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.
            success : function(response) {
                if(response == "true"){
                    $('#emailerrorbecome').slideDown();
                    $('#become-submit').prop('disabled', true);
                  event.preventDefault();
                }else{
                    $('#emailerrorbecome').slideUp();
                    $('#become-submit').prop('disabled', false);
                }
                 $('.black-screen').hide();

              },
              error : function(request, textStatus, errorThrown) {
                alert(errorThrown);

            }
        });
});
</script>