简体   繁体   中英

java.lang.NullPointerException with Android Java

Apparently the problem is with line 62.If you comment that out then line 63 and so on. I also tried a non array and it worked.

I cant see that any of the variables are nulled either. Logcat says the problem is: java.lang.NullPointerException .

Here is my code so far:

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++;
    }

Thanks in advance! I owe alot.

You declared but didn't initialize your playerStatistics array, so it's null . Try declaring it as:

int playerStatistics[] = new int[7];

or one more than your largest index possible.

You will need to do something similar with your other arrays, which also don't appear to be initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM