-1

I'm trying to insert object that I created in a List to display them in a table thanks to Javascript.

However the probleme is that the element of the object is registered in the list like this (display debug javascript) :

  1. listAnomalies: Array(18)
    • 0: "779"
    • 1: "2019/11/16"
    • 2: "test3"
    • 3: "test3"
    • 4: "test3"
    • 5: "2020/01/01"
    • 6: "778"
    • 7: "2019/09/28"
    • 8: "test2"
    • 9: "test2"
    • 10:"test2"

This is where I created the List :

final AnomalieJira returnData = new AnomalieJira();

    final List<SaisieAnomalieProjetVo> project1 = new ArrayList(Arrays.asList("777", "2019/01/01", "test", "test", "test", "2020/01/01"));

    final List<SaisieAnomalieProjetVo> project2 = new ArrayList(Arrays.asList("778", "2019/09/28", "test2", "test2", "test2", "2020/01/01"));

    final List<SaisieAnomalieProjetVo> project3 = new ArrayList(Arrays.asList("779", "2019/11/16", "test3", "test3", "test3", "2020/01/01"));

    final List<SaisieAnomalieProjetVo> projet = new ArrayList<SaisieAnomalieProjetVo>();
    projet.addAll(project3);
    projet.addAll(project2);
    projet.addAll(project1);

    //final List<SaisieAnomalieProjetVo> listAnomalies = jiraDao.getWorkIssueSP(id, datasource, jira);
    returnData.setListAnomalies(projet);
    //returnData.getListAnomalies().addAll(project2);

    returnData.setUrl(datasource.getUrl());
    returnData.setPsw(datasource.getPassword());
    returnData.setUser(datasource.getUsername());

            return returnData;

}

My class AnomalieJira :

public class AnomalieJira {


/**
 * Liste des différents info travaux
 */
private List<SaisieAnomalieProjetVo> listAnomalies;

private String url;
private String psw;
private String user;
}

And my List SaisieAnomalieProjetVo :

public class SaisieAnomalieProjetVo extends AbstractAudited<Integer> {

private static final long serialVersionUID = 1L;

@NotNull
private int projet;

@NotNull
private Date date;

@NotNull
@NotEmpty
private String type;

@NotNull
@NotEmpty
private String reference;

private String description;

private Date dateRea;
}

I truggled to get the display like this :

  1. listAnomalies: Array(18)
    • 0: "779", "2019/11/16", "test3", "test3", "test3", "2020/01/01"
    • 1: "778", "2019/09/28", "test2", "test2", "test2", "2020/01/01"
    • 2: .....

This display have more sense than the first one but I can't get this format.

ISSOU
  • 27
  • 7
  • You've mentioned javascript a few times in your question, but there's only java code here. Did you mean java? – byxor Sep 12 '19 at 14:37
  • no, the javascript was only for the display but i could say java for the display when I debug it. The result is the same, the new object goes in the existing one rather than a new one. – ISSOU Sep 12 '19 at 14:41

2 Answers2

0

When you add the elements to List using Arrays.asList, it creates list of elements those are being passed as input parameters, rather than the List of SaisieAnomalieProjetVo. But you need to create the Object of SaisieAnomalieProjetVo before adding it to List.

First add constructor to the SaisieAnomalieProjetVo as below:

public class SaisieAnomalieProjetVo extends AbstractAudited<Integer> {

private static final long serialVersionUID = 1L;

@NotNull
private int projet;

@NotNull
private Date date;

@NotNull
@NotEmpty
private String type;

@NotNull
@NotEmpty
private String reference;

private String description;

private Date dateRea;



public SaisieAnomalieProjetVo(final int project, final Date date, final Date dateRea, final String reference,
      final String description, final String type) {
    this.projet = project;
    this.date = date;
    this.dateRea = dateRea;
    this.reference = reference;
    this.description = description;
    this.type = type;
}
}

And then before adding to list Create Object of SaisieAnomalieProjetVo as below (assuming that Date format is "yyyy/MM/dd":

final SimpleDateFormat parser= new SimpleDateFormat("yyyy/MM/dd");

    final List<SaisieAnomalieProjetVo> project1 = new ArrayList(Arrays.asList(
        new SaisieAnomalieProjetVo(777, parser.parse("2019/01/01"), parser.parse("2020/01/01"), "test", "test", "test")));
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • hi, trying this but having an issue with the date : Unparseable date – ISSOU Sep 13 '19 at 07:13
  • Can you please paste logs for that? Or what String you are passing to parse as date? – Gaurav Jeswani Sep 13 '19 at 07:35
  • java.lang.NoSuchMethodError: fr.gfi.ppm.resource.ppm.groupama.SaisieAnomalieProjetVo.(ILjava/util/Date;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/Date;)V – ISSOU Sep 13 '19 at 07:38
  • This is different from Unparseable date. Anyways you are getting this error because the Constructor in SaisieAnomalieProjetVo is defined with a set of parameters, while you are passing different type of set of parameters to it. So it is not able to find constructor with those parameters. Lets say we have constructor with parameters (int, date, date, string, string, string) while you are passing parameter as (date, string, string, string, date). – Gaurav Jeswani Sep 13 '19 at 07:41
  • It can also happen due to https://stackoverflow.com/questions/35186/how-do-i-fix-a-nosuchmethoderror – Gaurav Jeswani Sep 13 '19 at 07:42
  • i know the probleme is i'm passing the right parameters – ISSOU Sep 13 '19 at 07:43
  • i'm passing this parameters : final List project1 = new ArrayList<>(Arrays.asList(new SaisieAnomalieProjetVo(777, parser.parse("2019-01-01"), "test", "test", "test", parser.parse("2020-01-01")))); – ISSOU Sep 13 '19 at 08:01
  • so it's (int, Date, String, String, String, Date) why it is not recognizing the first parameters 'int' ? – ISSOU Sep 13 '19 at 08:02
0

Ok, i found my problem, the list is displaying like I wanted.

 final List<SaisieAnomalieProjetVo> project1 = new ArrayList<>(Arrays.asList(new SaisieAnomalieProjetVo(parser.parse("2019-01-01"), 777, "test", "test", "test", parser.parse("2020-01-01"))));

    final List<SaisieAnomalieProjetVo> project2 = new ArrayList<>(Arrays.asList(new SaisieAnomalieProjetVo(parser.parse("2019-09-28"), 778, "test2", "test2", "test2", parser.parse("2020-01-01"))));

    final List<SaisieAnomalieProjetVo> projet = new ArrayList<SaisieAnomalieProjetVo>();

    projet.addAll(project2);
    projet.addAll(project1);

    returnData.setListAnomalies(projet);

I created the constuctor and I inserted this construtor in a list then created another list of 'projet' and inserted again all the 'projet'. ThenIi return the list of the 'ListAnomalies'

ISSOU
  • 27
  • 7