0

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!

2
  • Class should begin with a capital letter! Commented Jan 7, 2013 at 16:48
  • @JpBond correction should Commented Jan 7, 2013 at 16:51

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

But, how can i initialize them wihout B[++lg] = new pair(); //here this?
What do you mean? You will need to loop over the items of the array and initialise them one by one.
@user1948703 You have to initialise it some how. Woot4Moo's answer is an alternative, but this is better.
Yes, I understand now, but to loop over all items is the only solution?
@user1948703 If you want to initialise them all, yes.
3

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);
    }
}

Comments

2
    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;

1 Comment

@user1948703 sorry forgot to include it, yes it would be import java.util.Scanner;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.