Question

I use two dimensional array and suppose I want to convert a string to int in a specific element like that arr[0][1]="12";. I am using this

int id=0; String[][] description=new String[obj.length][]; 
String[] temp; 
for(int i=0; i<obj.length; i++) { 
   da[i]=obj[i].toString(); temp=da[i].split(","); 
   description[i]=new String[temp.length]; 
   for(int j=0; j<temp.length; j++) {
        // id=new String[temp.length]; 
      description[i][j]=temp[j];
      if(j==0) { 
      try { 
          id=Integer.parseInt(description[i][j]); 
      } 
      catch(NumberFormatException nfe) { // Log exception. } 
      //id=Integer.parseInt(description[i][j].toString());
     Toast.makeText(getApplicationContext(),id, Toast.LENGTH_SHORT).show();
   } 
}

But it gives null pointer exception.

Was it helpful?

Solution

check if its not null then cast to integer:

    try { 
             if(description[i][j]!=null && description[i][j].length()>0){
              id=Integer.parseInt(description[i][j]); 
              Toast.makeText(getApplicationContext(),id, Toast.LENGTH_SHORT).show();
    }
          } 

instead of

try { 
          id=Integer.parseInt(description[i][j]); 
      } 

i hope its work but description[i][j] must returns string.

if id getting value then print toast.otherwise no toast print..

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