Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Code for sorting four strings lexicographically

843785Oct 2 2008 — edited Oct 2 2008
public class compareStrings
{
    private String[] originalArray;
    private int number;
    private String[] printArray;
    private String currentString;
    private String string1;
    private String string2;
    private String string3;
    private String string4;
    
    public compareStrings (String string1, String string2, String string3, String string4)
    {
        String[] originalArray = new String[4];
        String[] printArray = new String[4];
        originalArray[0] = string1;
        originalArray[1] = string2;
        originalArray[2] = string3;
        originalArray[3] = string4;
        int number = 0;    
        String currentString = null;
    }
    

    public void sort() 
    {
        for (int i = 0; i <4; i++)
        {
            currentString = originalArray;
if (originalArray[i].compareTo(string1) < 0)
{
number = number + 1;
}
if (originalArray[i].compareTo(string2) < 0)
{
number = number + 1;
}
if (originalArray[i].compareTo(string3) < 0)
{
number = number + 1;
}
if (originalArray[i].compareTo(string4) < 0)
{
number = number + 1;
}
printArray[3-number] = currentString;
}
for (int j = 0; j<4; j++)
{
System.out.print(printArray[j] + " ");
}
}
}
Why does it have a nullpointerexception on " currentString = originalArray;" when i call the method sort? 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 30 2008
Added on Oct 2 2008
4 comments
271 views