简体   繁体   中英

Exception in thread “main” java.lang.NullPointerException

I started to learn java yesterday and I wrote the followind program which should print pairs of equal numbers, but when I run it I get

Exception in thread "main" java.lang.NullPointerException
at _aaaa.main(_aaaa.java:26)

Here is my program:

import java.util.*;

class pair {
    int first, second;
    pair() {
        first = second = 0;
    }
    public void make_pair(int a, int b)
    {
        first = a;
        second = b;
    }
}
public class aaaa {
    public static void main(String[] idontneedthis)
    {
        Scanner input = new Scanner(System.in);
        int N = input.nextInt(), i, lg = 0;
        int[] A = new int[5010];
        pair[] B = new pair[5010];
        for (N <<= 1, i = 1; i <= N; ++i)
        {
            int var = input.nextInt();
            if (A[var] > 0)
            {
                B[++lg].make_pair(A[var], var);
                A[var] = 0;
            }
            else
            {
                A[var] = i;
            }
        }
        if (lg == 0) System.out.print("-1");
        for (i = 1; i <= lg; ++i)
        {
            System.out.print(B[i].first + " " + B[i].second + "\n");
        }
    }
}

Please tell me what is wrong or why do I get this error. I mention that if I cut the line 26 ( B[++lg].make_pair(A[var], var); ) it will write -1.

Thank you!

You need to initialise the pairs in your array:

if (A[var] > 0) {
    B[++lg] = new pair(); //here
    B[lg].make_pair(A[var], var);
    A[var] = 0;
}

This line:

pair[] B = new pair[5010];

creates an array of 5010 pairs but until you initialise them, they are all null.

Also note that since 5010 and N are not related, you could get an ArrayIndexOutOfBoundException depending on N.

This is how I would write it. The less said the better ;)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Pair {
    final int first, second;

    Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }

    @Override
    public String toString() {
        return first + " " + second;
    }
}

public class Main {
    public static void main(String... ignored) {
        Scanner input = new Scanner(System.in);
        int numOfPairs = input.nextInt();
        List<Pair> pairs = new ArrayList<Pair>();
        for(int i = 0; i < numOfPairs;i++) {
            int first = input.nextInt();
            int second = input.nextInt();
            pairs.add(new Pair(first, second));
        }
        for (Pair pair : pairs) 
            System.out.println(pair);
    }
}
    pair[] B = new pair[5010];

Only allocates the space for 5010 B elements. You need to instantiate each element in that array.

for(int i = 0; i <B.length;i++)  
{  
    B[i] = new pair();
}

Style things:

Class names start with upper case letters: AAAA not aaaa .
also star imports are bad:

import java.util.*;    

replace with:

import java.util.Scanner;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM