Announcement

Collapse
No announcement yet.

HashMap->nullpointerExeption

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • HashMap->nullpointerExeption

    ich will in einer Hashtabelle überprüfen ob ein eintrag vorhanden ist:

    if (acknr.get(qdata).equals(null)){ bla bla .....}

    und tollwerweise kommt da eine java.lang.NullPointerException.
    ich dachte eine hashtable ist dazu da um nach einträgen zu suchen können???

  • #2
    hi,

    mal nicht gleich rummaulen und erstmal lesen, das hilft im allgemeinen.

    also, was hast du? ne hashmap oder ne hashtable? gibt es beides.

    für ne Hashtable gilt bei get:
    <code>

    public Object get(Object key)

    Returns the value to which the specified key is mapped in this hashtable.

    Parameters:
    key - a key in the hashtable.
    Returns:
    the value to which the key is mapped in this hashtable; null if the key is not mapped to any value in this hashtable.
    Throws:
    NullPointerException - if the key is null.
    </code>

    oder im Falle der Hashmap gilt:

    <code>
    public Object get(Object key)

    Returns the value to which the specified key is mapped in this identity hash map, or null if the map contains no mapping for this key. A return value of null does not necessarily indicate that the map contains no mapping for the key; it is also possible that the map explicitly maps the key to null. The containsKey method may be used to distinguish these two cases.

    Parameters:
    key - the key whose associated value is to be returned.
    Returns:
    the value to which this map maps the specified key, or null if the map contains no mapping for this key.
    </code>

    solltes du also eine hashtable haben, wäre die nullpointer exception vermutlich richtig und du solltest es evtl. über einen try/catch fangen. wenn es bei der hashmap auftaucht machst du was falsch, schick da doch mal den fehler ausdruck.

    vielleicht hast du die Hash* nicht initialisiert?

    mf

    Comment


    • #3
      Hallo fuzzy,

      ist Dir eigentlich klar, daß Dir das get ein null Object geliefert hat und Du mit diesem ein equal versuchst. Da gibt es immer eine npe.

      Frag einfach mit get(key)!=null ab und dann gehts.

      lg Dietma

      Comment


      • #4
        Eine Map verfügt doch über Methoden, um auf Schlüssel und Werte zu
        prüfen. Etwa :

        if(map.containsKey(key)) {

        Comment

        Working...
        X