-2

My java class is throwing some error. In my class i am using this to get my data.

((myDataDetails) Names.get(0)).InputParamNames().add("SomeValue");

But it is throwing error

Here is my Pohjo Class.

 package common.pojo;

import java.util.Date;
import java.util.List;

public class myDataDetails 
{
    private String myID;
    private List<String> InputParamNames;
    private List InputParamData;

    public String getmyID() {
        return this.myID;
    }

    public void setmyID(String myID) {
        this.myID = myID;
    }

    public List<String> getInputParamNames() {
        return this.InputParamNames;
    }

    public void setInputParamNames(List<String> InputParamNames) {
        this.InputParamNames = InputParamNames;
    }

    public List getInputParamData() {
        return this.InputParamData;
    }

    public void setInputParamData(List InputParamData) {
        this.InputParamData = InputParamData;
    }

}

What should I need to change in pojo to avoid this exception.

David
  • 4,266
  • 8
  • 34
  • 69
  • I cant see any method name InputParamNames() in POJO class. What is the error you getting? – pavithraCS Jul 09 '18 at 03:54
  • `(myDataDetails) Names.get(0)` -> What is the type of **`Names`** here? – Romeo Sierra Jul 09 '18 at 03:55
  • @RomeoSierra type is `String` here. – David Jul 09 '18 at 03:56
  • Are you sure? Because it doesn't make sense with the question title then... – Romeo Sierra Jul 09 '18 at 03:57
  • @RomeoSierra I am sorry. Type is object. – David Jul 09 '18 at 04:03
  • If that is `Object`, then you are trying to cast down which won't work as you expect. There is a [nice explanation](https://stackoverflow.com/questions/23414090/what-is-the-difference-between-up-casting-and-down-casting-with-respect-to-class#23414798) in here, on that. What is it that you are trying to achieve with this statement `((myDataDetails) Names.get(0)).InputParamNames().add("SomeValue");` exactly? I mean what do you want to get done? – Romeo Sierra Jul 09 '18 at 04:05

1 Answers1

-2

Your class 'myDataDetails' needs to extend from LinkedHashMap in order to cast it.

What you have right now is a regular POJO class that is not an instance of LinkedHashMap, so you can't cast it as such.

EDIT: It should look like this

package common.pojo;

import java.util.Date;
import java.util.List;
import java.util.LinkedHashMap;

public class myDataDetails extends LinkedHashMap<Object, Object> 
{
    private String myID;
    private List<String> InputParamNames;
    private List InputParamData;

    public String getmyID() {
        return this.myID;
    }

    public void setmyID(String myID) {
        this.myID = myID;
    }

    public List<String> getInputParamNames() {
        return this.InputParamNames;
    }

    public void setInputParamNames(List<String> InputParamNames) {
        this.InputParamNames = InputParamNames;
    }

    public List getInputParamData() {
        return this.InputParamData;
    }

    public void setInputParamData(List InputParamData) {
        this.InputParamData = InputParamData;
    }

}
Nick H
  • 8,897
  • 9
  • 41
  • 64
  • How to extend to `LinkedHashMap` – David Jul 09 '18 at 03:55
  • @hitch.united can you please add `@Override` to appropriate functions just for clarity. – Shanu Gupta Jul 09 '18 at 04:00
  • @ShanuGupta Can you be more specific? You can extend LinkedHashMap without performing any overrides. I suppose I could, but which ones? – Nick H Jul 09 '18 at 04:03
  • @hitch.united Nevermind, I thought there were some methods that needs to be overridden. – Shanu Gupta Jul 09 '18 at 04:05
  • @hitch.united thanks for your answer. i am building my app with this extends. My question is where we are using ` LinkedHashMap` I am using `List` in parameter but don't know where i m using ` LinkedHashMap ` AM i missing anything. – David Jul 09 '18 at 04:13
  • 1
    @David The problem is 'Names.get(0)' is returning an instance of LinkedHashMap and you are trying to cast it to 'myDataDetails' which is not an instance of LinkedHashMap' (Which is why you are seeing that exception). I would need to see more of the project. – Nick H Jul 09 '18 at 04:19