Duvida sobre ler um Array

2 respostas
P

OLá pessoal ,

como posso tratar o erro :
xception in thread "main" java.lang.NullPointerException
at Certifica.flow2.main(flow2.java:18)

quando vai ler um registro e não existe no array tem como verfiicar se naquele indice do array não existe …

Se alguém puder me ajudar agradeceria …conforme exemplo abaixo foi definido um array de 5 ,…mas só foi preenchido 4 …

String arr[] = new String[5];
		   arr[0] ="teste0";
		   arr[1] ="teste1";
		   arr[2] ="teste2";
		   arr[3] ="teste3";
		  // arr[4] ="teste4";

		    mainLoop: for (int i = 0; i < arr.length; i++) {
			 for (int j = 0; j < arr[i].length(); j++) {
			     if (arr[i] == "\u0000") {
			        continue mainLoop;
			    }
			    
			    	 
			     
			     if (arr[i]== null){
			    	 System.out.println("está nulo !!!!!!!!");
			    	 break;
			     }
			 }
		    }
		 


	}

2 Respostas

thiago.correa

usa-se ‘equals’ ao invés do operador ‘==’ para comparar strings. Se você quer saber se o array está nulo, esta verificação deve ser feita primeiro

String arr[] = new String[5];
     arr[0] ="teste0";
     arr[1] ="teste1";
     arr[2] ="teste2";
     arr[3] ="teste3";
     arr[4] ="teste4";

     for (int i = 0; i < arr.length; i++) {
       if (arr[i] == null){
         System.out.println("está nulo !!!!!!!!");
         break;
       }
       if (arr[i].equals("\u0000")) {
         System.out.println(arr[i]);
      }

     }
P

mesmo usando o equals está dando o erro :

Exception in thread “main” java.lang.NullPointerException
at Certifica.flow2.main(flow2.java:22)

Criado 27 de março de 2006
Ultima resposta 27 de mar. de 2006
Respostas 2
Participantes 2