samedi 20 décembre 2014

How to solve the problem of java,lang.NullPointerException in MVC based java program


I am creating a MVC based simple addition program in Java for learning purposes. It's like a calculator with three text fields and a button. In two fields we enter two numbers to add and on clicking the button the result is displayed in third field (Similar to this: A + B = C). Here, A and B are those two input field and C is the text field where result is displayed. It compiled successfully but showing an error during run time, which is as follow:



Exception in thread "main" java.lang.NullPointerException
at CalculatorView.launchUI(CalculatorView.java:42)
at CalculatorController.<init>(CalculatorController.java:12)
at Calculator.main(Calculator.java:5)


I am developing it in NetBeans IDE and tried to insert break-points at respective lines shown in error message to observe what was happening. The observation was something like this:


program control jumps from line 5 in Calculator.java to line 12 CalculatorController.java from there it goes to line 42 in CalculateView.java and then suddenly error is shown! I tried to comment that line then the error is shown like this:



..at CalculatorView.launchUI(CalculatorView.java:43)


I tried commenting this line also but then the next line is shown in error. I was able to comment these three lines because they were statements about the layout which are not affecting the internal logic and overall execution of program. I spent hours and could not find out what is actually wrong with this program. I tried to read about exception handling but still got failed in finding the problem.


Note: I am beginner in Java and knows nothing about exception handling and debugging process in particular.


I am uploading my code here. I have made 4 files: CalculatorModel.java, CalculatorView.java, CalculatorController.java and Calculator.java (main is located here). I am merging the code of all 4 files together and marking down when one file ends and other begins...


Note: The order of merging these programs are as follow:



  1. Import statements

  2. CalculatorModel.java

  3. CalculatorView.java (Error Line 42 is here, I've mentioned it in comment)

  4. CalculatorController.java (Error Line 12 is here)


  5. Calculator.java (Error Line 5 is here)



    import java.awt.*;
    import java.awt.event.*;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;


    /********* CalculatorModel.java BEGINS **********/

    public class CalculatorModel{
    private double numOne;
    private double numTwo;
    private double addResult;

    public void addition(double one, double two){
    addResult = one + two;
    }

    public double getAddResult(){
    return addResult;
    }
    }

    /******** ENDOF CalculatorModel.java **********/

    /********* CalculatorView.java BEGINS **********/

    public class CalculatorView{
    //Declaring refrences for components to use
    private Frame f;

    private TextField inputFieldOne;
    private TextField inputFieldTwo;
    private TextField inputFieldThree;

    private Label labelPlus;
    private Label labelEquality;

    private Panel rowOne;
    private Panel rowTwo;

    private Button buttonAdd;

    //Parameterized constructor to initialize refrences with their respective objects
    public CalculatorView(String title){
    f = new Frame(title);

    inputFieldOne = new TextField(10);
    inputFieldTwo = new TextField(10);
    inputFieldThree = new TextField(10);

    labelPlus = new Label("+");
    labelEquality = new Label("=");

    buttonAdd = new Button("Add");
    }

    //Method to launch the User Interface/UI
    public void launchUI(){

    //Setting the layout
    f.setLayout(new BorderLayout());
    rowOne.setLayout(new GridLayout(1, 5)); //<----THIS IS LINE 42
    rowTwo.setLayout(new FlowLayout());

    //Adding the components to their respective layout
    //Adding input fields and labels in rowOne panel
    rowOne.add(inputFieldOne);
    rowOne.add(labelPlus);
    rowOne.add(inputFieldTwo);
    rowOne.add(labelEquality);
    rowOne.add(inputFieldThree);

    //Adding button in rowTwo panel
    rowTwo.add(buttonAdd);

    //Adding rowOne and rowTwo panels on the frame
    f.add(rowOne, BorderLayout.NORTH);
    f.add(rowTwo, BorderLayout.CENTER);

    f.setSize(200, 300);
    f.setVisible(true);
    }

    //Method to GET VALUE of TextField "inputFieldOne" in double format
    public double getInputFieldOne(){
    return (Double.parseDouble(inputFieldOne.getText()));
    }

    //Method to GET VALUE of TextField "inputFieldTwo" in double format
    public double getInputFieldTwo(){
    return (Double.parseDouble(inputFieldTwo.getText()));
    }

    //Method to SET VALUE of TextField "inputFieldOne" in double format
    public void setInputFieldThree(double valueToSet){
    inputFieldThree.setText(String.valueOf(valueToSet));
    }

    //Attaching the ActionListeners to their respective components
    //SET ActionListener to Button
    public void setButtonActionListener(ActionListener al){
    buttonAdd.addActionListener(al);
    }

    //SET WindowListener to Frame
    public void setFrameWindowListener(WindowListener wl){
    f.addWindowListener(wl);
    }
    }

    /******** ENDOF CalculatorView.java **********/

    /********* CalculatorController.java BEGINS **********/

    public class CalculatorController{
    private CalculatorModel model;
    private CalculatorView view;

    public CalculatorController(CalculatorModel model, CalculatorView view){
    this.model = model;
    this.view = view;

    this.view.launchUI(); //<----THIS IS LINE 12
    this.view.setButtonActionListener(new ActionHandler());
    this.view.setFrameWindowListener(new WindowHandler());
    }

    //Inner class IMPLEMENTING ActionListener
    public class ActionHandler implements ActionListener{
    public void actionPerformed(ActionEvent ae){
    model.addition(view.getInputFieldOne(), view.getInputFieldTwo());
    view.setInputFieldThree(model.getAddResult());
    }
    }

    //Inner class EXTENDING WindowAdapter
    public class WindowHandler extends WindowAdapter{
    public void windowClosing(WindowEvent we){
    System.exit(0);
    }
    }
    }

    /******** ENDOF CalculatorController.java **********/

    /********* Calculator.java BEGINS **********/

    public class Calculator{
    public static void main(String args[]){
    CalculatorModel model = new CalculatorModel();
    CalculatorView view = new CalculatorView("Calculator");
    CalculatorController controller = new CalculatorController(model, view); //<----THIS IS LINE 5
    }
    }



Please help me in pin pointing the exact problematic area in the program and why it is creating error. In addition what are the steps should I follow to solve this?





Aucun commentaire:

Enregistrer un commentaire