- #1
DarkAnt
- 195
- 0
This is my first java gui program. When I try to get the text from the text fields i get a null pointer exception. I looked at the text fields with the debugger and found that the text fields were null even though i had typed stuff into them. could someone help me out here? btw I know my programming practices are terrible.
package project_7_5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class Gui extends JFrame implements ActionListener{
public double dblMoney = 0;
public int intCompound;
public String strMoney = "" + dblMoney;
public String strError = "";
public String strPrincipal;
public String strInterest;
public String strCompound;
public String strYears;
JTextField principalField;
JTextField interestField;
JTextField compoundField;
JTextField yearsField;
JLabel moneyLabel2;
JLabel errorLabel;
JButton calculateButton;
public Gui() {
super("Bank of Trehan");
setSize(200,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
FlowLayout button = new FlowLayout(FlowLayout.CENTER);
GridLayout grid = new GridLayout(7,1);
pane.setLayout(grid);
JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Principal: $", JLabel.RIGHT);
JTextField principalField = new JTextField(10);
JPanel row2 = new JPanel();
JLabel interestLabel = new JLabel("Interest(%): ", JLabel.RIGHT);
JTextField interstField = new JTextField(3);
JPanel row3 = new JPanel();
JLabel compoundLabel = new JLabel("Compound: ", JLabel.RIGHT);
JTextField compoundField = new JTextField(2);
JPanel row4 = new JPanel();
JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
JTextField yearsField = new JTextField(10);
JPanel row5 = new JPanel();
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
JPanel row6 = new JPanel();
JLabel moneyLabel = new JLabel("Money: $", JLabel.RIGHT);
JLabel moneyLabel2 = new JLabel(strMoney, JLabel.RIGHT);
JPanel row7 = new JPanel();
JLabel errorLabel = new JLabel(strError, JLabel.RIGHT);
row1.setLayout(flow);
row2.setLayout(flow);
row3.setLayout(flow);
row4.setLayout(flow);
row5.setLayout(button);
row6.setLayout(flow);
row7.setLayout(button);
row1.add(principalLabel);
row1.add(principalField);
row2.add(interestLabel);
row2.add(interstField);
row3.add(compoundLabel);
row3.add(compoundField);
row4.add(yearsLabel);
row4.add(yearsField);
row5.add(calculateButton);
row6.add(moneyLabel);
row6.add(moneyLabel2);
row7.add(errorLabel);
pane.add(row1);
pane.add(row2);
pane.add(row3);
pane.add(row4);
pane.add(row5);
pane.add(row6);
pane.add(row7);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals("Calculate"))
{
strError = "";
strPrincipal = principalField.toString();
strInterest = interestField.toString();
strCompound = compoundField.toString();
strYears = yearsField.toString();
double principal, money, interest, years;
int compound;
principal = Integer.parseInt(strPrincipal);
interest = Integer.parseInt(strInterest);
compound = Integer.parseInt(strCompound);
years = Integer.parseInt(strYears);
interest = interest / 100;
years = years / compound;
money = principal;
for(int i = 1; i <= years; i++)
{
money = money * interest + money;
}
dblMoney = money;
strMoney = "" + dblMoney;
moneyLabel2.setText(strMoney);
}
if (command != "Calculate");
{
strError = "Unknown Error";
errorLabel.setText(strError);
}
}
}
package project_7_5;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
class Gui extends JFrame implements ActionListener{
public double dblMoney = 0;
public int intCompound;
public String strMoney = "" + dblMoney;
public String strError = "";
public String strPrincipal;
public String strInterest;
public String strCompound;
public String strYears;
JTextField principalField;
JTextField interestField;
JTextField compoundField;
JTextField yearsField;
JLabel moneyLabel2;
JLabel errorLabel;
JButton calculateButton;
public Gui() {
super("Bank of Trehan");
setSize(200,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.LEFT);
FlowLayout button = new FlowLayout(FlowLayout.CENTER);
GridLayout grid = new GridLayout(7,1);
pane.setLayout(grid);
JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Principal: $", JLabel.RIGHT);
JTextField principalField = new JTextField(10);
JPanel row2 = new JPanel();
JLabel interestLabel = new JLabel("Interest(%): ", JLabel.RIGHT);
JTextField interstField = new JTextField(3);
JPanel row3 = new JPanel();
JLabel compoundLabel = new JLabel("Compound: ", JLabel.RIGHT);
JTextField compoundField = new JTextField(2);
JPanel row4 = new JPanel();
JLabel yearsLabel = new JLabel("Years: ", JLabel.RIGHT);
JTextField yearsField = new JTextField(10);
JPanel row5 = new JPanel();
JButton calculateButton = new JButton("Calculate");
calculateButton.addActionListener(this);
JPanel row6 = new JPanel();
JLabel moneyLabel = new JLabel("Money: $", JLabel.RIGHT);
JLabel moneyLabel2 = new JLabel(strMoney, JLabel.RIGHT);
JPanel row7 = new JPanel();
JLabel errorLabel = new JLabel(strError, JLabel.RIGHT);
row1.setLayout(flow);
row2.setLayout(flow);
row3.setLayout(flow);
row4.setLayout(flow);
row5.setLayout(button);
row6.setLayout(flow);
row7.setLayout(button);
row1.add(principalLabel);
row1.add(principalField);
row2.add(interestLabel);
row2.add(interstField);
row3.add(compoundLabel);
row3.add(compoundField);
row4.add(yearsLabel);
row4.add(yearsField);
row5.add(calculateButton);
row6.add(moneyLabel);
row6.add(moneyLabel2);
row7.add(errorLabel);
pane.add(row1);
pane.add(row2);
pane.add(row3);
pane.add(row4);
pane.add(row5);
pane.add(row6);
pane.add(row7);
setContentPane(pane);
setVisible(true);
}
public void actionPerformed(ActionEvent event)
{
String command = event.getActionCommand();
if (command.equals("Calculate"))
{
strError = "";
strPrincipal = principalField.toString();
strInterest = interestField.toString();
strCompound = compoundField.toString();
strYears = yearsField.toString();
double principal, money, interest, years;
int compound;
principal = Integer.parseInt(strPrincipal);
interest = Integer.parseInt(strInterest);
compound = Integer.parseInt(strCompound);
years = Integer.parseInt(strYears);
interest = interest / 100;
years = years / compound;
money = principal;
for(int i = 1; i <= years; i++)
{
money = money * interest + money;
}
dblMoney = money;
strMoney = "" + dblMoney;
moneyLabel2.setText(strMoney);
}
if (command != "Calculate");
{
strError = "Unknown Error";
errorLabel.setText(strError);
}
}
}