|
-
Java- Problem with array of objects
I am working on a lab that calculates a federal tax, state tax, and Net Pay and displays all the taxpayer's information to the screen. The Taxpayer is a class, and WeeklyTaxpayer, BiWeeklyTaxpayer, and MonthlyTaxpayer are its derived classes.
I know that my classes work fine, because I can get the program to work by declaring Taxpayer objects instead of the array.
However, the assignment requires we use a Taxpayer type array and one Taxpayer object to receive the data and display it in a JOPtionPane Dialog Box.
Whenever I execute the program, I keep getting a Null Pointer Exception.
Can anyone see what I am doing wrong? I have all my code posted.
(Oh and yes the array is supposed to be size 50, even though you can only read in 5 taxpayers.)
Main Code:
Code:
/*
* Main.java
*
* Created on October 4, 2007, 2:09 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package lab06b;
import javax.swing.*;
import java.text.*;
/**
*
* @
*/
public class Main {
/** Creates a new instance of Main */
public Main() {
}
/**
* @param args the command line arguments
*/
public static void main( String[] args ) {
final int MAX_NUMBER = 5;
Taxpayer [] taxInfo = new Taxpayer [50];
Taxpayer t = null;
int index = 0; // Holds index value for array variable
DecimalFormat prec2 = new DecimalFormat("$#.00");
DecimalFormat prec1 = new DecimalFormat("#.0%");
String nameStr = ""; // Holds taxpayer name entered by user
String ssNumStr = ""; // Holds taxpayer's SSNnumber as string, entered by user
int ssNum = 0; // Taxpayer's ssnumber as int
char taxpayerType; // The char form of the user-enetered taxpayer type
String grossStr = ""; // Taxpayer's gross pay as string, entered by user
double grossPay = 0.0; // Taxpayer's gross pay as double
int count = 0; // The number of taxpayers created
String outputStr = ""; // String for output display to user
String outputStr2 = "";// String for output display to user
String msgStr = ""; // Holds message for display to user
int resp = 0; // User's response from showConfirmDialog method
while (true) { // Loop to input data on each taxpayer
count++;
// Read in name from user as a string
nameStr = JOptionPane.showInputDialog("Enter taxpayer name");
// Read in ID number from user as a string
ssNumStr = JOptionPane.showInputDialog("Enter SSN number");
// Convert from type String to type int
ssNum = Integer.parseInt(ssNumStr.trim());
// Convert from Type string to type char
taxpayerType = getTaxpayerType();
// Read in gross pay from user as a string
grossStr = JOptionPane.showInputDialog("Enter gross pay");
// Convert from type String to type double
grossPay = Double.parseDouble(grossStr.trim());
// Create Taxpayer instance
switch(taxpayerType) {
case 'W': t = new WeeklyTaxpayer(nameStr, ssNum, grossPay); break;
case 'w': t = new WeeklyTaxpayer(nameStr, ssNum, grossPay); break;
case 'B': t = new BiweeklyTaxpayer(nameStr, ssNum, grossPay); break;
case 'b': t = new BiweeklyTaxpayer(nameStr, ssNum, grossPay); break;
case 'M': t = new MonthlyTaxpayer(nameStr, ssNum, grossPay); break;
case 'm': t = new MonthlyTaxpayer(nameStr, ssNum, grossPay); break;
} // End of switch statement
// Assignes taxpayer information to array (index)
taxInfo[index] = t;
// Create ouputstr for each taxpayer's information
outputStr += count + ". " + taxInfo[index].toString() + "\n";
++index; // increment array to next index
if (count < MAX_NUMBER) {
msgStr = "\nMax number of allowed taxpayers is: " + MAX_NUMBER + "\nContinue?";
resp = JOptionPane.showConfirmDialog(null, outputStr + msgStr, "Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (resp == JOptionPane.NO_OPTION)
break;
} else {
msgStr = "Program cannot handle any more additional taxpayers.";
JOptionPane.showMessageDialog(null, outputStr + msgStr, "Results",
JOptionPane.INFORMATION_MESSAGE);
break;
}// End of if statement
}// End of while loop
outputStr2 = "Taxpayer list:\n";
for (int i = 0; i <= count; i++){
t = taxInfo[i];
outputStr2 += "\n" + i + ". " + t.getName() +
"\n Taxpayer Type: " + t.getClass().getName() +
"\n SSN: " + t.getSSNumber() +
"\n Gross Pay: " + prec2.format(t.getGrossPay()) +
"\n NYS Tax: " + prec2.format(t.computeStateTax()) +
"\n Fed Tax: " + prec2.format(t.computeFedTax()) +
"\n Net Pay: " + prec2.format(t.computeNetPay()) + "\n";
}
JOptionPane.showMessageDialog(null, outputStr2, "TAXPAYERS",
JOptionPane.INFORMATION_MESSAGE);
}// End of main
// Gets, checks, and returns character for taxpayer type
public static char getTaxpayerType() {
String taxpayerTypeStr;
char type;
while(true){
// Read in letter choice for taxpayerType as a string
taxpayerTypeStr = JOptionPane.showInputDialog(
"Choose taxpayer type by letter choice\n"
+ "W - Weekly\n" + "B - Biweekly\n" + "M - Monthly");
// Convert from Type string to type char
type = taxpayerTypeStr.charAt(0);
if (type == 'W' || type == 'w' || type == 'B' || type == 'b' ||
type == 'M' || type == 'm') {
break;
} else {
JOptionPane.showMessageDialog(null,
"Error: Invalid Character! \nPlease input a valid character.",
"ERROR MESSAGE", JOptionPane.ERROR_MESSAGE);
}
}
return(type);
}
}// End of class
Netbeans is saying the exception is happening here:
Code:
outputStr2 += "\n" + i + ". " + t.getName() +
I wanted to post my classes too, but the post became too long.
EDIT:
I can't believe I didn't catch this all day, but I must have been typing the code too fast and accidentally put a <= sign in my for loop. This was looping to the 6th array variable which is null. This was creating the null exception.
Last edited by Luvanime; 10-13-2007 at 12:06 AM.
Reason: Found The Problem
Similar Threads
-
Replies: 1
Last Post: 02-06-2006, 12:48 AM
-
Replies: 1
Last Post: 05-13-2005, 06:46 AM
-
By brouse in forum VB Classic
Replies: 1
Last Post: 05-10-2005, 02:19 PM
-
Replies: 0
Last Post: 12-13-2001, 12:06 PM
-
By Dharmesh in forum .NET
Replies: 4
Last Post: 10-01-2001, 03:47 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks