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?

3
  • 1
    interface should be annotated with @Repository Commented Nov 19, 2018 at 8:10
  • I figured out the solution. Actually, the TicketRepository bean was not getting autowired somehow. Commented Nov 19, 2018 at 8:40
  • @roger_that : Hey I'm facing the same issue. Could you please let me know how you fixed it? Commented Oct 7, 2019 at 11:40

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

4 Comments

Its not mandatory i believe!! You can also use @EnableJpaRepositories(basePackages="org.my.pkg").
@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
I figured out the solution. Actually, the TicketRepository bean was not getting autowired somehow. It works with or without the annotation.
@roger_that how can you use its object without creating this object ??
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.

1 Comment

I figured out the solution. Actually, the TicketRepository bean was not getting autowired somehow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.