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 840905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:42:16+00:00 2026-05-15T05:42:16+00:00

I have written the following mergesort code. public class mergesort { public static int

  • 0

I have written the following mergesort code.

public class mergesort {

    public static int a[];

    public static void merges (int work[], int low, int high) {
        if (low==high)
            return;
        else {
            int mid = (low+high)/2;
            merges(work,low,mid);
            merges(work,mid+1,high);
            merge(work,low,mid+1,high);
        }
    }

    public static void main (String[] args) {
        int a[] = new int[] {64, 21, 33, 70, 12, 85, 44, 99, 36, 108};
        merges(a,0,a.length-1);
        for (int i=0; i<a.length; i++) {
            System.out.println(a[i]);
        }
    }


    public static void merge (int work[], int low, int high, int upper) {
        int j = 0;
        int l = low;
        int mid = high-1;
        int n = upper-l+1;
        while (low<=mid && high<=upper)
            if (a[low] < a[high])
                work[j++] = a[low++];
            else
                work[j++] = a[high++];
        while (low <= mid)
            work[j++]=a[low++];
        while (high <= upper)
            work[j++] = a[high++];
        for (j=0;j<n;j++)
            a[l+j]=work[j];
    }
}

It does not work. After compilation, there is this error:

java.lang.NullPointerException
    at mergesort.merge(mergesort.java:45)
    at mergesort.merges(mergesort.java:12)
    at mergesort.merges(mergesort.java:10)
    at mergesort.merges(mergesort.java:10)
    at mergesort.merges(mergesort.java:10)
    at mergesort.main(mergesort.java:27)

How can this problem be fixed?

  • 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-05-15T05:42:17+00:00Added an answer on May 15, 2026 at 5:42 am

    You have two arrays called a:

    a static, mergesort.a:

    public static int a[];
    

    and a local variable in main:

    int a[]=new int[]{64,21,33,70,12,85,44,99,36,108};
    

    When you use a in merge, you use the static member, which was never initialized.
    It you wanted to initialize mergesort.a, you should have written

    a = new int[]{64,21,33,70,12,85,44,99,36,108};
    

    However, that would make merge weird, since it will have work = a as an argument, and reference to the static mergesort.a, which would then be the same array.

    Confused? So am I… :)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in a program (written in VB.NET): Imports Microsoft.Office.Interop.Excel Public Class
I have written following PHP code: $input=menu=1&type=0&; print $input.<hr>.ereg_replace('/&/', ':::', $input); After running above
I have written the following simple test in trying to learn Castle Windsor's Fluent
I have written something that uses the following includes: #include <math.h> #include <time.h> #include
I have written a KornShell (ksh) script that sets an array the following way:
(All of the following is to be written in Java) I have to build
I have written some code in my VB.NET application to send an HTML e-mail
I have written following stored procedure. If I don’t use IF ELSE block Order
I have written following regular expression /^[A-Za-z0-9-_\s]*$/ in PHP which allows numbers, letters, spaces,
I have written following function which checks whether start_date field is not empty and

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.