"cannot find symbol method getMyVal()"

I am working on a project, and in testing, I came across the error in the title. I just cannot understand why I am getting it, though! Perhaps there is some coding nuance I am forgetting?

import java.util.*;

import java.io.*;

publicclass Mapping{

privateint myValue;

public Mapping (int yay){

myValue = yay;

}

publicint getMyVal(){

return myValue;

}

}

and my second class:

import java.util.*;

import java.io.*;

publicclass MappingTester{

publicstaticvoid main (String args[]){

HashMap map =new HashMap();

for (int i = 0; i<3; i++){

Mapping obj =new Mapping(i);

map.put(i, obj);

}

Object a = map.get(0);

Object b = map.get(1);

Object c = map.get(2);

System.out.println("The first obj has a value of "+ a.getMyVal());

System.out.println("The second obj has a value of "+ b.getMyVal());

System.out.println("The third obj has a value of "+ c.getMyVal());

}

}

I realize it's not very elegant/OO, but right now I'm just trying to get it to work. :)

Any help is appreciated!

[2291 byte] By [amysendersa] at [2007-11-27 4:54:52]
# 1

You have to cast a, b, and c back into a Mapping object. The Object class doesn't have the method getMyVal(), but the Mapping class does, and you're handling a, b, and c as Objects, not Mappings.

Mapping a = (Mapping) map.get(0);

Or you could parametrize the Map.

HashMap<Integer, Mapping> map = new HashMap<Integer, Mapping>();

One more thing... if all your keys are integers, then I don't see why you're using a Map when you could use a List or Set.

ktm5124a at 2007-7-12 10:09:40 > top of Java-index,Java Essentials,New To Java...