引用初始化为null的对象数组中的槽

时间:2013-04-16 17:18:38

标签: java debugging generics iterator nullpointerexception

我正在尝试检查数组的槽是否等于null,以便迭代哈希表。但是,我得到了NullPointerException。有没有办法解决这个问题?

谢谢!

这是造成问题的next()方法(及其类)。

 public K next() throws NoSuchElementException{
      int tempStore = -1;
      MapHashCellList tempCells = null;
      System.out.println("Got here! 1");
      System.out.println(mapCells);
      if(mapCells.length==0) {throw new NoSuchElementException();}
      if (mapCells[arrayPointer]!=null && listCounter<=mapCells[arrayPointer].size()-2){
              System.out.println("Got here! 2");
        tempCells = mapCells[arrayPointer];
        tempStore = listCounter;
        listCounter++;
      }
      else{
        arrayPointer++;
              System.out.println("Got here! 3");
        while (mapCells[arrayPointer]==null){
          arrayPointer++;
          listCounter=0;
                System.out.println("Got here! 4");
          if(mapCells[arrayPointer]!=null){
            tempCells = mapCells[arrayPointer];
            tempStore = listCounter;
            listCounter++;
          }
          System.out.println("ListCounter: " + listCounter);
          System.out.println("Array: " + arrayPointer);
        }
      }
      return (K)tempCells.get(tempStore).getKey();
    }

根据用户的一个问题,这是整个班级的代码:

import java.util.*;

public class ProjectMap<K,V> implements MapInterface<K,V>{

  public MapHashCellList[] mapCells;
  public int size;

  public ProjectMap(){
    mapCells = new MapHashCellList[11];
    size = 0;
  }

  public boolean containsKey( K key ){
    if(key==null){return false;}
    if(isEmpty()){return false;}
    if(mapCells[hashValue(key)]!=null){
      MapHashCellList temp = (MapHashCellList)mapCells[hashValue(key)];
      for (int g =0; g<temp.size(); g++){
        if (temp.get(g).getKey().equals(key)){
          return true;
        }
      }
    }
    return false;
  }

  public boolean isEmpty(){
    if(size==0){return true;}
    else return false;
  }


  public int hashValue(K key){
    return (int)(Math.abs(key.hashCode()%10));
  }

  public MapHashCellList get(int index){
    return (MapHashCellList)mapCells[index];
  }

  public V get( K key ){
    if(mapCells[hashValue(key)]!=null){
      MapHashCellList temp = (MapHashCellList)mapCells[hashValue(key)];
      for (int q =0; q<temp.size(); q++){
        if (temp.get(q).getKey().equals(key)){
          return (V)temp.get(q).getValue();
        }
      }

    }
    return null;
  }

  //Key = docID, V = documentNumber
  public V put( K key, V value ){
    //If cell isn't empty and Key exists, replace existing Value since duplicate
    if(mapCells[hashValue(key)]!=null){
      MapHashCellList temp = (MapHashCellList)mapCells[hashValue(key)];
      for (int q =0; q<temp.size(); q++){
        if (temp.get(q).getKey().equals(key)){
          V temp1 = (V)temp.get(q).getValue();
          temp.get(q).setValue(value);
          return temp1;
        }
      }
    }

    //Otherwise add to end of linkedlist
    if(mapCells[hashValue(key)]!=null){
      MapHashCellList temp = (MapHashCellList)mapCells[hashValue(key)];
      MapHashCell newCell = new MapHashCell(key,value);
      temp.add(newCell);
      size++;
      return null;
    }


    //Index doesn't exist yet, so make new LinkedList and add to correct index position
    MapHashCellList newCell = new MapHashCellList(key,value);
    mapCells[hashValue(key)] = newCell;
    size++;
    return null;
  }


  public V remove( K key ){
    if(mapCells[hashValue(key)]!=null){
      MapHashCellList temp = (MapHashCellList)mapCells[hashValue(key)];
      for (int q =0; q<temp.size(); q++){
        if (temp.get(q).getKey().equals(key)){
          V temp1 = (V)temp.get(q).getValue();
          temp.remove(q);
          size--;
          return temp1;
        }
      }
    }
    return null;
  }

  public int size(){
    return size;
  }

  public Iterator<K> iterator(){
    Iterator<K> temp = new KIterator();
    return temp;
  }

  private class KIterator implements Iterator<K>{
    int arrayPointer=0;
    int listCounter=0;

    KIterator(){
    }


    public K next() throws NoSuchElementException{
      int tempStore = -1;
      MapHashCellList tempCells = null;
      System.out.println("Got here! 1");
      System.out.println(mapCells);
      if(mapCells.length==0) {throw new NoSuchElementException();}
      if (mapCells[arrayPointer]!=null && listCounter<=mapCells[arrayPointer].size()-2){
              System.out.println("Got here! 2");
        tempCells = mapCells[arrayPointer];
        tempStore = listCounter;
        listCounter++;
      }
      else{
        arrayPointer++;
              System.out.println("Got here! 3");
        while (mapCells[arrayPointer]==null){
          arrayPointer++;
          listCounter=0;
                System.out.println("Got here! 4");
          if(mapCells[arrayPointer]!=null){
            tempCells = mapCells[arrayPointer];
            tempStore = listCounter;
            listCounter++;
          }
          System.out.println("ListCounter: " + listCounter);
          System.out.println("Array: " + arrayPointer);
        }
      }
      return (K)tempCells.get(tempStore).getKey();
    }

    public boolean hasNext(){
      if(isEmpty()) {return false;}
      for(int d=arrayPointer; d<mapCells.length; d++){
        if (mapCells[d]!=null){
          try{
            if(mapCells[d].get(listCounter+1)!=null){
              return true;
            }
          }
          catch(IndexOutOfBoundsException e){
            for(int l=arrayPointer; l<mapCells.length; l++){
              if (mapCells[l]!=null){
                return true;
              }
              else{
                return false;
              }
            }
          }
        }
      }
      return false;
    }

    //Removes last element
    public void remove(){
      mapCells[arrayPointer].remove(listCounter-1);
    }
  }
}

1 个答案:

答案 0 :(得分:1)

至少有一个问题出在您的public K next方法上:

public K next() throws NoSuchElementException{
  int tempStore = -1;
  MapHashCellList tempCells = null;
  System.out.println("Got here! 1");
  System.out.println(mapCells);
  if(mapCells.length==0) {throw new NoSuchElementException();}
  if (mapCells[arrayPointer]!=null && listCounter<=mapCells[arrayPointer].size()-2){
          System.out.println("Got here! 2");
    tempCells = mapCells[arrayPointer];
    tempStore = listCounter;
    listCounter++;
  }
  else{
    arrayPointer++;
          System.out.println("Got here! 3");
    while (mapCells[arrayPointer]==null){
      arrayPointer++;
      listCounter=0;
            System.out.println("Got here! 4");
      if(mapCells[arrayPointer]!=null){
        tempCells = mapCells[arrayPointer];
        tempStore = listCounter;
        listCounter++;
      }
      System.out.println("ListCounter: " + listCounter);
      System.out.println("Array: " + arrayPointer);
    }
  }
  return (K)tempCells.get(tempStore).getKey();
}

一开始:

MapHashCellList tempCells = null;

然后查看else部分。你有以下循环:

while (mapCells[arrayPointer]==null){

在这个循环中你有这个:

if(mapCells[arrayPointer]!=null){
        tempCells = mapCells[arrayPointer];
        tempStore = listCounter;
        ..................

由于该计划从不进入此if(您的while条件保证),tempCells仍为 null

然后,最后你有以下内容:

return (K)tempCells.get(tempStore).getKey();

必须生成NullPointerException