Answer the question
In order to leave comments, you need to log in
Why am I getting a NullPointerException?
For some reason, I get a NullPointerException
complaining about the interface method of the findById() repository
(THIS IS NOT BECAUSE OF LONG, I TRIED Integer, my method is highlighted in red)
Repositories:
package com.blrmyfc.repos;
import com.blrmyfc.domain.XmlFileEntity;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
public interface XmlFileRepo extends CrudRepository<XmlFileEntity, Integer> {
XmlFileEntity findById(Long id);
// XmlFileEntity findOne(int id);
// XmlFileEntity findByFilename(String filename);
}
package com.blrmyfc.domain;
import org.hibernate.validator.constraints.Length;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class XmlFileEntity {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
private String fileName;
private String fileLink;
private String valid;
@Length(max = 4096)
private String fileStrings;
public String getValid() {
return valid;
}
public void setValid(String valid) {
this.valid = valid;
}
public String getFileStrings() {
return fileStrings;
}
public void setFileStrings(String fileStrings) {
this.fileStrings = fileStrings;
}
public XmlFileEntity() {
}
public XmlFileEntity(String fileName, String fileLink) {
this.fileName = fileName;
this.fileLink = fileLink;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileLink() {
return fileLink;
}
public void setFileLink(String fileLink) {
this.fileLink = fileLink;
}
}
package com.blrmyfc.controller;
import com.blrmyfc.domain.XmlFileEntity;
import com.blrmyfc.logic.SchemaValidator;
import com.blrmyfc.logic.XmlViewer;
import com.blrmyfc.repos.XmlFileRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.SAXException;
import java.io.IOException;
//@Controller
@RestController
public class ShowController {
@Value("${upload.path}")
private String uploadPath;
@Autowired
private XmlFileRepo xmlFileRepo;
@PostMapping("/validate")
public String validateFiles(@RequestParam("xmls") String xmls, @RequestParam("xsds") String xsds, Model model){
SchemaValidator schemaValidator = new SchemaValidator();
boolean flag = true;
try{
schemaValidator.validate(uploadPath+"/"+(xmlFileRepo.findById(Integer.parseInt(xmls)).get().getFileLink()),
uploadPath+"/"+(xmlFileRepo.findById(Integer.parseInt(xsds)).get().getFileLink()));
// schemaValidator.validate("./upload-dir/students.xml",
// "./upload-dir/students.xsd");
}catch (SAXException e){
flag=false;
e.printStackTrace();
}catch (IOException e){
flag=false;
e.printStackTrace();
}catch (NullPointerException e){
flag=false;
e.printStackTrace();
}
if(flag){
XmlFileEntity xmlFileEntity = xmlFileRepo.findById(Integer.parseInt(xmls)).get();
xmlFileEntity.setValid("true");
XmlViewer xmlViewer = new XmlViewer();
xmlFileEntity.setFileStrings(xmlViewer.getStrings(uploadPath+"/"+(xmlFileRepo.findById(Integer.parseInt(xmls)).get().getFileLink())));
xmlFileRepo.save(xmlFileEntity);
}else {
xmlFileRepo.findById(Integer.parseInt(xmls)).get().setValid("false");
}
return "Валидация: "+flag;
// return "Воть (xmls)(xsds)==>"+xmls+xsds+"+_=-=-=-"+uploadPath+"/"+(xmlFileRepo.findById(Integer.parseInt(xmls)).get().getFileLink() )+"-------"+uploadPath+"/"+(xmlFileRepo.findById(Integer.parseInt(xsds)).get().getFileLink());
}
}
package com.blrmyfc.logic;
import com.blrmyfc.repos.XmlFileRepo;
import org.springframework.beans.factory.annotation.Autowired;
public class ValidateFromDBAndSave {
@Autowired
public XmlFileRepo xmlFileRepo;
public String printXmlFileStringFromDB(){
String xmlFileString="";
System.out.println(xmlFileRepo.findById(13).get().getFileStrings());
return xmlFileString;
}
}
Answer the question
In order to leave comments, you need to log in
We need to initialize the repository like this:
thanks for the help
@Service
public class ValidateFromDBAndSave {
private final XmlFileRepo xmlFileRepo;
@Autowired
public ValidateFromDBAndSave(XmlFileRepo xmlFileRepo) {
this.xmlFileRepo = xmlFileRepo;
}
public String printXmlFileStringFromDB(){
String xmlFileString="";
xmlFileString=xmlFileRepo.findById(13).get().getFileStrings();
return xmlFileString;
}
}
@GetMapping("/soli")
public String soli(){
return new ValidateFromDBAndSave(xmlFileRepo).printXmlFileStringFromDB();
// return xmlFileRepo.findById(13).get().getFileStrings();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question