Storing each item in array to variable fails


Last Updated:

  1. jdizzle1988

    jdizzle1988 Member This Topic's Starter

    Joined:
    Jul 1, 2010
    Messages:
    12
    Likes Received:
    1
    Ok so I have this problem where I store info to an internal file using fileOutputStream() and then I start another intent and pull that information using the fileInputStream() method. I then parse the inputStr for indexes of ";" (those are my delimeters). I then do a inputStr.substring(0, index) and store that in a section of an array (infoArray). I loop until all ";" (fields) have been extracted and stored in the array. I then put each item from the array into a variable. This is where the program errors out because I can comment just this section out and the program works flawelessly. Can anyone help? code below.


    public
    void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);



    String FILENAME = "monster";
    String inputStr = "";


    String[] infoArray =


    new String[4];

    String info;
    String inputStr1 = "";
    int index;



    byte[] byteArray = newbyte[102400];
    int bytesRead = 0;


    try {

    FileInputStream fstream = new FileInputStream(FILENAME);
    while((bytesRead = fstream.read(byteArray)) != -1) {
    inputStr = new String(byteArray,0,bytesRead);

    }
    inputStr1 = inputStr;

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }




    for (int i = 0; i == 4; i++) {


    index = inputStr1.indexOf(";");
    info = inputStr1.substring(0, index);
    inputStr1 = inputStr1.substring(index, inputStr1.lastIndexOf(";"));
    infoArray = info;
    }



    TextView tvMonsterID1 = (TextView) this.findViewById(R.id.tvMonsterID1);
    TextView tvMonsterHealth1 = (TextView) this.findViewById(R.id.tvMonsterHealth1);
    TextView tvMonsterName1 = (TextView)this.findViewById(R.id.tvMonsterName1);
    TextView tvMonsterAtk1 = (TextView)this.findViewById(R.id.tvMonsterAtk1);
    TextView tvMonsterDef1 = (TextView)this.findViewById(R.id.tvMonsterDef1);
    ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);



    if (infoArray[1] == "Shewanadon") {
    imgMonsterPic1.setImageResource(R.drawable.two);
    } else {
    imgMonsterPic1.setImageResource(R.drawable.icon);
    }


    /**--------This is where the code craps out------------------


    String ID = infoArray[0];
    String Health = infoArray[1];
    String Name = infoArray[2];
    String atk = infoArray[3];
    String def = infoArray[4];
    ---------------------------------------------------------*/




    tvMonsterID1.setText("ID: " + ID);
    tvMonsterHealth1.setText("Health : " + Health);
    tvMonsterName1.setText("Name: " + Name);
    tvMonsterAtk1.setText("Attack: " + atk);
    tvMonsterDef1.setText("Defense: " + def);

     

    Advertisement
  2. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    My first inclination is that you're receiving an ArrayIndexOutOfBounds exception. Can you confirm that's the error terminating your program?
     
  3. jdizzle1988

    jdizzle1988 Member This Topic's Starter

    Joined:
    Jul 1, 2010
    Messages:
    12
    Likes Received:
    1
    Im not really sure how to confirm that. If I debug it I cannot understand how to use the debugger. Also I am testing this app on my phone as it is reliant on another app that is on my phone, and I cannot put it in the emulater. So Im not sure if that helps or not.
     
  4. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    If you're using Eclipse, you should be able to go to the "Debug" perspective and take a look at the "LogCat" window while your program executes. I gave the same description a few posts back - check it out for a picture of what I'm talking about.

    And, if that doesn't work, then try manually putting out an error message in the log:
    Code (Text):
    1.  
    2. [SIZE=2]try {[/SIZE]
    3. [SIZE=2]  String ID = infoArray[0];[/SIZE]
    4. [SIZE=2]  String Health = infoArray[1];[/SIZE]
    5. [SIZE=2]  String Name = infoArray[2];[/SIZE]
    6. [SIZE=2]  String atk = infoArray[3];[/SIZE]
    7.    [SIZE=2]String def = infoArray[4];[/SIZE]
    8. [SIZE=2]} catch (ArrayIndexOutOfBoundsException e) {[/SIZE]
    9. [SIZE=2]   Log.e("<Your Program>", "Yup, an ArrayIndexOutOfBoundsException was thrown. Details: " + e.toString());[/SIZE]
    10. [SIZE=2]} catch (Exception e) {[/SIZE]
    11. [SIZE=2]   Log.e("<Your Program>", "A general Exception was caught. Details: " + e.toString());[/SIZE]
    12. [SIZE=2]}[/SIZE]
    13.  
    Let us know how that goes, and we can proceed from there!

    --Boogs
     
    jdizzle1988 likes this.
  5. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    It looks like you're trying to access 5 elements of a 4 element array.


    You can only use infoArray[0] - infoArray[3].
    Using infoArray[4] will make it crash.

    Mark

    p.s.
    for future reference the code will be easier to read if you post it inside CODE tags (The # button in the toolbar), so the indentation is preserved.
    Some of your code looks a bit odd, but it would be easier to check if the code was inside CODE tags.


     
    jdizzle1988 likes this.
  6. Boogs

    Boogs Well-Known Member

    Joined:
    Apr 6, 2010
    Messages:
    85
    Likes Received:
    16
    markb++

    Nothing makes me want to read your code less than not having code tags around your code.

    Even though you know the cause of the problem, it would benefit you to learn how to properly debug your programs using Eclipse. I recommend taking a little tour of the Debug perspective to gain some familiarity.

    Cheers,
    Boogs
     
  7. jdizzle1988

    jdizzle1988 Member This Topic's Starter

    Joined:
    Jul 1, 2010
    Messages:
    12
    Likes Received:
    1
    Ok thanks guys Ill remember that code thing I was looking for that when I posted the code but I couldnt find it. I looked at the logcat over the weekend I got this (this is after I changed the array bounds):

    Code (Text):
    1.  
    2. 07-06 15:53:20.075: ERROR/AndroidRuntime(10244): Uncaught handler: thread main exiting due to uncaught exception
    3. 07-06 15:53:20.115: ERROR/AndroidRuntime(10244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.monster/com.monster.game}: java.lang.StringIndexOutOfBoundsException
    4.  
    5.  
    Reposted the changed code Im stumped on this and I know it has to be something easy.

    Code (Text):
    1.  
    2. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Integer [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]size[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 4;[/SIZE][/LEFT]
    3.  
    4. [LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
    5. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
    6. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
    7. [SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]game[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
    8.  
    9.  
    10. [LEFT][SIZE=2]String FILENAME = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"monster"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    11. [SIZE=2]String inputStr = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
    12.  
    13. [LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/**[/COLOR][/SIZE][/COLOR][/SIZE]
    14. [SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]try {[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][/LEFT]
    15.  
    16. [LEFT][SIZE=2][COLOR=#3f5fbf]FileInputStream [U]fos[/U] = openFileInput(FILENAME);[/COLOR][/SIZE]
    17. [SIZE=2][COLOR=#3f5fbf]fos.read(inputStr.getBytes());[/COLOR][/SIZE]
    18. [SIZE=2][COLOR=#3f5fbf]fos.close();[/COLOR][/SIZE][/LEFT]
    19. [/COLOR][/SIZE]
    20.  
    21. [LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]} catch (IOException e) {[/COLOR][/SIZE][/LEFT]
    22.  
    23. [SIZE=2][COLOR=#3f5fbf]// [/COLOR][/SIZE]
    24. [LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf] Auto[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#7f7f9f][SIZE=2][COLOR=#7f7f9f]-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]generated catch block[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
    25.  
    26. [SIZE=2][COLOR=#3f5fbf][LEFT][SIZE=2][COLOR=#3f5fbf]e.printStackTrace();[/COLOR][/SIZE]
    27. [LEFT][SIZE=2][COLOR=#3f5fbf]}[/COLOR][/SIZE]
    28. [SIZE=2][COLOR=#3f5fbf]*/[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
    29. [/LEFT]
    30. [SIZE=2][COLOR=#3f5fbf]
    31. [/COLOR][/SIZE]
    32.  
    33. [LEFT][SIZE=2]String[] infoArray = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[5];[/SIZE]
    34. [LEFT][SIZE=2]String info;[/SIZE]
    35. [SIZE=2]String [U]inputStr1[/U] = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    36. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] index;[/SIZE][/LEFT]
    37. [/LEFT]
    38.  
    39.  
    40.  
    41.  
    42.  
    43. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] byteArray = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][102400]; [/SIZE]
    44. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] bytesRead = 0; [/SIZE][/LEFT]
    45. [/LEFT]
    46.  
    47.  
    48.  
    49.  
    50. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE][/LEFT]
    51.  
    52.  
    53.  
    54. [LEFT][SIZE=2]FileInputStream fstream = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FileInputStream(FILENAME); [/SIZE]
    55. [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]((bytesRead = fstream.read(byteArray)) != -1) { [/SIZE]
    56. [SIZE=2]inputStr = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String(byteArray,0,bytesRead); [/SIZE][/LEFT]
    57. [/LEFT]
    58.  
    59.  
    60.  
    61. [LEFT][SIZE=2]}[/SIZE]
    62. [LEFT][SIZE=2]inputStr1 = inputStr; [/SIZE][/LEFT]
    63. [/LEFT]
    64.  
    65.  
    66.  
    67. [LEFT][SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (IOException e) {[/SIZE]
    68. [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block[/COLOR][/SIZE][/COLOR][/SIZE]
    69. [SIZE=2]e.printStackTrace();[/SIZE]
    70. [SIZE=2]} [/SIZE][/LEFT]
    71. [/LEFT]
    72.  
    73.  
    74.  
    75. [LEFT][SIZE=2]&#12288;[/SIZE]
    76. [LEFT][SIZE=2]&#12288;[/SIZE]
    77. [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i = 0; i <= [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]size[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; i++) {[/SIZE][/LEFT]
    78. [/LEFT]
    79.  
    80.  
    81.  
    82. [LEFT][SIZE=2]index = inputStr.indexOf([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]";"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
    83. [LEFT][SIZE=2]info = inputStr.substring(0, index);[/SIZE]
    84. [SIZE=2]inputStr = inputStr.substring(index, inputStr.lastIndexOf([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]";"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
    85. [SIZE=2]infoArray[I] = info;[/I][/SIZE]
    86. [I][SIZE=2]}[/SIZE][/I][/LEFT]
    87. [/LEFT]
    88.  
    89.  
    90.  
    91. [LEFT][I][SIZE=2]TextView tvMonsterID1 = (TextView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterID1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    92. [LEFT][I][SIZE=2]TextView tvMonsterHealth1 = (TextView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterHealth1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    93. [I][SIZE=2]TextView tvMonsterName1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterName1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    94. [I][SIZE=2]TextView tvMonsterAtk1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterAtk1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    95. [I][SIZE=2]TextView tvMonsterDef1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterDef1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I][/LEFT]
    96. [/LEFT]
    97.  
    98.  
    99.  
    100.  
    101.  
    102. [LEFT][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (infoArray[1] == [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Shewanadon"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) {[/SIZE][/I]
    103. [LEFT][I][SIZE=2]ImageView imgMonsterPic1 = (ImageView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imgMonsterPic1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    104. [I][SIZE=2]imgMonsterPic1.setImageResource(R.drawable.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]two[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    105. [I][SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE][/I]
    106. [I][SIZE=2]ImageView imgMonsterPic1 = (ImageView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imgMonsterPic1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
    107. [i][SIZE=2]imgMonsterPic1.setImageResource(R.drawable.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]icon[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
    108. [SIZE=2]}[/SIZE][/LEFT]
    109. [/LEFT]
    110.  
    111.  
    112.  
    113.  
    114. [LEFT][SIZE=2]String ID = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE]
    115. [LEFT][SIZE=2]String Health = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    116. [SIZE=2]String Name = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    117. [SIZE=2]String atk = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
    118. [SIZE=2]String def = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
    119. [/LEFT]
    120.  
    121.  
    122.  
    123. [LEFT][SIZE=2]&#12288;[/SIZE]
    124. [LEFT][SIZE=2]ID = infoArray[0];[/SIZE]
    125. [SIZE=2]Health = infoArray[1];[/SIZE]
    126. [SIZE=2]Name = infoArray[2];[/SIZE]
    127. [SIZE=2]atk = infoArray[3];[/SIZE]
    128. [SIZE=2]def = infoArray[4];[/SIZE][/LEFT]
    129. [/LEFT]
    130.  
    131.  
    132.  
    133.  
    134. [LEFT][SIZE=2]tvMonsterID1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ID: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + ID); [/SIZE]
    135. [LEFT][SIZE=2]tvMonsterHealth1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Health : "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + Health);[/SIZE]
    136. [SIZE=2]tvMonsterName1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + Name);[/SIZE]
    137. [SIZE=2]tvMonsterAtk1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Attack: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + atk);[/SIZE]
    138. [SIZE=2]tvMonsterDef1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Defense: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + def);[/SIZE][/LEFT]
    139. [/LEFT]
    140.  
    141.  
    142.  
    143. [LEFT][SIZE=2]}[/SIZE]
    144.  
    145. [SIZE=2]}[/SIZE][/LEFT]
    146.  
     
  8. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Using code tags doesn't help that much if your original code doesn't use indentation.
    It's a good habit to get into, and I'd recommend you give it some serious consideration.

    This isn't really an Android specific problem. It's a Java coding problem, and you might be better off in a forum dedicated to people learning Java.

    Here are a few observations.

    -------

    This code probably works, but only if your input file isn't very large.
    You read a buffer full each time around the loop, but only use the last buffer read.
    If the file isn't large then you'll only read one buffers worth of data, so it will still work.

    Code (Text):
    1.  
    2. FileInputStream fstream = new FileInputStream(FILENAME);
    3. while((bytesRead = fstream.read(byteArray)) != -1) {
    4.   inputStr = new String(byteArray,0,bytesRead);
    5. }
    6. inputStr1 = inputStr;
    7.  

    -------

    In this code, if inputStr doesn't contain a ";" character then index will be -1, and the substring call will fail with a string index out of bounds exception.

    Code (Text):
    1.  
    2. for (int i = 0; i <= size; i++) {
    3.   index = inputStr.indexOf(";");
    4.   info = inputStr.substring(0, index);
    5.   inputStr = inputStr.substring(index, inputStr.lastIndexOf(";"));
    6.   infoArray = info;
    7. }
    8.  

    -------

    This code almost certainly isn't doing what you intend it to do.
    You don't use == to compare strings.

    Code (Text):
    1.  
    2. if (infoArray[1] == "Shewanadon") {
    3.   ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);
    4.   imgMonsterPic1.setImageResource(R.drawable.two);
    5. } else {
    6.   ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);
    7.   [i]imgMonsterPic1.setImageResource(R.drawable.icon);
    8. }
    9.  
    The syntax
    imgMonsterPic1
    isn't valid Java.

    Does your code compile?

    As the error you get is
    java.lang.StringIndexOutOfBoundsException
    you need to look for any code that indexes into a string where the index might be too large or small. From the code you supplied it'll be the call to substring I mention earlier in this post.

    But as boogs suggested, it will be worth your while getting to know the development tools, and getting to grips with the debugger.

    Mark
     

Share This Page

Loading...