Runtime Error

Discussion on Java at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
Post Reply
User avatar
alurquiza
Posts: 54
Joined: 3 years ago
Gender: Male

Runtime Error

Post by alurquiza » 2 years ago

This code give Runtime Error en la linea que esta comentada, quien me ayuda a arreglarla.
Da este mensaje: java.lang.NullPointerException

Code: Select all

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;

public class COJ_1480 {
			
	static class par{
		double a,b;
		
		public par(){
			a = b = 0;
		}
	}
	
	static class trio{
		double cost;
		int n1,n2;
	}
	
	static par A[];
	static trio V[];
	
	static class DisjointsSets{
		int DS[];
		
		public DisjointsSets(int ne){
			DS = new int[ne + 1];
			for(int i = 1;i <= ne;i++){
				DS[i] = i;
			}
		}
		
		public int busca(int a){
			if(a != DS[a])
				DS[a] = busca(DS[a]);
			return DS[a];
		}
		
		public void une(int a,int b){
			DS[busca(a)] = busca(b);
		}
	}
	
	public static void main(String []args) throws NumberFormatException, IOException{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(System.out);
		
		int N = Integer.parseInt(in.readLine());
		
		A = new par[N + 1];
		
		DisjointsSets C = new DisjointsSets(N + 1);
		
		int capacity = (N * (N - 1)) / 2 + 1;
		V = new trio[capacity];
		
		int cont = 0;
		
		String E[];
		
		for(int i = 1;i <= N;i++){
			E = in.readLine().split(" ");
			A[i].a = Double.parseDouble(E[0]); /*Runtime error in this line*/
			A[i].b = Double.parseDouble(E[1]);
			
			for(int j = 1;j < i;j++){
				V[cont++].cost = dist(A[i],A[j]);
				V[cont].n1 = i;
				V[cont].n2 = j;				
			}
		}
		
		Arrays.sort(V);
		
		double sol = 0;
		for(int i = 0;i < capacity;i++){
			if(C.busca(V[i].n1) != C.busca(V[i].n2)){
				C.une(V[i].n1,V[i].n2);
				sol += V[i].cost;
			}
		}
		
		out.println(sol * 5);
	}
	
	private static double dist(par par, par par2) {
		return Math.sqrt(Math.pow(par.a - par2.a,2) + Math.pow(par.b - par2.b,2));
	}
}



User avatar
frankr
Posts: 49
Joined: 6 years ago
Location: Las Tunas
Gender: Male

Re: Runtime Error

Post by frankr » 2 years ago

Postea la entrada para la cual te da RTE, tal vez es problema con la entrada. Por otra parte NullPointerException
quiere decir que alguna variable tiene el valor null, puede ser el arreglo A o el E

User avatar
alurquiza
Posts: 54
Joined: 3 years ago
Gender: Male

Re: Runtime Error

Post by alurquiza » 2 years ago

8
10.23 12.45
11.34 20.45
34.65 45.56
11.34 23.87
12.89 23.09
77.50 99.00
15.78 12.32
25.95 44.44

User avatar
Oreste
Posts: 8
Joined: 6 years ago
Gender: Male

Re: Runtime Error

Post by Oreste » 2 years ago

atendiendo a lo k comente en el hilo anterior estas accediendo a memoria que no existe, basta con agregar
for(int i=0;i<=N;i++)A=new par();
for(int i=0;i<capacity;i++)V=new trio();
ademas estas llamando a la funcion Arrays.sort(V); sin haber definido el criterio de ordenamiento(esto es bastante parecido a c).

User avatar
alurquiza
Posts: 54
Joined: 3 years ago
Gender: Male

Re: Runtime Error

Post by alurquiza » 2 years ago

Gracias ya lo arregle, Accepted: http://coj.uci.cu/24h/submission.xhtml?id=961975

Post Reply

Return to “Java”