aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Interesting exception errors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Interesting exception errors" Watch "Interesting exception errors" New topic
Author

Interesting exception errors

Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Hello everyone
My program compiles fine, but when I try to run it I get this error:

Exception in thread "main" java.lang.NullPointerException
at javax.swing.DefaultComboBoxModel.<init>(Unknown Source)
at javax.swing.JComboBox.<init>(Unknown Source)
at MortgageFrame5.<init>(MortgageFrame5.java:45)
at MortgageFrame5.main(MortgageFrame5.java:199)

I have looked around on the net looking for solutions, but have not found one yet that worked. I am new at this still and appreciate any assistance you all can offer.

Jenny

import javax.swing.*; //importing the ability to create and use a frame
import java.awt.*; //importing the swing functions like the combo box
import java.awt.event.*;
import java.lang.String;
import java.io.*;

public class MortgageFrame5 extends JFrame implements ItemListener, ActionListener, Runnable{

//setting up my variables
String paythis;
double monthly;
int a;
int t;
double I;
double total;
int loop;
double balance;
String actInt;

//creating lables, boxes, arrays, and buttons
JLabel amountLabel = new JLabel ("House cost in dollars:");
JComboBox amount = new JComboBox();{
amount.addItem("$100,000");
amount.addItem("$125,000");
amount.addItem("$150,000");
amount.addItem("$175,000");
amount.addItem("$200,000");
amount.addItem("$225,000");
amount.addItem("$250,000");
amount.addItem("$275,000");
}
JLabel tILabel = new JLabel ("Payment term and rate:");
String[] termsInterestArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
String[] termsArray = {"7 years", "15 years", "30 years"};
String[] interestArray;


JComboBox tI = new JComboBox(termsInterestArray);
JComboBox term = new JComboBox(termsArray);
JComboBox interest = new JComboBox(interestArray);
JButton calculate = new JButton("Calculate");
JButton quit = new JButton("Quit");
JLabel heading = new JLabel ("Payment | Loan Balance | Interest Paid");
JTextArea future = new JTextArea (180, 40);
JScrollPane futureInfo = new JScrollPane(future);

JCheckBox seperateCheck = new JCheckBox("Check to select term and interest seperately");
JCheckBox tICheck = new JCheckBox("Check to select term and interest together");
ButtonGroup select = new ButtonGroup();

public MortgageFrame5(){
//setting up the frame and frame characteristics
setTitle("Mortgage calculator");
setSize(340,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //user can see the box
FlowLayout flo = new FlowLayout();
Container pane = getContentPane();
future.setLineWrap(true);
select.add(seperateCheck);
select.add(tICheck);

//setting the default layout manager and adding everything to the container
pane.setLayout(flo);
pane.add(amountLabel);
pane.add(amount);
pane.add(tILabel);
pane.add(tICheck);
pane.add(tI);
pane.add(seperateCheck);
pane.add(term);
pane.add(interest);
pane.add(calculate);
pane.add(quit);
pane.add(heading);
pane.add(futureInfo);
setContentPane(pane);

//setting up the listeners
amount.addItemListener(this);
tI.addItemListener(this);
term.addItemListener(this);
interest.addItemListener(this);
seperateCheck.addItemListener(this);
tICheck.addItemListener(this);
calculate.addActionListener(this);
quit.addActionListener(this);
repaint();
// retrieving the interest rates from the file
try{
File interestRate = new File("interestRate.txt");
FileInputStream fileStream = new FileInputStream(interestRate);
BufferedReader stream = new BufferedReader(new InputStreamReader(fileStream));
String interestArray = new String(stream.readLine());
}
catch (Exception e){
System.err.println("Could not read file.");
}

}
//this is where the program will act upon the user selections
public void itemStateChanged(java.awt.event.ItemEvent e){

}
//This is where the calculate button functionality comes from
public void actionPerformed(ActionEvent event){
if(event.getSource() == calculate){
String strA = (String)amount.getSelectedItem();
if (strA.equals("$100,000")){
a = 100000;
}
else if (strA.equals("$125,000")){
a = 125000;
}
else if (strA.equals("$150,000")){
a = 150000;
}
else if (strA.equals("$175,000")){
a = 175000;
}
else if (strA.equals("$200,000")){
a = 200000;
}
else if (strA.equals("$225,000")){
a = 225000;
}
else if (strA.equals("$250,000")){
a = 250000;
}
else if (strA.equals("$275,000")){
a = 275000;
}
if (seperateCheck.isSelected()){
String strT = (String)term.getSelectedItem();
if (strT.equals("7 years")){
t = 7*12;
}
else if (strT.equals("15 years")){
t = 15*12;
}
else if (strT.equals("30 years")){
t = 30*12;
}
String strI = (String)interest.getSelectedItem();
if (strI.equals("5.35%")){
I = 0.0535;
}
else if (strI.equals("5.5%")){
I = 0.055;
}
else if (strI.equals("5.75%")){
I = 0.0575;
}
}
else if (tICheck.isSelected()){
String strT = (String)tI.getSelectedItem();
if (strT.equals("7 years at 5.35%")){
t = 7*12;
I = 5.35;
}
else if (strT.equals("15 years at 5.5%")){
t = 15*12;
I = 5.5;
}
else if (strT.equals("30 years at 5.75%")){
t = 30*12;
I = 5.75;
}
}

}
total = ((I/1200) + (I/1200)/(Math.pow(1+(I/1200),t) -1)) * 100 * a;
monthly = total/t;
balance = total;
actInt = ("$" + (Math.round((monthly * I)* 100) / 100.00));

//setting up the loop to run and print the information into the text area
future.setText("\n");
for (int loop = 1; loop <= t; loop ++){
monthly = Math.round(monthly * 100) / 100;
balance = Math.round(balance * 100) / 100;
future.append(" " + loop + "\t $" + monthly + "\t $" + balance + "\t" + actInt + "\n");
balance = (total - (monthly*loop));
}

if(event.getSource() == quit){
System.exit(0);
}
}
public void run(){
}
//this is the main function where the program runs
public static void main(String[] arguments){
MortgageFrame5 mor = new MortgageFrame5();
}
}

Here is the full code listing:
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Since it's a null pointer exception in the <init> of the JComboBox - that is, during construction - maybe you're not passing in all the things you need to pass in to the constructor? You might want to double check Sun's documentation on what's required.
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
The code worked just fine (to include the section you are referring to) until I added this part:
// retrieving the interest rates from the file
try{
File interestRate = new File("interestRate.txt");
FileInputStream fileStream = new FileInputStream(interestRate);
BufferedReader stream = new BufferedReader(new InputStreamReader(fileStream));
String interestArray = new String(stream.readLine());
}
catch (Exception e){
System.err.println("Could not read file.");
}
And then I got the error that I asked about in the original post. Do you think that by adding this code that it has caused a problem with another part of the code?

Jenny
Vijay Raj
Greenhorn

Joined: Dec 07, 2004
Posts: 3
You are passing a null string array object for initializing JComboBox.

I think the problem is with this line of code....
JComboBox interest = new JComboBox(interestArray);
Jen Wren
Greenhorn

Joined: Nov 22, 2004
Posts: 17
Good point, I moved my stream file reader above that line, but now I am getting an error for illegal start of type. I'm still researching it, but it isn't going well so far.

Here is what the code looks like after the change.

import javax.swing.*; //importing the ability to create and use a frame
import java.awt.*; //importing the swing functions like the combo box
import java.awt.event.*;
import java.lang.String;
import java.io.*;

public class MortgageFrame5 extends JFrame implements ItemListener, ActionListener, Runnable{

//setting up my variables
String paythis;
double monthly;
int a;
int t;
double I;
double total;
int loop;
double balance;
String actInt;

int totalGraph;
double interestGraph;
double principalGraph;
int pie[];
int pieDegree[];

//creating lables, boxes, arrays, and buttons
JLabel amountLabel = new JLabel ("House cost in dollars:");
JComboBox amount = new JComboBox();{
amount.addItem("$100,000");
amount.addItem("$125,000");
amount.addItem("$150,000");
amount.addItem("$175,000");
amount.addItem("$200,000");
amount.addItem("$225,000");
amount.addItem("$250,000");
amount.addItem("$275,000");
}
JLabel tILabel = new JLabel ("Payment term and rate:");
String[] termsInterestArray = {"7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
String[] termsArray = {"7 years", "15 years", "30 years"};
String[] interestArray;

try{
File interestRate = new File("interestRate.txt");
FileInputStream fileStream = new FileInputStream(interestRate);
BufferedReader stream = new BufferedReader(new InputStreamReader(fileStream));
String interestArray = new String(stream.readLine());
}
catch (Exception e){
System.err.println("Could not read file.");
}


JComboBox tI = new JComboBox(termsInterestArray);
JComboBox term = new JComboBox(termsArray);
JComboBox interest = new JComboBox(interestArray);
JButton calculate = new JButton("Calculate");
JButton quit = new JButton("Quit");
JLabel heading = new JLabel ("Payment | Loan Balance | Interest Paid");
JTextArea future = new JTextArea (180, 40);
JScrollPane futureInfo = new JScrollPane(future);

JCheckBox seperateCheck = new JCheckBox("Check to select term and interest seperately");
JCheckBox tICheck = new JCheckBox("Check to select term and interest together");
ButtonGroup select = new ButtonGroup();

public MortgageFrame5(){
//setting up the frame and frame characteristics
setTitle("Mortgage calculator");
setSize(340,350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true); //user can see the box
FlowLayout flo = new FlowLayout();
Container pane = getContentPane();
future.setLineWrap(true);
select.add(seperateCheck);
select.add(tICheck);

//setting the default layout manager and adding everything to the container
pane.setLayout(flo);
pane.add(amountLabel);
pane.add(amount);
pane.add(tILabel);
pane.add(tICheck);
pane.add(tI);
pane.add(seperateCheck);
pane.add(term);
pane.add(interest);
pane.add(calculate);
pane.add(quit);
pane.add(heading);
pane.add(futureInfo);
setContentPane(pane);

//setting up the listeners
amount.addItemListener(this);
tI.addItemListener(this);
term.addItemListener(this);
interest.addItemListener(this);
seperateCheck.addItemListener(this);
tICheck.addItemListener(this);
calculate.addActionListener(this);
quit.addActionListener(this);
repaint();
// retrieving the interest rates from the file

}
//this is where the program will act upon the user selections
public void itemStateChanged(java.awt.event.ItemEvent e){

}
//This is where the calculate button functionality comes from
public void actionPerformed(ActionEvent event){
if(event.getSource() == calculate){
String strA = (String)amount.getSelectedItem();
if (strA.equals("$100,000")){
a = 100000;
}
else if (strA.equals("$125,000")){
a = 125000;
}
else if (strA.equals("$150,000")){
a = 150000;
}
else if (strA.equals("$175,000")){
a = 175000;
}
else if (strA.equals("$200,000")){
a = 200000;
}
else if (strA.equals("$225,000")){
a = 225000;
}
else if (strA.equals("$250,000")){
a = 250000;
}
else if (strA.equals("$275,000")){
a = 275000;
}
if (seperateCheck.isSelected()){
String strT = (String)term.getSelectedItem();
if (strT.equals("7 years")){
t = 7*12;
}
else if (strT.equals("15 years")){
t = 15*12;
}
else if (strT.equals("30 years")){
t = 30*12;
}
String strI = (String)interest.getSelectedItem();
if (strI.equals("5.35%")){
I = 0.0535;
}
else if (strI.equals("5.5%")){
I = 0.055;
}
else if (strI.equals("5.75%")){
I = 0.0575;
}
}
else if (tICheck.isSelected()){
String strT = (String)tI.getSelectedItem();
if (strT.equals("7 years at 5.35%")){
t = 7*12;
I = 5.35;
}
else if (strT.equals("15 years at 5.5%")){
t = 15*12;
I = 5.5;
}
else if (strT.equals("30 years at 5.75%")){
t = 30*12;
I = 5.75;
}
}

}
total = ((I/1200) + (I/1200)/(Math.pow(1+(I/1200),t) -1)) * 100 * a;
monthly = total/t;
balance = total;
actInt = ("$" + (Math.round((monthly * I)* 100) / 100.00));

//setting up the loop to run and print the information into the text area
future.setText("\n");
for (int loop = 1; loop <= t; loop ++){
monthly = Math.round(monthly * 100) / 100;
balance = Math.round(balance * 100) / 100;
future.append("\t $" + monthly + "\t $" + balance + "\t" + actInt + "\n");
balance = (total - (monthly*loop));
}
interestGraph = (total * I) / 100;
principalGraph = total;
pie = new int[2];
pieDegree = new int [2];
pie[0] = interestGraph;
pie[1] = principalGraph;
totalGraph = pie[0] + pie[1];
pieDegree[0] = ((pie[0] * 360) / totalGraph);
pieDegree[1] = ((pie[1] * 360) / totalGraph);

if(event.getSource() == quit){
System.exit(0);
}
}
public void run(){
}
//this is the main function where the program runs
public static void main(String[] arguments){
MortgageFrame5 mor = new MortgageFrame5();
}
}
Vijay Raj
Greenhorn

Joined: Dec 07, 2004
Posts: 3
I hope the changes you did would have given compilation errors itself..

Make the following changes and try it out..

 
 
subject: Interesting exception errors