首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复NullPointerException?

修复NullPointerException?
EN

Stack Overflow用户
提问于 2015-02-15 05:47:48
回答 1查看 228关注 0票数 1

我怎样才能让程序输出所有信息?它当前返回一个NullPointException错误。谢谢。我应该使用删除方法,就像它们一样,我不能更改它们,但是我确信一定有我能做的事情。

代码语言:javascript
复制
 public class TestCandidate7
{     

 public static int getTotal(Candidate[] election)
 {
  int total = 0;
  for(Candidate candidate : election )
  {
    total += candidate.numVotes;
  }
   return total;
 }

 public static void printResults(Candidate[] election)
 {
   double percent;
   System.out.println("Candidate        Votes Received      % of Total Votes");
   for (int x = 0; x < election.length; x++)
    {
      percent = (double) (election[x].votes()) / getTotal(election) * 100;
      System.out.printf("%-15s %10d %20.0f", election[x].getName(), election[x].votes(), percent);
      System.out.println();
    }
  }     

   public static void deleteByLoc(Candidate[] election, 
                                int location) 
 {          
   if ((location > 0) && (location < election.length))
      {
          //move items up in the array -
          for(int index = location; index < election.length -1; index++)
              election[index] = election[index + 1];

          election[election.length-1] = null;
      }
}

   public static void deleteByName(Candidate[] election, 
                                String find) 
{
    int location = 0;
    int index;

    // find location of item you want to delete
    for(index = 0; index < election.length; index++)
      if ((election[index] != null) && (election[index].getName().equals(find)))
           { 
               location = index;
               break;
            }
      else if (election[index] == null)
            {
                location = -1;
                break;
            }

   if ((index != election.length) && (location >= 0))       
    { //move items up in the array 
      for(index = location; index < election.length -1; index++)
         election[index] = election[index + 1];

       election[election.length-1] = null;
    }
}  

 public static void main(String[] args)
 {
    Candidate[] election = new Candidate[10];

    // create election
    election[0] = new Candidate("John Smith", 5000);
    election[1] = new Candidate("Mary Miller", 4000);        
    election[2] = new Candidate("Michael Duffy", 6000);
    election[3] = new Candidate("Tim Robinson", 2500);
    election[4] = new Candidate("Joe Ashtony", 1800);  
    election[5] = new Candidate("Mickey Jones", 3000);
    election[6] = new Candidate("Rebecca Morgan", 2000);
    election[7] = new Candidate("Kathleen Turner", 8000);
    election[8] = new Candidate("Tory Parker", 500);
    election[9] = new Candidate("Ashton Davis", 10000);

    System.out.println("Original results:");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

    deleteByLoc(election, 6);
    System.out.println("Deleted location 6:");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

    deleteByName(election, "Kathleen Turner");
    System.out.println("Deleted Kathleen Turner:");
    System.out.println();
    printResults(election);
    System.out.println();
    System.out.println("Total of votes in election: " + getTotal(election) );
    System.out.println();

 }

}

候选

代码语言:javascript
复制
public class Candidate
{
 // instance variables 
 int numVotes;
 String name;

/**
 * Constructor for objects of class InventoryItem
 */
 public Candidate(String n, int v)
 {
 // initialise instance variables
 name = n;
 numVotes = v;
 }
 public int votes() 
 {
    return numVotes;
 }
 public void setVotes(int num)
 {
    numVotes = num;
 }
 public String getName()
 {
    return name;
 }
 public void setName(String n)
 {
    name = n;
 }    
 public String toString()
 {
   return name + " received " + numVotes + " votes.";
 }

}

EN

回答 1

Stack Overflow用户

发布于 2015-02-15 05:56:20

当您“删除”数组元素时,在移位之后将null分配给数组中最右边的元素。

getTotal()中,您遍历整个数组并检索每个元素的numVotes值。当您到达null元素时,您将得到异常,因为null没有任何字段。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28523402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档