1

I have a Spring-Boot with Mongo application where I am trying to persist a list of object.

I have a method which parses some json data and creates a list of objects. I then call the save method of MongoRepository to save it all in a go.

The Repository code looks like:

public interface TicketRepository extends MongoRepository<Ticket, String> {

}

And Ticket object POJO is:

@Document
public class Ticket {

    @Id
    private String id;
    private String topic;
    private String type;
    private long brand;
    private long group;
    private String priority;
    private String status;
    private String created_date;
    private String created_time;
    private String channel;

    public Ticket() {
    }

    //.........getters and setters....

}

Now, some of the fields for some objects would be null as well because I do not set any values for them.

And finally, this is how I save them:

@RestController
public class TicketController {

    @Autowired
    TicketRepository ticketRepository;

    @GetMapping("/tickets")
    public void saveTicketData() {
        List<Ticket> tickets = null;
        try {
            tickets = getTicketObjectList(ticketJson);
            ticketRepository.save(tickets);
        } catch (DuplicateKeyException e) {
            System.out.println("duplicate found!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

This throws NullPointerExcetion on save method call without any further stacktrace.

What could be the cause?

Abdullah Khan
  • 12,010
  • 6
  • 65
  • 78
roger_that
  • 9,493
  • 18
  • 66
  • 102

2 Answers2

1

the problem i think in autowiring not in @Repository or even in EnableJpaRepositories. according to this answer

Spring data repository works without annotations you willn't need to mention annotation because it is auto configuration in starting point , autowiring like this

@Autowired
TicketRepository ticketRepository;

//getter and setter 
Shaaban Ebrahim
  • 9,766
  • 1
  • 18
  • 20
  • 1
    Its not mandatory i believe!! You can also use `@EnableJpaRepositories(basePackages="org.my.pkg")`. – Abdullah Khan Nov 19 '18 at 08:27
  • @EnableJpaRepositories is for just component scan for different packages (different packages mean that these packages are not under the same that contains your starting point main method ) , but Repository or controller or component or service is marking that there will be created from this , so in our case i should put annotation for this to enable creating object to be usable – Shaaban Ebrahim Nov 19 '18 at 08:29
  • I figured out the solution. Actually, the `TicketRepository` bean was not getting autowired somehow. It works with or without the annotation. – roger_that Nov 19 '18 at 08:40
  • @roger_that how can you use its object without creating this object ?? – Shaaban Ebrahim Nov 19 '18 at 08:43
0

Have you tried running the application in debug, and breaking at the point where you call ticketRepository.save(tickets)?

It is possible that the tickets List is null. You haven't show what the getTicketObjectList method does, but it could be returning a null object, rather than an actual List. Therefore the NPE could be because you are trying to save a null.

Alternatively it could be that the ticketRepository is null, and has not been auto wired correctly, but I think you would have had an exception from spring when the application starts up if that were the case.

robjwilkins
  • 5,462
  • 5
  • 43
  • 59