Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. Exception in thread "main" java.lang.NullPointerException
  2. at CalculatorView.launchUI(CalculatorView.java:42)
  3. at CalculatorController.<init>(CalculatorController.java:12)
  4. at Calculator.main(Calculator.java:5)
  5.  
  6. ..at CalculatorView.launchUI(CalculatorView.java:43)
  7.  
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.awt.BorderLayout;
  11. import java.awt.FlowLayout;
  12. import java.awt.GridLayout;
  13.  
  14.  
  15. /********* CalculatorModel.java BEGINS **********/
  16.  
  17. public class CalculatorModel{
  18. private double numOne;
  19. private double numTwo;
  20. private double addResult;
  21.  
  22. public void addition(double one, double two){
  23. addResult = one + two;
  24. }
  25.  
  26. public double getAddResult(){
  27. return addResult;
  28. }
  29. }
  30.  
  31. /******** ENDOF CalculatorModel.java **********/
  32.  
  33. /********* CalculatorView.java BEGINS **********/
  34.  
  35. public class CalculatorView{
  36. //Declaring refrences for components to use
  37. private Frame f;
  38.  
  39. private TextField inputFieldOne;
  40. private TextField inputFieldTwo;
  41. private TextField inputFieldThree;
  42.  
  43. private Label labelPlus;
  44. private Label labelEquality;
  45.  
  46. private Panel rowOne;
  47. private Panel rowTwo;
  48.  
  49. private Button buttonAdd;
  50.  
  51. //Parameterized constructor to initialize refrences with their respective objects
  52. public CalculatorView(String title){
  53. f = new Frame(title);
  54.  
  55. inputFieldOne = new TextField(10);
  56. inputFieldTwo = new TextField(10);
  57. inputFieldThree = new TextField(10);
  58.  
  59. labelPlus = new Label("+");
  60. labelEquality = new Label("=");
  61.  
  62. buttonAdd = new Button("Add");
  63. }
  64.  
  65. //Method to launch the User Interface/UI
  66. public void launchUI(){
  67.  
  68. //Setting the layout
  69. f.setLayout(new BorderLayout());
  70. rowOne.setLayout(new GridLayout(1, 5)); //<----THIS IS LINE 42
  71. rowTwo.setLayout(new FlowLayout());
  72.  
  73. //Adding the components to their respective layout
  74. //Adding input fields and labels in rowOne panel
  75. rowOne.add(inputFieldOne);
  76. rowOne.add(labelPlus);
  77. rowOne.add(inputFieldTwo);
  78. rowOne.add(labelEquality);
  79. rowOne.add(inputFieldThree);
  80.  
  81. //Adding button in rowTwo panel
  82. rowTwo.add(buttonAdd);
  83.  
  84. //Adding rowOne and rowTwo panels on the frame
  85. f.add(rowOne, BorderLayout.NORTH);
  86. f.add(rowTwo, BorderLayout.CENTER);
  87.  
  88. f.setSize(200, 300);
  89. f.setVisible(true);
  90. }
  91.  
  92. //Method to GET VALUE of TextField "inputFieldOne" in double format
  93. public double getInputFieldOne(){
  94. return (Double.parseDouble(inputFieldOne.getText()));
  95. }
  96.  
  97. //Method to GET VALUE of TextField "inputFieldTwo" in double format
  98. public double getInputFieldTwo(){
  99. return (Double.parseDouble(inputFieldTwo.getText()));
  100. }
  101.  
  102. //Method to SET VALUE of TextField "inputFieldOne" in double format
  103. public void setInputFieldThree(double valueToSet){
  104. inputFieldThree.setText(String.valueOf(valueToSet));
  105. }
  106.  
  107. //Attaching the ActionListeners to their respective components
  108. //SET ActionListener to Button
  109. public void setButtonActionListener(ActionListener al){
  110. buttonAdd.addActionListener(al);
  111. }
  112.  
  113. //SET WindowListener to Frame
  114. public void setFrameWindowListener(WindowListener wl){
  115. f.addWindowListener(wl);
  116. }
  117. }
  118.  
  119. /******** ENDOF CalculatorView.java **********/
  120.  
  121. /********* CalculatorController.java BEGINS **********/
  122.  
  123. public class CalculatorController{
  124. private CalculatorModel model;
  125. private CalculatorView view;
  126.  
  127. public CalculatorController(CalculatorModel model, CalculatorView view){
  128. this.model = model;
  129. this.view = view;
  130.  
  131. this.view.launchUI(); //<----THIS IS LINE 12
  132. this.view.setButtonActionListener(new ActionHandler());
  133. this.view.setFrameWindowListener(new WindowHandler());
  134. }
  135.  
  136. //Inner class IMPLEMENTING ActionListener
  137. public class ActionHandler implements ActionListener{
  138. public void actionPerformed(ActionEvent ae){
  139. model.addition(view.getInputFieldOne(), view.getInputFieldTwo());
  140. view.setInputFieldThree(model.getAddResult());
  141. }
  142. }
  143.  
  144. //Inner class EXTENDING WindowAdapter
  145. public class WindowHandler extends WindowAdapter{
  146. public void windowClosing(WindowEvent we){
  147. System.exit(0);
  148. }
  149. }
  150. }
  151.  
  152. /******** ENDOF CalculatorController.java **********/
  153.  
  154. /********* Calculator.java BEGINS **********/
  155.  
  156. public class Calculator{
  157. public static void main(String args[]){
  158. CalculatorModel model = new CalculatorModel();
  159. CalculatorView view = new CalculatorView("Calculator");
  160. CalculatorController controller = new CalculatorController(model, view); //<----THIS IS LINE 5
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement