Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:31:57+00:00 2026-06-16T22:31:57+00:00

I started to learn java yesterday and I wrote the followind program which should

  • 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:31:59+00:00Added an answer on June 16, 2026 at 10:31 pm
        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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started writing a small program to learn a bit more about java and
I recently started to learn Java programming and we just reach the Java Threads
greetings, today i have started to learn java using netbeans ide. i would like
Okay, first off, i started (trying) to learn Java about 2 days ago. im
I have just started to learn the very basics of Java programming. Using a
I've recently (4 days) started to learn C++ coming from C / Java background.
I started to learn generics today, but this is somelike weird for me: I
I just started to learn Java, so have a lot of questions. And now
I just started to learn Android, I'm trying to write a widget which is
I have started to program in Java SE recently, and I noticed from many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.