简体   繁体   English

DVD分类程序中的线程“ main”中的异常java.lang.NullPointerException

[英]Exception in thread “main” java.lang.NullPointerException in DVD sorting program

I am attempting to create a program that puts DVD information into an array and sorts them alphabetically based on title using the compareTo method from Comparable. 我试图创建一个程序,将DVD信息放入一个数组中,并使用Comparable中的compareTo方法根据标题按字母顺序对它们进行排序。

I have a boolean method which finds out whether the two titles are the same: 我有一个布尔方法,找出两个标题是否相同:

public boolean equals (Object other)
{
    return (title.equals(((DVD)other).getTitle()));
}

and my compareTo method looks like this: 和我的compareTo方法看起来像这样:

public int compareTo (Object other)
{
    int result;

    String otherTitle = ((DVD)other).getTitle();

    result = title.compareTo(otherTitle);

    return result;
}

These two methods are in the DVD class which I have implementing Comparable. 这两种方法都在DVD类中,我已经实现了Comparable。

The Sorting algorithm itself I took used directly from an example that sorts names alphabetically but here that is as well: 我直接从一个示例中直接使用了排序算法本身,该示例按字母顺序对名称进行排序,但这里也是如此:

public class DVDSorting 
{
    public static void selectionSort (Comparable[] list)
    {
        int min;
        Comparable temp;

        for (int index = 0; index < list.length-1; index++)
        {
            min = index;
            for (int scan = index+1; scan < list.length; scan++)
                if (list[scan].compareTo(list[min]) < 0)
                    min = scan;

            temp = list[min];
            list[min] = list[index];
            list[index] = temp;
        }
    }

    public static void insertionSort(Comparable[] list)
    {
        for (int index = 1; index < list.length; index++)
        {
            Comparable key = list[index];
            int position = index;
            while (position > 0 && key.compareTo(list[position-1]) < 0)
            {
                list[position] = list[position-1];
                position--;
            }

            list[position] = key;
        }
    }
}

I then made a DVDCollection class that initializes the array and contains a method to add DVD's. 然后,我制作了一个DVDCollection类,该类初始化数组并包含添加DVD的方法。

In my driver class to test I am able to insert DVD's and print out what has been inserted but when I attempt to sort them I get a null pointer exception even though I don't see anything initialized to null. 在要测试的驱动程序类中,我可以插入DVD并打印出已插入的内容,但是当我尝试对它们进行排序时,即使没有看到初始化为null的内容,也会出现null指针异常。 Here is my driver: 这是我的司机:

public class Movies
{
    public static void main (String[] args)
    {
        DVDCollection movies = new DVDCollection();

        movies.addDVD("The Godfather", "Francis Ford Coppola", 1972, 24.95, true);
        movies.addDVD("District 9", "Neill Blomkamp", 2009, 19.95, false);
        movies.addDVD("Iron Man", "Jon Favreau", 2008, 15.95, false);
        movies.addDVD("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
        movies.addDVD("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
        movies.addDVD("Clash of the Titans", "Louis Leterrier", 2010, 19.95, true);

        DVDSorting.selectionSort(movies.collection);
        System.out.println (movies);

        movies.addDVD("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
        movies.addDVD("Casablanca", "Michael Curtiz", 1942, 19.95, false);
        movies.addDVD("Clash of the Titans", "Desmond Davis", 1981, 5.00, false);


        DVDSorting.selectionSort(movies.collection);
        System.out.println (movies);
    }
}

I used movies.collection in the sorting call because when I attempted to use just movies like examples I have seen, it said selectionSort was not applicable for movies So i made collection a public variable and just used that. 我在sorting调用中使用了movies.collection,因为当我尝试仅使用电影(如我所见的示例)时,它说selectionSort不适用于电影,因此我将collection设为一个公共变量,并仅使用了它。

Here's the error. 这是错误。

Exception in thread "main" java.lang.NullPointerException
    at DVDSorting.selectionSort(DVDSorting.java:13)
    at Movies.main(Movies.java:15)

It tells me the error is in the DVDSorting class but since that's copied from another source I have a feeling the actual error is in my compareTo method in my DVD class. 它告诉我错误在DVDSorting类中,但是由于是从另一个源复制的,所以我感觉实际的错误是在DVD类中的compareTo方法中。

I'm very sorry if my question doesn't follow proper etiquette of the site, it's my first time posting. 非常抱歉,如果我的问题没有遵循网站的适当礼节,这是我第一次发布。 Any help would be greatly appreciated. 任何帮助将不胜感激。

My guess is that either list or list[scan] is null at some point in the selectionSort() method. 我的猜测是listlist[scan]selectionSort()方法中的某个位置为null。 Perhaps try verifying that movies.collection (from your main method) is actually storing the values that you think it is. 也许尝试验证movies.collection (从您的main方法)是否实际上存储了您认为的值。

This is a good task for the Eclipse debugger. 对于Eclipse调试器来说,这是一项很好的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM