How can I generate a String Array for a ListView using conditionals to determine if the String elements should be on the List?

StackOverflow https://stackoverflow.com/questions/10392529

Question

This is my first question on the site and I hope it's not too embarrassing. I'm making an Inventory tab for an Android game. I've declared static variables at the beginning of my class and based on whether or not the variables meet a requirement (the variable needs to be equal to at least one to show up in the inventory) I want to generate a string array that populates a list that will show you what is in your inventory. When I write the code in Eclipse, it tells me it's error-free but when I try to run it in an Android emulator, I get an error and have to force exit. Here's the code for reference.

(Sorry, if the code formatting is off. Nightly is an odd browser sometimes.)

    public class Inventory extends Activity {
    public static String selection = null;
    public static String IronS = "Iron";
    public static String WoodS = "Wood";
    public static String StoneS = "Stone";
    public static String WaterS = "Water";
    public static String ShieldRS = "Shield Rim";
    public static String ShieldBS = "Shield Base";
    public static String ShieldS = "Shield";
    public static String BladeS = "Blade";
    public static String SwordHS = "Sword Handle";
    public static String SwordS = "Sword";
    public static String BreastS = "Breastplate";
    public static String GauntS = "Gauntlets";
    public static String GreaveS = "Greaves";
    public static String RuneS = "Rune(s)";
    public static String FishingPS = "Fishing Pole";
    public static String PickS = "Pickaxe";
    public static String PickHS = "Pickaxe Head";
    public static int water = 1;
    public static int iron = 1;
    public static int stone = 1;
    public static int wood = 1;
    public static int shieldr, shieldb, shield, sword, swordh, blade, breast, gaunt, greave, rune, fishingp, pick, pickh;

    public static String[] CONTENTS;
    public static String[] MENU;

    public static int getWater() {
        return water;
    }
    public static void setWater(int water) {
        Inventory.water = water;
    }

    public static int getIron() {
        return iron;
    }
    public static void setIron(int iron) {
        Inventory.iron = iron;
    }

    public static int getWood() {
        return wood;
    }
    public static void setWood(int wood) {
        Inventory.wood = wood;
    }

    public static int getStone() {
        return water;
    }
    public static void setStone(int stone) {
        Inventory.stone = stone;
    }



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            if (getWater()>1)
                CONTENTS = new String []
                        {WaterS};
            if (getIron()>1)
                CONTENTS = new String []
                        {WaterS,IronS};
            if (getWood()>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS};
            if (getStone()>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,StoneS};
            if (shieldr>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS};
            if (shieldb>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS};
            if (shield>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS};
            if (sword>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS};
            if (swordh>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS};
            if (blade>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS};
            if (breast>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS};
            if (gaunt>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS};
            if (greave>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS};
            if (rune>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS,RuneS};
            if (fishingp>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS,RuneS,FishingPS};
            if (pick>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS};
            if (pickh>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS,PickHS};
            if (stone>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS,SwordS,SwordHS,BladeS,
                         BreastS,GauntS,GreaveS,RuneS,FishingPS,PickS,PickHS,StoneS};
            Arrays.sort(CONTENTS);
    }


}

When I remove the if statements and just leave the array, there are no problems. It compiles as usual and I can insert the array into a ListView. I've been banging away at this for a while and have tried making switch/case statements instead of the ifs but it didn't work correctly (wouldn't take multiple variables into account for conditions). If-else-if didn't work, because the statements wouldn't continue to the preceding conditions if a true value was found.

I'm very new at this, so I have no idea how to precede. i can't think of another way to populate an inventory list without conditionals evaluating if items even "exist" (in the game).

Was it helpful?

Solution

You have a logical error not a syntactical one.

These integers are null

public static int shieldr, shieldb, shield, sword, swordh, blade, breast, gaunt, greave, rune, fishingp, pick, pickh;

and you are trying to evaluate them

if (shieldr>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS};
            if (shieldb>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS};
            if (shield>1)
                CONTENTS = new String []
                        {WaterS,IronS,WoodS,WaterS,ShieldRS,ShieldBS,ShieldS};

...
...

This would throw a null exception your variables need values before you use them in expressions.

Have fun !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top