0

I was trying to use the compareTo() method to sort the String array while the user keep inputting Strings into array. But I keep getting errors of: Exception in thread "main" java.lang.NullPointerException at java.lang.String.compareTo(Unknown Source)

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int numsDVD =0; //eSize
    String [] DVD = new String [128];
    int [] Length = new int[128];
    String choice = "";
    do
    {
        if(choice.equalsIgnoreCase("a")&&numsDVD<=DVD.length-1)
        {
            numsDVD=addDVD(DVD, Length, numsDVD, input);
        }
    }while(!(choice.equalsIgnoreCase("Q")));
    input.close();
}

public static void sortedinsert(String[] titles, int[] lengths, int numDVDs, String DVD, int length)
{
    System.out.println("Index to compare : " + (numDVDs-1));
    if(DVD.compareTo(titles[numDVDs-1])>=0)
    {
        titles[numDVDs]=DVD;
    }
    else if(DVD.compareTo(titles[numDVDs-1])<0)
    {
        for(int i=0; i<numDVDs-1; ++i)
        {
            int search = numDVDs-i;
            String tmpString = "";
            int tmplength = 0;
            if(DVD.compareTo(titles[search])>0)
            {
                for(int j=numDVDs; j>numDVDs-1; --j)
                {
                    tmpString=titles[j-1];
                    tmplength=lengths[j-1];
                    titles[j]=tmpString;
                    lengths[j]=tmplength;
                }
                break;
            }
        }
        for (int i =0; i<numDVDs; ++i)
        {
            System.out.println(titles[i]);
        }
    }
}
// add to titles and lengths arrays at index numDVDs

// return numDVDs + 1
public static int addDVD(String[] titles, int[] lengths, int numDVDs, Scanner stdIn)
{
    if(numDVDs==0)
    {
        titles[numDVDs]=getvalidatedinfo(stdIn, "Please enter DVD Title : ", "");
        lengths[numDVDs]=getvalidatedinfo(stdIn, "Please enter DVD Length : ", 0);
    }
    else
    {
        String input = getvalidatedinfo(stdIn, "Please enter DVD Title : ", "");
        int length = getvalidatedinfo(stdIn, "Please enter DVD Length: ", 0);
        sortedinsert(titles, lengths, numDVDs, input, length);
    }
    return numDVDs + 1;
}

And this is the errors I keep getting

Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(Unknown Source)
at Moviecheckeradv.sortedinsert(Moviecheckeradv.java:105)
at Moviecheckeradv.addDVD(Moviecheckeradv.java:148)
at Moviecheckeradv.main(Moviecheckeradv.java:21)

Please help me, Thank you

  • This is the line 105: if(DVD.compareTo(titles[numDVDs-1])>=0) Sorry for the comfusion – study english May 08 '16 at 06:37
  • Are you sure your `DVD` variable is not null ? Can you share the code of your `getvalidatedinfo()` method, please ? – Alexandre Fillatre May 08 '16 at 06:39
  • public static String getvalidatedinfo(Scanner stdIn, String Prompt, String validateString) { do { System.out.print(Prompt); validateString = stdIn.nextLine(); }while(validateString.length()==0); for (int i=0; i='a'&&validateString.charAt(i)<='z')||validateString.charAt(i)==' '||validateString.charAt(i)>='A'&&validateString.charAt(i)<='Z')) { System.out.print(Prompt); validateString = stdIn.nextLine(); } } return validateString; } – study english May 08 '16 at 06:43
  • @AlexandreFillatre sorry for the confusion – study english May 08 '16 at 06:49

1 Answers1

1

Always check your input and make sure it is correct and as expected. Most likely definitely the problem is that:

titles[numDVDs-1]

is null. Thus you're trying to compareTo(null) which gives the NPE.

Idos
  • 15,053
  • 14
  • 60
  • 75
  • 1
    I was going to post the same answer. This is the right one. See http://stackoverflow.com/questions/7168497/undocumented-string-comparetonull-npe as well? – Alexandre Fillatre May 08 '16 at 06:42
  • I set the numDVDs to 0 at the beginning though... – study english May 08 '16 at 06:44
  • 1
    @studyenglish *there's* your problem then... What is the meaning of `titles[-1]` ? – Idos May 08 '16 at 06:45
  • @Idos I have two seprate conditions. I don't think the value of numDVDs will became -1 though – study english May 08 '16 at 06:48
  • @studyenglish *I don't think* is not very good when it comes to programming. **Test** it! Put some `print` in your code and follow it, with a debugger preferably! :) – Idos May 08 '16 at 06:50
  • If it where `-1`, you'll get an `ArrayOutOfBoundsException`. @Idos is right, test it :) – Alexandre Fillatre May 08 '16 at 06:54
  • @Idos I tried, when the value of 'numDVDs' become 2, it just crushed as the error I show above. – study english May 08 '16 at 06:55
  • @AlexandreFillatre I've run my code. but I didn't seems to get thee ArrayOutofBoundException error. Please notes that I have two seprate condition at 'addDVD' – study english May 08 '16 at 07:00
  • This is really a wonderful bug to have, since it can be solved by using a debugger in a matter of minutes. Seriously just walk through the code line by line and check the variable contents, I am sure you will find where it goes off – Idos May 08 '16 at 07:05
  • I've just insert 'System.out.println(DVDnums-1);' in the code, it's still goes from 0 to 2 and then it crushed with the error as I mentioned above. @Idos – study english May 08 '16 at 07:19
  • I can't help you if you don't want to help yourself. Again, and for the last time, **use a debugger**. (it is obvious that it goes out of bounds...) – Idos May 08 '16 at 07:21
  • 1
    I'll try to read through the codes again, thank you for helping me. @Idos – study english May 08 '16 at 07:29