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!