当前位置: 动力学知识库 > 问答 > 编程问答 >

Android NullPointerException causing crash

问题描述:

I am creating an android card game. I have created a playing card class and a card deck class. When i run sample code it fails to build the deck. Here is my code along with the logcat

PlayingCard.java

package com.michaelpeerman.hi_low;

public class PlayingCard {

public String[] CardSuit = {"SPADE","HEART","CLUB","DIAMOND"};

public String[] CardRank= {"ACE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE","JACK","QUEEN","KING"};

public String theSuit;

public int theRank;

public boolean theState;

public PlayingCard(){

theSuit = "SPADE";

theRank = 1;

theState = false;

}

public PlayingCard(String mySuit, int myRank){

theSuit = mySuit;

theRank = myRank;

}

public int getRank(){

return theRank;

}

public String getSuit(){

return theSuit;

}

public boolean getState(){

return theState;

}

public void setState(boolean myState){

theState = myState;

}

public void setRank(int myRank){

theRank = myRank;

}

public void setSuit(String mySuit){

theSuit = mySuit;

}

public String getCardString(){

String cardString = null;

String theCardRanks[] = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};

if (theSuit == "CLUB")

cardString = "C"+theCardRanks[theRank-1];

else if (theSuit == "SPADE")

cardString = "S"+theCardRanks[theRank-1];

else if (theSuit == "HEART")

cardString = "H"+theCardRanks[theRank-1];

else if (theSuit == "DIAMOND")

cardString = "D"+theCardRanks[theRank-1];

return cardString;

}

}

CardDeck.java

package com.michaelpeerman.hi_low;

import java.util.Random;

import com.michaelpeerman.hi_low.PlayingCard;

public class CardDeck {

public PlayingCard[] cardArray;

public int num_decks;

public int cards_left;

public CardDeck(int my_decks)

{

num_decks = my_decks;

cardArray = new PlayingCard[52*num_decks];

cards_left = 52*num_decks;

for (int i=0; i< num_decks; i++){

for (int j = 0; j < 13; j++)

{

cardArray[(j+(52*i))].setSuit("SPADE");

cardArray[j+13+(52*i)].setSuit("HEART");

cardArray[j+26+(52*i)].setSuit("CLUB");

cardArray[j+39+(52*i)].setSuit("DIAMOND");

if (j==0){

cardArray[j+(52*i)].setRank(1);

cardArray[j+13+(52*i)].setRank(1);

cardArray[j+26+(52*i)].setRank(1);

cardArray[j+39+(52*i)].setRank(1);

}

else if (j==1){

cardArray[j+(52*i)].setRank(2);

cardArray[j+13+(52*i)].setRank(2);

cardArray[j+26+(52*i)].setRank(2);

cardArray[j+39+(52*i)].setRank(2);

}

else if (j==2){

cardArray[j+(52*i)].setRank(3);

cardArray[j+13+(52*i)].setRank(3);

cardArray[j+26+(52*i)].setRank(3);

cardArray[j+39+(52*i)].setRank(3);

}

else if (j==3){

cardArray[j+(52*i)].setRank(4);

cardArray[j+13+(52*i)].setRank(4);

cardArray[j+26+(52*i)].setRank(4);

cardArray[j+39+(52*i)].setRank(4);

}

else if (j==4){

cardArray[j+(52*i)].setRank(5);

cardArray[j+13+(52*i)].setRank(5);

cardArray[j+26+(52*i)].setRank(5);

cardArray[j+39+(52*i)].setRank(5);

}

else if (j==5){

cardArray[j+(52*i)].setRank(6);

cardArray[j+13+(52*i)].setRank(6);

cardArray[j+26+(52*i)].setRank(6);

cardArray[j+39+(52*i)].setRank(6);

}

else if (j==6){

cardArray[j+(52*i)].setRank(7);

cardArray[j+13+(52*i)].setRank(7);

cardArray[j+26+(52*i)].setRank(7);

cardArray[j+39+(52*i)].setRank(7);

}

else if (j==7){

cardArray[j+(52*i)].setRank(8);

cardArray[j+13+(52*i)].setRank(8);

cardArray[j+26+(52*i)].setRank(8);

cardArray[j+39+(52*i)].setRank(8);

}

else if (j==8){

cardArray[j+(52*i)].setRank(9);

cardArray[j+13+(52*i)].setRank(9);

cardArray[j+26+(52*i)].setRank(9);

cardArray[j+39+(52*i)].setRank(9);

}

else if (j==9){

cardArray[j+(52*i)].setRank(10);

cardArray[j+13+(52*i)].setRank(10);

cardArray[j+26+(52*i)].setRank(10);

cardArray[j+39+(52*i)].setRank(10);

}

else if (j==10){

cardArray[j+(52*i)].setRank(11);

cardArray[j+13+(52*i)].setRank(11);

cardArray[j+26+(52*i)].setRank(11);

cardArray[j+39+(52*i)].setRank(11);

}

else if (j==11){

cardArray[j+(52*i)].setRank(12);

cardArray[j+13+(52*i)].setRank(12);

cardArray[j+26+(52*i)].setRank(12);

cardArray[j+39+(52*i)].setRank(12);

}

else if (j==12){

cardArray[j+(52*i)].setRank(13);

cardArray[j+13+(52*i)].setRank(13);

cardArray[j+26+(52*i)].setRank(13);

cardArray[j+39+(52*i)].setRank(13);

}

}

}

}

public void useCard(){

cards_left = cards_left - 1;

}

public int get_cards_left(){

return cards_left;

}

public void setCard(int card, PlayingCard myCard){

cardArray[card]=myCard;

}

public PlayingCard getCard(int card){

return cardArray[card];

}

public void printDeck(){

for (int j = 0; j < (52*num_decks); j++){

// Print out to textview

//String cardString = cardArray[j].getCardString();

}

}

public void suffleDeck(){

for (int i = 0; i < 100*num_decks; i++)

{

int rand1 = 1 + new Random().nextInt(52*num_decks);

int rand2 = 1 + new Random().nextInt(52*num_decks);

PlayingCard tmpCard = cardArray[rand1];

cardArray[rand1] = cardArray[rand2];

cardArray[rand2] = tmpCard;

}

}

public boolean isDeckFinished(){

boolean isFinished =true;

for (int i = 0; i<52*num_decks; i++){

PlayingCard myCard = cardArray[i];

if (myCard.getState() == false){

isFinished = false;

}

}

return isFinished;

}

}

StartScreen.java

package com.michaelpeerman.hi_low;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import com.michaelpeerman.hi_low.R;

import com.michaelpeerman.hi_low.CardDeck;

import com.michaelpeerman.hi_low.PlayingCard;

public class StartScreen extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

CardDeck myDeck = new CardDeck(1);

int cards = myDeck.cards_left;

String text = Integer.toString(cards);

TextView t=new TextView(this);

t=(TextView)findViewById(R.id.main_text);

t.append(text);

}

}

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<ScrollView

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<TextView

android:id="@+id/main_text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/>

</ScrollView>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.michaelpeerman.hi_low"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="13" />

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:name=".StartScreen"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

LogCat Output

05-03 18:03:55.067: E/AndroidRuntime(9805): FATAL EXCEPTION: main

05-03 18:03:55.067: E/AndroidRuntime(9805): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michaelpeerman.hi_low/com.michaelpeerman.hi_low.StartScreen}: java.lang.NullPointerException

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.access$600(ActivityThread.java:123)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.os.Handler.dispatchMessage(Handler.java:99)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.os.Looper.loop(Looper.java:137)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.main(ActivityThread.java:4424)

05-03 18:03:55.067: E/AndroidRuntime(9805): at java.lang.reflect.Method.invokeNative(Native Method)

05-03 18:03:55.067: E/AndroidRuntime(9805): at java.lang.reflect.Method.invoke(Method.java:511)

05-03 18:03:55.067: E/AndroidRuntime(9805): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

05-03 18:03:55.067: E/AndroidRuntime(9805): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

05-03 18:03:55.067: E/AndroidRuntime(9805): at dalvik.system.NativeStart.main(Native Method)

05-03 18:03:55.067: E/AndroidRuntime(9805): Caused by: java.lang.NullPointerException

05-03 18:03:55.067: E/AndroidRuntime(9805): at com.michaelpeerman.hi_low.CardDeck.<init>(CardDeck.java:18)

05-03 18:03:55.067: E/AndroidRuntime(9805): at com.michaelpeerman.hi_low.StartScreen.onCreate(StartScreen.java:16)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.Activity.performCreate(Activity.java:4465)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)

05-03 18:03:55.067: E/AndroidRuntime(9805): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

05-03 18:03:55.067: E/AndroidRuntime(9805): ... 11 more

网友答案:

You're creating the cardArray properly, but you're not filling it with any references to PlayingCard. They're all nulls until you set them to something.

网友答案:

According to the stack trace, the line at issue is:

cardArray[(j+(52*i))].setSuit("SPADE");

In CardDeck.java.

And I'm guessing it's because the content of that element of the array is null. While you have defined the array, you don't appear to put any objects into it before you try to call .setSuit(...) on an element of it - so you are calling .setSuit(...) on null - hence NPE.

分享给朋友:
您可能感兴趣的文章:
随机阅读: