Search code examples
javascriptjavaspring-bootarraylistthymeleaf

Why first values in ArrayList always returns null in Java?


while i was coding on my little website-project for school, i ran into a mistake (probably by me) that is driving me crazy...

I got an ArrayList with objects (users) i created... currently 4:

public static ArrayList<Nutzer> users = new ArrayList<Nutzer>();
...
public static void init() {

        Nutzer testNutzer1 = new Nutzer("joey", "Gothra", "10", null);
        Nutzer testNutzer2 = new Nutzer("Badshah", "Gothra", "10", "Test");
        Nutzer testNutzer3 = new Nutzer("Ray", "Gothra", "10", "Baum");
        Nutzer testNutzer4 = new Nutzer("Sven", "Gothra", "10", "Niemand");

        users.add(testNutzer1);
        users.add(testNutzer2);
        users.add(testNutzer3);
        users.add(testNutzer4);

I then create an identical copy of that ArrayList within the Controller which then goes into my DTO as an attribute Value:

@RequestMapping(value = { "/edit" }, method = RequestMethod.GET)
    public String showEditPage(Model model) {
        ArrayList<Nutzer> user = new ArrayList<>(users);
        model.addAttribute("formular", new NutzerErstellenDTO(user));
        return "Edit";
    }
...
DTO
...
public class NutzerErstellenDTO {                               //DATA TRANSFER OBJECT  
    @Autowired
    private ArrayList<Nutzer> besucher;
    public NutzerErstellenDTO(ArrayList<Nutzer> user) {         
        this.besucher = user;                               
    }

It then goes into the "Edit" template and is beeing processed: (sorry for long code)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Edit</title>
</head>
<script th:inline="javascript">
function() sendDaten(){
/*<![CDATA[*/
var data = /*[[${formular.besucher}]]*/
/*]]>*/     
var url = "http://localhost:8080/edit";
var json = JSON.stringify(data);
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
xhr.onload = function () {
    var users = JSON.parse(xhr.responseText);
    if (xhr.readyState == 4 && xhr.status == "200") {
        console.table(users);
    } else {
        console.error(users);
    }
}
xhr.send(json);
}
</script>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-12">                                                             <!-- AHA WTF -->
                <h1>Teilnehmer bearbeiten</h1>
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">                                                      
                <a class="btn btn-info" href="#" th:href="@{/Overview}"> Zurück zur Übersicht </a>
                <form action="#" class="form-horizontal" th:action="@{/edit}"                                       
                    th:object="${formular.besucher}" method="post">                                             
                    <fieldset>
                        <span class="pull-right"> <input onclick="sendDaten()" type="submit" id="submitButton" class="btn btn-success" th:value="Speichern">
                            <input type="reset" id="resetButton" class="btn btn-danger" th:value="Zurücksetzen" />
                             <button onclick="sendDaten();">Click</button>
                        </span>
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Vorname</th>
                                    <th>Nachname</th>
                                    <th>Karten</th>
                                    <th>Wunschsitzpartner</th>              
                                </tr>
                            </thead>
                            <tbody>
                                <tr th:each="u, itemStat : ${formular.besucher}">
                                    <td><input 
                                        th:placeholder="Vorname"
                                        th:name="|besucher[${itemStat.index}].vorname|"
                                        th:value="${u.getVorname()}" required /></td>
                                    <td><input
                                        th:placeholder="Nachname"
                                        th:name="|besucher[${itemStat.index}].nachname|"
                                        th:value="${u.getNachname()}" required /></td>
                                    <td><input
                                        th:placeholder="Karten"
                                        th:name="|besucher[${itemStat.index}].karten|"
                                        th:value="${u.getKarten()}" required /></td>
                                    <td><input
                                        th:placeholder="Wunschsitzpartner"
                                        th:name="|besucher[${itemStat.index}].sitzpartner|"
                                        th:value="${u.getSitzpartner()}"/></td>   
                                </tr>
                            </tbody>
                        </table>

                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

it all just works out pretty fine when it gos back into the Controller with a post request (Any tips on how i could turn it into a PUT request are much appreciated (points in the test xD))

    @RequestMapping(value = "/edit", method = RequestMethod.POST, consumes = "application/x-www-form-urlencoded")
    public String updateTest(NutzerErstellenDTO dto)
        List<Nutzer> test = dto.getBesucher();
        for (Nutzer nutzer : test) {
            System.out.println("test: " + nutzer.getVorname() + nutzer.getNachname() + nutzer.getKarten() + nutzer.getSitzpartner());
        }

At this point, its already lost as you can see here:

test: nullGothraTestEditWorksFine10*edited

The first Value is always "null" and i don't know why because everything else just works fine...

ANY tips or solutions to this? i already googled a ton and have found nothing.

EDIT: This is my "Nutzer" class:

public class Nutzer {

    private String vorname;
    private String nachname;
    private String karten;
    private String sitzpartner;

    public Nutzer() {   
    }
    public Nutzer(String vorname, String nachname, String karten, String sitzpartner) {
        super();
        this.vorname = vorname;
        this.nachname = nachname;
        this.karten = karten;
        this.sitzpartner = sitzpartner;
    }
    public void display() {
        System.out.println(vorname + nachname + karten + sitzpartner);
    }
    public String getVorname() {
        return vorname;
    }
    public void setVorame(String vorname) {
        this.vorname = vorname;
    }
    public String getNachname() {
        return nachname;
    }
    public void setNachname(String nachname) {
        this.nachname = nachname;
    }
    public String getKarten() {
        return karten;
    }
    public void setKarten(String karten) {
        this.karten = karten;
    }
    public String getSitzpartner() {
        return sitzpartner;
    }
    public void setSitzpartner(String sitzpartner) {
        this.sitzpartner = sitzpartner;
    }

Solution

  • If this is your real Nutzer class (and not some edited version that contains additional errors) then your problem is most probably the setter for the vorname field:

    public String getVorname() {
        return vorname;
    }
    public void setVorame(String vorname) {
        this.vorname = vorname;
    }
    

    The setters name is misspelled: you named it setVorame() when it should be named setVorname()