gpt4 book ai didi

Android Java 中的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:20:40 25 4
gpt4 key购买 nike

显然问题出在第 62 行。如果您将其注释掉,则第 63 行等等。我还尝试了非数组并且它有效。

我也看不到任何变量都为空。Logcat 说问题是:java.lang.NullPointerException

这是迄今为止我的代码:

package com.zxz.zxcdnd;

import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;

public class MainActivity extends Activity
{

//Player statistics
int playerStatistics[];

//Room statistics
String roomStatistics[];
int type = 1;

//Player inventory
int inventorySlot = 1;
int playerInventoryNumbers[];
String playerInventoryNames[];
String playerInventoryAffectedStats[];

//Index representation variables
int name = 1,roomsConquered = 2,goldCoins = 3,stamina = 4,damage = 5,luck = 6,difficulty=3;

//Random generator
Random r = new Random();
int dice;

//List of available items
int itemNumbers[] = {1,1,2,2,3,3,4,4,5,5};

String itemAffectedStats[] = {"stamina","damage",
"luck","damage",
"stamina","damage",
"damage","luck",
"damage","stamina"};

String itemNames[] = {"lemon","app",
"ist","Bms",
"Hateech","Lasacsyrup",
"Picagger","Hompa",
"Bori","Offipenser"};

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Initialize important player variables
startUp();

//Create room
initializeRoom();
}

void startUp()
{
//Initialize stamina, damage and luck
playerStatistics[stamina] = r.nextInt(7-1) + 1; //LINE 62
playerStatistics[damage] = r.nextInt(7-1) + 1; //LINE 63
playerStatistics[luck] = r.nextInt(7-1) + 1;

//Initialize player inventory
playerInventoryNumbers[inventorySlot] = itemNumbers[r.nextInt(11-1) + 1];
playerInventoryNames[inventorySlot] = itemNames[r.nextInt(11-1) + 1];
playerInventoryAffectedStats[inventorySlot] = itemAffectedStats[r.nextInt(11-1) + 1];

playerStatistics[damage] = playerStatistics[damage] + playerInventoryNumbers[inventorySlot];
inventorySlot++;
}

提前致谢!我欠了很多。

最佳答案

您已声明但未初始化 playerStatistics 数组,因此它为 null。尝试将其声明为:

int playerStatistics[] = new int[7];

或者比您可能的最大索引多一个。

您将需要对其他数组执行类似的操作,这些数组似乎也未初始化。

关于Android Java 中的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21056179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com