0

Hi I'm not sure if this is a problem with Eclipse or Java but recently my code has stopped working. I only changed things like assigning new variables to store things. My program takes a multi-dimensional string array and should return a new array trimmed of nulls.

public void makebuttons(final int n, String equals) {
    //does lots of widget functions and id assignments
    String[] items=getArray(Integer.parseInt(data.equnits.substring(n*3, n*3+1)));
    unitvalues[n]=Integer.parseInt(data.equnits.substring(n*3, n*3+1));
    ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item, items);
    //does more code
}
public static String[] getArray(int selection) {
    String[] result;//stores the new array
    int x=0,j=0 ,ulength = data.units[0].length;//ints used
    String temp="";
    while(j < ulength && temp!=null) {
        temp= data.units[selection][j][0];    //find the actual array length
        j++;
    }
    if(j==ulength)j--;
    result = new String[j+1];//initalise array from check
    for(x=0; x<=j; x++) { //add data to array
        result[x]=data.units[selection][x][0];
    }
    return result;//return corrected array
}

Integer.parseInt and Integer.valueOf give value of 0 each time for a string like "01,02,03,04" data.equnits stores the string to be converted to integer by checking 2 digits only to select from a large 3 dimensional array. Since its a 3 dimensional array some nulls are present

Null check for the String doesnt seem to work since the while loop doesnt seem to detect it and it ends up being in the array that gets passed into the array adapter for spinner causing NullPointerException while scrolling.

Restarting eclipse doesn't help.

8
  • 1
    First port of call: insert sensible whitespace so that your code has some chance of being readable... Commented Mar 27, 2012 at 18:20
  • 3
    Second port of calling: giving us a short but complete piece of code. The chances of the problem really being in Java, Android or Eclipse are very small. Commented Mar 27, 2012 at 18:21
  • Provide more code, where does the n variable come from? Commented Mar 27, 2012 at 18:24
  • the full code is very very long. variable n and selection are integers. n is incremented normally while variable selection is a function parameter for getting a specific array from a multidimentional array. Commented Mar 27, 2012 at 18:32
  • I suspect your strings are never actually null, or the data.units[selection][j][0] would throw an error since you are trying to take the first character of a null. After the "temp != null", try adding " && !temp.isEmpty()", also remove the "[0]" from the temp assignment. Commented Mar 27, 2012 at 18:43

1 Answer 1

1

I can't help with your first problem without more information, but this seems to be the issue with your second:

The function substring is inclusive of the first parameter and exclusive of the second. Since you are only adding 1 to the n*3, you only get one character.

Try using:

substring(n*3, n*3+2)

Edit:

Adding the updated code from my comment above:

while(j < ulength && temp != null && !temp.isEmpty())
{
   temp = data.units[selection][j];
   j++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

2 digits are needed. You'll get a numberformatexception if the comma was included. substring starts from index 0. the maths for it is right
n increments by 1, for a string of "01,02,03,04" it would be substring(0,1) substring(3,4) substring(6,7) substring(9,10) that string has 11 characters. cant take convert a comma into integer. hence substring(n*3,n*3+2) would mean substring(0,2) when n=0 which would give 3 characters instead of 2 digits needed and cause an number format error.
substring returns the characters beginning with the first index, up to, but not including the second. substring(0,1) will give you the character at position 0, but not 1. substring(0,2) gives you positions 0 and 1, but not 2. If you want 2 characters returned to be parsed, (0,2) should give you that. docs.oracle.com/javase/7/docs/api/java/lang/String.html
strange because it wasnt like that before and yet it worked. Changed it to 2 and that portion works now. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.