Null Pointer Exception in DFS

I was solving “Fence Planning” here,
but when I submitted my solution, I got a Null Pointer Exception. A similar thing happened with Moocast, but I managed to fix it and forgot how.

My Work

import java.util.*;
import java.io.*;

public class FencePlanning_9USOS3 {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;
        public InputReader() throws FileNotFoundException {
            reader = new BufferedReader(new FileReader("fenceplan.in"));
            tokenizer = null;
        }
        String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() { return Integer.parseInt(next()); }
        public long nextLong() { return Long.parseLong(next()); }
        public double nextDouble() { return Double.parseDouble(next()); }
    }
    public static ArrayList<Integer> adj[];
    public static boolean visited[];
    public static boolean used[];
    public static long minX;
    public static long maxX;
    public static long minY;
    public static long maxY;
    public static int x[];
    public static int y[];
    public static void main(String[] args) throws FileNotFoundException, IOException {
        InputReader r = new InputReader();
        PrintWriter pw = new PrintWriter(new FileWriter("fenceplan.out"));

        int N = r.nextInt();
        int M = r.nextInt();
        int[] x = new int[N];
        int[] y = new int[N];
        ArrayList<Integer>[] adj = new ArrayList[N];
        for (int i = 0; i < N; i++) {
            adj[i] = new ArrayList<Integer>();
        }
		visited = new boolean[N];
		used = new boolean[N];
        for (int i = 0; i < N; i ++)
        {
        	x[i] = r.nextInt();
        	y[i] = r.nextInt();
        }
        for (int i = 0; i < M; i ++)
        {
        	int a = r.nextInt();
        	int b = r.nextInt();
        	adj[a-1].add(b-1);
        	adj[b-1].add(a-1);
        }
        Arrays.fill(visited, false);
        Arrays.fill(used, false);
        long ans = Integer.MAX_VALUE;
        for (int i = 0; i < N; i ++)
        {
        	if (used[i])
        	{
        		break;
        	}
        	else
        	{
        		minX = Integer.MAX_VALUE;
        		maxX = 0;
        		minY = Integer.MAX_VALUE;
        		maxY = 0;
        		dfs(i,adj);
        		ans=2*(maxX-minX+maxY-minY);
        	}
        }
        pw.println(ans);
        pw.close();
    }
    public static void dfs(int node,ArrayList<Integer>[] adj)
	{
		visited[node] = true;
		used[node] = true;
		for (int u: adj[node])
			if(!visited[u])
				dfs(u,adj);
		minX = Math.min(minX, x[node]);
		maxX = Math.max(maxX, x[node]);
		minY = Math.min(minY, y[node]);
		maxY = Math.max(maxY, y[node]);
	}
}




I thought that a null pointer exception happened when one of the arrays or ArrayLists were null, but printing them seems to give the correct result, so I don’t understand why I have a null pointer. The errors were on lines 89 and 90, specifically at the line dfs(u,adj);

Printing x at the site of the exception gives null.