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

  • SEARCH
  • Home
  • 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 871157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:35:54+00:00 2026-05-15T10:35:54+00:00

the program gives following exception: Exception in thread main java.lang.NullPointerException at myclasses.BubbleSort.run(BubbleSort.java:42) at acm.program.Program.runHook(Program.java:1519)

  • 0

the program gives following exception:

Exception in thread “main” java.lang.NullPointerException
at myclasses.BubbleSort.run(BubbleSort.java:42)
at acm.program.Program.runHook(Program.java:1519)
at acm.program.Program.startRun(Program.java:1508)
at acm.program.Program.start(Program.java:729)
at myclasses.BubbleSort.main(BubbleSort.java:49)

what is wrong?

thank you very much!

package myclasses;

import acm.program.DialogProgram;

public class BubbleSort extends DialogProgram {
    int[] array;

    public int[] getArray() {
    return array;
    }

    public void setArray(int[] array) {
    this.array = array;
}   


void swap(int firstPos, int secondPos) {
    int temp = array[firstPos];
    array[firstPos] = array[secondPos];
    array[secondPos] = temp;
}

public void bubblesort() {
    int i, j, k;
    for (i = 1; i < array.length; i++) {
        j = i;
        k = array[i];
        while (j > 0 && array[j - 1] > k) {
            array[j] = array[j - 1];
            --j;
        }
        array[j] = k;
    }
} 


public void run() {
    BubbleSort a = new BubbleSort();
    a.setArray(new int[] {1, 3, 5, 7, 6, 2});
    a.bubblesort();
    StringBuffer sb = new StringBuffer(a.array.length * 2);
    for (int i = 0; i < getArray().length; i++) sb.append(getArray()[i]).append(" ");
    println(sb);

}    


public static void main(String[] args) {
    new BubbleSort().start(args);

            }

}
  • 1 1 Answer
  • 2 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-15T10:35:55+00:00Added an answer on May 15, 2026 at 10:35 am

    Change the for loop in your run method to:

    for (int i = 0; i < a.getArray().length; i++) sb.append(a.getArray()[i]).append(" ");
    

    Explanation:

    On this line you had 2 calls to getArray() which will call getArray() on the instance of BubbleSort created in main whereas the array has been set on a, the instance of BubbleSort created in run() so the 2 calls to getArray in the for loop need to be a.getArray(). The array in the instance created in main has never been set so getArray().length will throw a NullPointerException.

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

Sidebar

Related Questions

When I run the java program it gives following error: Exception in thread main
Possible Duplicate: Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”' I'm hoping this is
When I try to run my program, it gives the following error... Exception in
Why gives the following Windows 7 .cmd command script: set SUN_JAVA_HOME=C:\Program Files (x86)\Java\jdk1.6.0_17 if
My program installation sometimes gives the following error in the installer log: `Failed to
I am trying to initialize NSDictionary but it gives me following error: Program received
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
New Java programmers often encounter messages like the following when they attempt to run
Given the following simple program: import wx class TestDraw(wx.Panel): def __init__(self,parent=None,id=-1): wx.Panel.__init__(self,parent,id,style=wx.TAB_TRAVERSAL) self.SetBackgroundColour(#FFFFFF) self.Bind(wx.EVT_PAINT,self.onPaint)
Changing a Linux C++ program which gives the user limited file access. Thus the

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.