-1

I am getting this error as follows :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at recipes.presenter.InsertPresenter.insertNewEntry(InsertPresenter.java:33) at recipes.view.InsertView.jButton1ActionPerformed(InsertView.java:229) at recipes.view.InsertView.access$000(InsertView.java:14) at recipes.view.InsertView$1.actionPerformed(InsertView.java:124)

The code in InsertPresenter file is as follows :

package recipes.presenter;

import javax.swing.JOptionPane;
import recipes.model.*;
import recipes.view.*;

public class InsertPresenter {

    private IInsertView iInsertView;
    InsertView iv;
    IRecipeQuery query;

    public InsertPresenter() {
        iInsertView = null;
        iv =  new InsertView();
    }

    public void bind(IInsertView iv) {
        iInsertView = iv;
    }

    public void insertNewEntry() {
        int result = 0;
        result = query.addRecipe(iv.getNameField(),iv.getCategoryField(),iv.getMainIngField(),
                iv.getPrepTimeField(),iv.getCookTimeField());
        if (result == 1) {
            JOptionPane.showMessageDialog(null, "Recipe added!");
        } else {
            JOptionPane.showMessageDialog(null, "Recipe not added!");
        }
    }
}

Removing the iInsertView = null also didn't help. I searched for multiple answers here but they mostly asked to not initialize the variable to object to null and I tried that as well without any luck.

Could anyone please help, what am I doing wrong here?

Update:

I tried the following way as well to initalize 'query' without any luck.

package recipes.presenter;

import java.util.Objects;
import javax.swing.JOptionPane;
import recipes.model.*;
import recipes.view.*;

public class InsertPresenter {

    IInsertView view;
    IRecipeQuery queries;

    InsertPresenter(IInsertView iiv, IRecipeQuery iqv) {
        view = iiv;
        queries = iqv;
    }

    public InsertPresenter() {
    }

    public void bind(IInsertView iv) {
        view = iv;
    }

    public void insertNewEntry() {
        int result = 0;
        result = queries.addRecipe(view.getNameField(), view.getCategoryField(), view.getMainIngField(),
                view.getPrepTimeField(), view.getCookTimeField());
        if (result == 1) {
            JOptionPane.showMessageDialog(null, "Recipe added!");
        } else {
            JOptionPane.showMessageDialog(null, "Recipe not added!");
        }
    }
}

Please help!

Keyur Amin
  • 45
  • 1
  • 1
  • 8
  • 1
    Is `query` initialized anywhere? – Thomas Oct 09 '15 at 12:01
  • where did youn create query? – nano_nano Oct 09 '15 at 12:02
  • `I searched for multiple answers here but they mostly asked to not initialize the variable to object to null and I tried that as well without any luck.` - any uninitialized field (class or instance variable) which references an object will automatically be initialized to null, so no difference here. – Thomas Oct 09 '15 at 12:02
  • What should I initialize it with because it is of type IRecipeQuery and as I have seen some answers, I should not initialize it will null. – Keyur Amin Oct 09 '15 at 12:10
  • Show how you create the class InsertPresenter, Looks like a parameter is null. – Jens Oct 09 '15 at 18:32

1 Answers1

0

query is never initialized. You have to initialize it.

Jens
  • 67,715
  • 15
  • 98
  • 113