Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is the code (including both classes):

import java.util.Scanner;
import java.util.Random;
public class SortedArray
{
    public int [] myNums;
    public int sizeOfArray;
    private int userInput;
    public Sort sortedArray;
    public Scanner input = new Scanner(System.in);
    public void sortedArray()
    {
        System.out.println("Enter the number of values to put in the array:");
        int userInput = input.nextInt();
        myNums = new int [userInput];
        sizeOfArray = userInput;
        for(int i=0;i<userInput;i++)
        {
            Random randomNum =  new Random();
            myNums[i] = randomNum.nextInt(100)+1;
        }
        for(int i=0;i<userInput;i++)
        {
            System.out.println(myNums[i]);
        }
        sortedArray = new Sort(myNums, sizeOfArray);
        sortedArray.publicdisplay();
    } 
}

*******************************************************************************

public class Sort
{
    public int[] array;
    public int sizeOfArray;

    public Sort(int[] oldArray, int sizeOfOldArray)
    {
       sizeOfArray = sizeOfOldArray; 
       for( int i = 0; i < sizeOfArray; i++) {
           array[i] = oldArray[i];
       }
       sort();
    }

    public void publicdisplay()
    { 
       for ( int i = 0; i < sizeOfArray; i++){
           System.out.println(array[i]);
       }
    }

    private void sort()
    {
        for (int i = 0; i < sizeOfArray; i++)
        {
            for (int j = 0; j < sizeOfArray; i++)
            {
                if (array[j] < array[i])
                {
                    swap(i,j);
                }
            }
        }
    }

    private void swap(int x, int y)
    {
        int temp = 100;
        temp = array[x];
        array[x] = array[y];
        array[y] = temp;
   }
}

The program compiles correctly, however when I run the program and enter the number of values to put in the array, I get the following error:

java.lang.NullPointerException
at Sort.<init>(Sort.java:17)
at SortedArray.sortedArray(SortedArray.java:32)

It generates the array but does not sort it. I would appreciate any help, thanks.

share|improve this question

closed as too localized by Anthony Pegram, Raedwald, Michal Borek, A.H., hexblot Jun 15 '13 at 12:07

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
By the way, your sort() function won't work. You can probably fix it by changing for (int j = 0; j < sizeOfArray; i++) to for (int j = i+1; j < sizeOfArray; j++). However, I suggest visiting sorting-algorithms.com/shell-sort instead. –  Edward Falk Jun 14 '13 at 0:56
    
add comment

2 Answers

You need to initialize your array. Add a line in your constructor:

public Sort(int[] oldArray, int sizeOfOldArray)
{
    sizeOfArray = sizeOfOldArray; 
    array = new int[sizeOfOldArray];  // <-- Added this line
    for( int i = 0; i < sizeOfArray; i++) {
        array[i] = oldArray[i];
}

or even better, use the Arrays.copyOfRange() utility method to do the work for you:

public Sort(int[] oldArray, int sizeOfOldArray)
{
    sizeOfArray = sizeOfOldArray; 
    array = Arrays.copyOfRange(oldArray, 0, sizeOfOldArray);
}
share|improve this answer
add comment

In your Sort class, you have declared an instance variable that is an array, called array, but you haven't initialized it, so it's still null when you try to access it in your constructor at this line:

array[i] = oldArray[i];

You need to create your array first.

share|improve this answer
1  
Although efficacious, the side effect of this approach is that the array passed in will be sorted. Modifying the array passed in may not be desirable. –  Bohemian Jun 13 '13 at 21:58
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.