0

I am trying to create an object based on a condition, therefore the object creation is within the conditional's scope, however I need to see the object outside of that scope. I thought adding it to a Map would work, but it doesn't. Consider the following example:

TestModel.java

public class TestModel {
private String text;
public void setText(String text){
this.text = text;}
public String getText(){
return this.text;}
}

ScopeTest.java

import java.util.*;
class ScopeTest {

public static void main(String[] args) {

TestModel testModel;
Map<String, Object> myModel = new HashMap<String, Object>();

for (int i=1; i<2; i++){ // if a certain condition is met, create an object as below
    testModel = new TestModel();
    testModel.setText("test text");
    myModel.put("test", testModel);
    }

for (Map.Entry<String, Object> entry : myModel.entrySet()) {
    String key = entry.getKey();
    Object value = entry.getValue();
    System.out.println("key=" + key); // I can see the key value
    System.out.println("value.getText()=" + value.getText()); // but can't see testModel object. I am not sure how to resolve.
}
}
}

cheers,

Geofrey Rainey.

5
  • 1
    What exactly you mean by "can't see myModel object"? Commented Jan 22, 2014 at 10:03
  • 1
    What do you mean "cannot see"? Is there an exception thrown? Commented Jan 22, 2014 at 10:03
  • hm.. I got it ... you are trying to call getText() method on an instance of Object class... Either you should create Map<String, TestModel> in the first place or cast the entry.getValue() as TestModel and change the type of value to TestModel.. Commented Jan 22, 2014 at 10:05
  • do you know that you can access your testModel object directly with out putting it into a map ?? so you don't need the cast operation at all !! Commented Jan 22, 2014 at 10:05
  • Yes an error is thrown for this example as: "cannot find symbol". However the real version of this code is actually written in springmvc 3 and the value of the object is null when printed to a logfile. Commented Jan 22, 2014 at 10:07

4 Answers 4

2

You have to cast the Object value with your Class. Like this.

System.out.println("value.getText()=" + ((TestModel) value).getText());

If you dont want to cast the object then you can use like this.

class ScopeTest {

public static void main(String[] args) {

TestModel testModel;
Map<String, TestModel> myModel = new HashMap<String, TestModel>();//Use TestModel
                                                                instead of object

for (int i=1; i<2; i++){ 
    testModel = new TestModel();
    testModel.setText("test text");
    myModel.put("test", testModel);
    }

for (Entry<String, TestModel> entry : myModel.entrySet()) {
    String key = entry.getKey();
    TestModel value = entry.getValue();
    System.out.println("key=" + key); 
    System.out.println("value.getText()=" + value.getText()); 
}
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comments everyone. It seems you're all correct after I tried it. I will retest in the springmvc code to see if that works too, hopefully it's all good. cheers.
1

You should cast the Object into your model.

TestModel value = (TestModel) entry.getValue();

Comments

0

I think you might have to cast the object returned by the HashMap to be a TestModel before you can use a method of that class.

1 Comment

Right that does make sense, however I am sure I was doing that but let me clarify. This is an example of what is actually written in the springmvc framework.
0

Why not just use TestModel as generic type of value in your Map like below?

Map<String, TestModel> myModel = new HashMap<>();
//          ^^^^^^^^^ - instead of Object

This way you can iterate over your map with

for (Map.Entry<String, TestModel> entry : myModel.entrySet()) {
    ...
}

and you will be able to store value in

TestModel value = entry.getValue();

without needing to cast it. Now since type of value reference will be TestModel compiler will let you use its methods like getText() without problems.

System.out.println("value.getText()=" + value.getText());

Also I am not sure why you are using loop if you want to iterate over it once. Simple if would be better. Another thing is using Map to hold one element seems unnecessary. You can just use one reference like

boolean someCondition = true;

TestModel testModel = null;
if (someCondition) { // if a certain condition is met, create
    testModel = new TestModel();
    testModel.setText("test text");
}

if (testModel!=null){
    System.out.println(testModel.getText());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.