NullPointerException问题

时间:2014-04-07 02:35:39

标签: java nullpointerexception

我将继续收到此错误消息。现在我已经为我的SortSearchUtil得到了它。我试过做一些调试但可以解决问题。错误如下:

 ----jGRASP exec: java PostOffice
Exception in thread "main" java.lang.NullPointerException
    at SortSearchUtil.selectionSort(SortSearchUtil.java:106)
    at PostOffice.sortLetters(PostOffice.java:73)
    at PostOffice.main(PostOffice.java:15)

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.

选择第106行排序:

if (array[indexSmallest].compareTo(array[curPos]) > 0)

我不知道我的方法有什么问题。这是我的导师给我的标准方法。我试图调试我的程序,但我很困惑。以下是错误源自的方法,selectionSort:

   public static void selectionSort(Comparable[] array)
   {
      int curPos, indexSmallest, start;
      Comparable temp;
      for (start = 0; start < array.length - 1; start++)
      {
         indexSmallest = start;
         for (curPos = start + 1; curPos < array.length; curPos++)
            if (array[indexSmallest].compareTo(array[curPos]) > 0)
            {
               indexSmallest = curPos;
            }
          // end for
         temp = array[start];
         array[start] = array[indexSmallest];
         array[indexSmallest] = temp;
      } // end for       
   }

sort方法位于底部,调用此邮局方法的SortSearchUtil.selectionSort:

import java.util.*;
import java.io.*;

public class PostOffice 
{

   private final int max = 1000;
   private Letter [] ltrAra = new Letter[max];
   private int count;

   public static void main(String [] args) 
   {
      PostOffice postOffice = new PostOffice();
      postOffice.readLetters("letters.in");
      postOffice.sortLetters();
      postOffice.printLetters();
   }

   public PostOffice() 
   {
      Letter [] Letters = ltrAra;
      this.count = 0;
   }

   public void readLetters(String filename) 
   {
      int count = 0;
      int iWork = 0;

      Scanner fin = new Scanner(filename);

      String toName, toStreet, toCity, toState, toZip;
      String fromName, fromStreet, fromCity, fromState, fromZip, temp;
      double weight;
      String sWork;
      fin = FileUtil.openInputFile(filename);
      if (fin != null)
      {
         while (fin.hasNext())
         {
            toName = fin.nextLine();
            toStreet = fin.nextLine();
            sWork = fin.nextLine();
            iWork = sWork.indexOf(",");
            toCity = sWork.substring(0, iWork);
            iWork = iWork + 2;
            toState = sWork.substring(iWork, iWork + 2);
            iWork = iWork + 3;
            toZip = sWork.substring(iWork);

            fromName = fin.nextLine();
            fromStreet = fin.nextLine();
            sWork = fin.nextLine();
            iWork = sWork.indexOf(",");
            fromCity = sWork.substring(0, iWork);
            iWork = iWork + 2;
            fromState = sWork.substring(iWork, iWork + 2);
            iWork = iWork + 3;
            fromZip = sWork.substring(iWork);

            sWork = fin.nextLine();
            weight = Double.parseDouble(sWork);   

            ltrAra[count] = new Letter(toName, toStreet, toCity, toState, toZip, fromName, fromStreet, fromCity, fromState, fromZip, weight);      
            count++;
            }
            fin.close();
         }
   }

   public void sortLetters() 
   {
     SortSearchUtil.selectionSort(ltrAra);
   }

   public void printLetters() 
   {
      for (Letter ltr : ltrAra)
      {
         System.out.println(ltr);
         System.out.println();
      }
   }
}

我的文件看起来像“letters.in”:

Stu Steiner
123 Slacker Lane
Slackerville, IL 09035
Tom Capaul
999 Computer Nerd Court
Dweebsville, NC 28804-1359
0.50
Tom Capaul
999 Computer Nerd Court
Dweebsville, NC 28804-1359
Chris Peters
123 Some St.
Anytown, CA 92111-0389
1.55

2 个答案:

答案 0 :(得分:2)

显然你得到了NPE,因为:

您将ltrAra初始化为1000个项目的数组,但您在方法readLetters()中读取的项目少于1000个。因此,在此数组的末尾,一些空引用仍然未初始化(请记住,数组创建本身并未将单个项设置为任何对象)。因此,在排序方法之后获得一些null-references =&gt; NPE。

建议的解决方案:

您应该使用ArrayList而不是数组,因为这会自动阻止您因内部范围检查而访问太多项目。

答案 1 :(得分:2)

除了Meno已经说明的上述答案之外,您还需要了解何时获得Null指针异常。

您的错误行:if (array[indexSmallest].compareTo(array[curPos]) > 0)

如果我们在此行中获得NPE,很明显array[indexSmallest] null

当您在null上调用某个操作时,您会获得NPE。希望这有助于您进行调试。

  • 此外,我们选择ArrayList超过Arrays的主要原因之一是我们不知道数组的长度。

  • 还有一项建议,如果您想坚持使用ArrayList

  • ,您可以创建Arrays,然后转换为Arrays

要将任何类的ArrayList转换为数组,请将T转换为相应的类。例如:如果您想要String数组,请将T转换为'String'

List<T> list = new ArrayList<T>();

T [] students = list.toArray(new T[list.size()]);