NullPointerException on Button Clicks

Hey

I am getting null pointer exception after clicking on a button

I have added a click event event for button which will trigger service.uploadPhoto() method and the method will do repository.save(photo)

I dont know why I am getting null pointer tho

here is it

Showing us the stracktrace without code doesn’t really help us to help you :wink:

I will share my code soon

Any time you share code or stack traces, please share the actual text in a code block so it’s easier to read and search

```

```

Ok

Navbar.java

public class Navbar extends AppLayout{

    @Autowired
    PhotoService service;

    TextField name = new TextField();
    TextField author = new TextField();
    TextField tag = new TextField();
    byte[] file;

    Photo photo = new Photo();

    MemoryBuffer buffer;
    Upload upload;
    
    public Navbar(){
        createNavBar();
    }
    
    private void createNavbar(){...}

    private VerticalLayout createDialogueLayout(){...}

the code for both methods are below:

private void createNavBar() {
        H1 title = new H1("PhotosUp");
        title.addClassNames("text-l","m-m");
        
        Dialog form = new Dialog();
        Button addButton = new Button("Add", new Icon(VaadinIcon.PLUS));
        addButton.addThemeVariants(ButtonVariant.LUMO_ICON);
        addButton.addClassName("m-s");
        addButton.addClickListener(e -> form.open());

        HorizontalLayout layout = new HorizontalLayout(title,addButton);
        layout.setVerticalComponentAlignment(Alignment.CENTER);
        layout.expand(title);
        layout.setWidth("100%");
        layout.addClassNames("py-0","px-m");
        addToNavbar(layout);

        form.setHeaderTitle("Add Images");
        VerticalLayout formLayout = createDialogueLayout();
        form.add(formLayout);
        Button saveButton = new Button("Add", e->{
            photo.setAuthor(author.getValue());
            photo.setName(name.getValue());
            photo.setTag(tag.getValue());
            photo.setPhoto(file);
            photo.setId(new ObjectId().toString());
            System.out.println(photo.getPhoto().length);
            System.out.println(photo.getId());
            try {
                System.out.println(photo.getAuthor());
                System.out.println(file);
                System.out.println(photo.getId());
                String id = service.addPhoto(photo);
                System.out.println(id);
                form.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        });

        Button cancelButton = new Button("Cancel", e -> {
            name.clear();
            author.clear();
            tag.clear();
            upload.clearFileList();
            form.close();
        });
        
        form.getFooter().add(cancelButton);
        form.getFooter().add(saveButton);
    }
private VerticalLayout createDialogueLayout(){
        name = new TextField("Image Title");
        author = new TextField("Author");
        tag = new TextField("Tags");

        buffer = new MemoryBuffer();
        upload = new Upload(buffer);

        upload.addClassName(Margin.Top.MEDIUM);

        upload.addSucceededListener(e -> {
            String fileName = e.getFileName();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            try {
                ImageIO.write(ImageIO.read(buffer.getInputStream()),"png",stream);
                file = stream.toByteArray();
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            System.out.println(fileName);
        });

        VerticalLayout dialogLayout = new VerticalLayout(name,
                author,tag, upload);
        dialogLayout.setPadding(false);
        dialogLayout.setSpacing(false);
        dialogLayout.setAlignItems(Alignment.STRETCH);
        dialogLayout.getStyle().set("width", "18rem").set("max-width", "100%");

        return dialogLayout;
    }

ignore sysyout lines thats for debugging

shall i share service, repository class codes too?

https://pastebin.com/FwA2xgsV

this contains code for service, repository

the null pointer exception was due to repo interface what can I do for it?

I have autowired it still

Autowiring only works on spring managed beans - I don’t see any annotations on the Navbar, letting me think that you instantiate it with “new Navbar()” which doesn’t work