Ahoura Ghotbi
Ahoura Ghotbi

Reputation: 2896

storing and retrieving an instance of a class in a hashtable

So I am trying to store an instance of a class in hash table:

 public void insertEdge(String val1, String val2, int len) {
       Hashtable ref = new Hashtable(10, 3); 

       if(!ref.containsKey(val2))
       {
           Vert temp = new Vert(val2);
            ref.put(val2, temp);
           curNod++;
       }

       ref.get(val1).firstEdge = new Edge(ref.get(val2), ref.get(val1).firstEdge, len);
}

When I run the program above the compiler returns the following error:

error: cannot find symbol

       ref.get(val1).firstEdge = new Edge(ref.get(val2), ref.get(val1).firstEdge, len);

I am still learning hashtables so go easy. from what I understand you should be able to store an instance of a class in hashtable and retrieve it, firstEdge is a variable of my class Vertso I in theory I should technically be able to retrieve it without a problem!

Here is the Vert class

class Vert {
   public Edge firstEdge;
    public String name; 

   public Vert() {
      firstEdge = null;

   }

    public Vert(String n)
    {
        firstEdge = null;
        name = n;   
    }
}

Upvotes: 0

Views: 882

Answers (1)

Syam S
Syam S

Reputation: 8509

You are not annotating your Hashtable. So ref.get(val1) returns an Object class and you are trying to get firstEdge which is not a property of Object class. You'll either have to initialize your hashtable like Hashtable<String, Vert> ref = new Hashtable<String, Vert>(10, 3); or cast ((Vert)ref.get(val1)).firstEdge

Upvotes: 1

Related Questions