Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- ..at CalculatorView.launchUI(CalculatorView.java:43)
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement