Problema de ArrayIndexOutOfBoundsException

4 respostas
V

Pessoal estou usando essa classe é dá erro de java.lang.ArrayIndexOutOfBoundsException: 0.

Alguém pode me ajudar?

public java.lang.String[][] searchPresentation(
			java.lang.String searchPresentationRequest)
	throws java.rmi.RemoteException {
		
		ClientDAO cDAO = (ClientDAO) DAOFactory
		.getDAOInstance(DAOFactory.CLIENT_DAO);

		ClientFilter clientFilter = new ClientFilter();

		String[][] presentationURLList = null;

		try {
			clientFilter.setLink(searchPresentationRequest);
		} catch (Exception e) {
		}
		List clientList = null;
		try {
			clientList = cDAO.searchObjects(clientFilter);
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long clientId = 0;
		for (int i = 0; i < clientList.size(); i++) {

			Client c = (Client) clientList.get(i);
			clientId = c.getId();

		}
		
		DAO daoPresentation = DAOFactory
		.getDAOInstance(DAOFactory.PRESENTATION_DAO);
		PresentationFilter presentationFilter = new PresentationFilter();

		try {
			presentationFilter.setClientId(clientId);
		} catch (Exception e) {
		}

		List presentationList;

		try {
			presentationList = daoPresentation
			.searchObjects(presentationFilter);
			presentationURLList = new String[presentationList.size()][5];
			if (presentationList.size() > 0) {
				for (int i = 0; i < presentationList.size(); i++) {
					Presentation p = (Presentation) presentationList.get(i);
					String path = p.getPath().toString();
					String name = p.getTitle();
					long presentationID = p.getId();
					path = path.replace('\', '/');
					path = path.substring(path.indexOf("teste"));
					presentationURLList[i][0] = "http://localhost:8082/"
						+ path + "/pres.ram";
					presentationURLList[i][1] = name;
					presentationURLList[i][2] = Long.toString(presentationID);
					presentationURLList[i][3] = Integer
					.toString(presentationList.size());
					presentationURLList[i][4] = "No momento há "+Integer.toString(presentationList
							.size())+"cadastradas.";
				}
			} else {
				//presentationURLList[0][0] = Integer.toString(presentationList.size());
				//presentationURLList[0][1] = Integer.toString(presentationList.size());
				//presentationURLList[0][2] = Integer.toString(presentationList.size());
				//presentationURLList[0][3] = Integer.toString(presentationList.size());
				//presentationURLList[0][4] = "Não há apresentações cadastradas.";
				
				
				presentationURLList[0][0] = Integer.toString(presentationList.size());
				presentationURLList[0][1] = Integer.toString(presentationList.size());
				presentationURLList[0][2] = Integer.toString(presentationList.size());
				presentationURLList[0][3] = Integer.toString(presentationList.size());
				presentationURLList[0][4] = Integer.toString(presentationList.size());
						
						
			}

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("oi: "+presentationURLList[0][0]);
		System.out.println("oi: "+presentationURLList[0][1]);
		System.out.println("oi: "+presentationURLList[0][2]);
		System.out.println("oi: "+presentationURLList[0][3]);
		System.out.println("oi: "+presentationURLList[0][4]);
		
		
		return presentationURLList;
	}

Obrigado,
Vinicius.

4 Respostas

_Renatu

Hm…

olha:

presentationURLList = new String[presentationList.size()][5];
 			if (presentationList.size() &gt 0) {
 			     ......
 			} else {
 				//presentationURLList[0][0] = Integer.toString(presentationList.size());
 				//presentationURLList[0][1] = Integer.toString(presentationList.size());
 				//presentationURLList[0][2] = Integer.toS

Ele entra nesse else?? se ele entrar, pode ser aí o erro…

você cria o vetor com uma dimensao, e depois ve se ela é maior que zero… se ele nao for maior que zero você tenta usar o vetor mesmo assim… :?

ctrlaltleo

não olhei o seu código, mas essa msg é de quando você tenta acessar um vetor com um indice maior do que o seu tamanho

vc pode ter atribuido uma variável que no incremento dentro do indice fique maior que o vetor
lembre se que array começa com 0 então o ultimo é x.length-1

neohacker

Cara só pra ajudar mais um pokinho, posta o erro ai, pelo menos ele fala a linha pra dar uma olhada

LPJava

bom hehe nem precisa realmente olhara sintaxe em algum lugar vc ta acessando um indice fora do array, ou recebendo um valor negativo tb… veja isso ai que aonde ta o erro… so para lembrar indice inicial é o valor 0.

Criado 23 de janeiro de 2007
Ultima resposta 10 de mai. de 2007
Respostas 4
Participantes 5