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 written the following code class Program { static void Main(string[] args) {
I have written following code for getting location name : UseGpsActivity.java public class UseGpsActivity
I have written following xaml code: <Window x:Class=WpfApplication3.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml Title=MainWindow Height=200 Width=200> <StackPanel>
I have written following mergesort code, but its not working. Could you please help
I have written following code on Textbox Validated event : private void txtHbhakt1_Validated(object sender,
I have written following function public void TestSB() { string str = The quick
I have written following code to attach gesture recogniser to multiple imageviews. [imageview1 setUserInteractionEnabled:YES];
I am trying my hands on WPF MVVM. I have written following code in
I have written the following code, CrystalDecisions.CrystalReports.Engine.ReportDocument report = new CrystalDecisions.CrystalReports.Engine.ReportDocument(); report.Load(@C:\Users\XXX\Desktop\Backup1\Project\ReportsFolder\ReportSalesInvoice.rpt); Report works
I have written the following Utility class to get an instance of any class

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.