1

I have been banging my head against a wall for the past few hours. I am trying to write a post method for a site, it have been giving me nothing but errors, the latest being a NullPointer Exception. Here are the relevant models (excluding getters and setters:

Translation:

@Entity
@Table(name = "translations")
public class Translation {
    @Id
    @GeneratedValue
    private long id;

    @OneToOne
    private User user;

    @Column(length = 9999)
    private String userTranslation;

    @Column(columnDefinition = "text")
    private String description_of_changes;

    @Column (nullable = false)
    private boolean flag_problem;

    @Column(columnDefinition = "text")
    private String reason;

    @OneToOne
    private Request request;

    @OneToOne
    private Translation_Status status;

Request:

@Entity
@Table(name = "requests")
public class Request {
    @Id
    @GeneratedValue
    private long id;

    @Column(unique = true, length = 1000)
    private String web_page;

    @Column(columnDefinition = "text")
    private String untranslated_text;

    @Column(columnDefinition = "text")
    private String description;

    @OneToOne
    private Department department;

    @OneToOne
    private User user;

    @Column(columnDefinition = "text")
    private String google_translate;

    @OneToOne
    private Request_Status status;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column
    private Date time;

Here is the html for the template and form:

<div class="form-group">
        <form id="translateForm" th:action="@{/translate}" th:method="post" th:object="${newTranslation}">
            <label for="untranslatedText">Untranslated Text</label>
            <br/>
            <textarea id="untranslatedText" name="untranslatedText" rows="10" th:text="${translationRequest.getUntranslated_text()}" readonly="readonly"></textarea>
            <br/>
            <label for="translatedText">Google Translated Text; Please make edits here</label>
            <br/>
            <textarea name="translatedText" id="translatedText"  rows="10" th:text="${translationRequest.getGoogle_translate()}" th:field="*{userTranslation}"></textarea>
            <label for="description">Please describe any changes you made</label>
            <textarea name="description" id="description" rows="10" th:field="*{description_of_changes}"></textarea>
            <label for="flagProblem">Are there any severe problems with the Google Translations?</label>
            <select name="flagProblem" id="flagProblem" th:field="*{flag_problem}">
                <option value="true">Yes</option>
                <option value="false">No</option>
            </select>
            <br/>
            <!--TODO Look into a hidden input that becomes unhidden if above input is true-->
            <label for="reason">If you answered yes to the above question please explain why</label>
            <input type="text" id="reason" name="reason" th:field="*{reason}"/>
            <input type="hidden" id="refRequest" name="refRequest" th:value="${translationRequest.id}" th:field="*{request}"/>
            <input type="submit" class="btn btn-block btn-primary" value="Submit Translation"/>
        </form>
    </div>

And here is the post method:

@PostMapping("/translate")
public String submitTranslation(@ModelAttribute Translation newTranslation){
    User testUser = userDao.findOne(1L);
    newTranslation.setUser(testUser);
    Translation_Status translationStatus = translationStatusDao.findOne(101L);
    newTranslation.setStatus(translationStatus);
    long time = date.getTime();
    Timestamp ts = new Timestamp(time);
    newTranslation.setTime(ts);
    Long id = newTranslation.getRequest().getId();
    System.out.println(id);
    Request change = requestDao.findOne(id);
    Request_Status newStatus = requestStatusDao.findOne(201L);
    change.setStatus(newStatus);
    Translation savedTranslation = translationDao.save(newTranslation);
    Request changedRequest = requestDao.save(change);
    return "index";
}

My IDE is telling me that the error is occurring when I try and get newTranslation.getRequest().getId(); but I don't know why as I can call any other part of my translation object with no problems. Could someone please help me figure out what I am doing wrong?

Morteza Jalambadani
  • 1,881
  • 5
  • 19
  • 30
Ryan
  • 141
  • 7
  • *the error is occurring when I try and get newTranslation.getRequest().getId();* either newTranslation is null or getRequest returns null – Antoniossss Mar 17 '19 at 21:16
  • You never initialize the request of the translation. So it's null.So calling newTranslation.getRequest().getId() tries to call getId() on a null reference. So you get a NullPointerException. – JB Nizet Mar 17 '19 at 21:18
  • I'm sorry but I don't understand how it is not being initialized, translationRequest is a request that is coming directly from the Getmapping before this, it is being called correctly by everything else that uses it, why is it not being initialized here? – Ryan Mar 17 '19 at 21:21

0 Answers0