2012-10-26 31 views
0

我的代码对我来说看起来不错,但我一直得到一个nullpointerexception。我输入的文本文件看起来像这样:NullPointerException与Filechooser?

克米特D.Frogge 17.25 5.25 0.0 8.0 1.5 8.0 2.25 7.75 0.0 8.0 2.0

这是我的一些代码:

private static BufferedReader br; 
private static StringTokenizer strTkn; 


private static String st, firstName, middleInitial, lastName; 
private static double monRegHours, monOverHours, tuesRegHours, tuesOverHours,  wedRegHours, wedOverHours, 
thurRegHours, thurOverHours, friRegHours, friOverHours; 
private static double totalRegHours, totalOverHours, hourlyWage, regularPay, overtimePay, weeklyPay; 
private static JFileChooser chooser; 


public static void main (String args []) throws IOException 
{ 
chooser(); 
getData(); 
calcpay(); 
printresult(); 
} 


public static void chooser() throws IOException 
{ 
JFileChooser chooser=new JFileChooser(); 
int returnVal = chooser.showOpenDialog(null); 
if(returnVal == JFileChooser.APPROVE_OPTION) 
{ 
    File f = chooser.getSelectedFile(); 
    br=new BufferedReader(new FileReader(f)); 
    st=""; 
    while((st=br.readLine())!=null); 
    { 
    System.out.println ("Data Line = " + st); //print the whole line 
    } 
    } 
    } 


public static void getData() throws IOException 
{ 
strTkn = new StringTokenizer(st, " ."); 

firstName = strTkn.nextToken(); 
middleInitial = strTkn.nextToken(); 
lastName = strTkn.nextToken(); 
hourlyWage = Double.parseDouble(strTkn.nextToken()); 

monRegHours = Double.parseDouble(strTkn.nextToken()); 
monOverHours = Double.parseDouble(strTkn.nextToken()); 

tuesRegHours = Double.parseDouble(strTkn.nextToken()); 
tuesOverHours = Double.parseDouble(strTkn.nextToken()); 

wedRegHours = Double.parseDouble(strTkn.nextToken()); 
wedOverHours = Double.parseDouble(strTkn.nextToken()); 

thurRegHours = Double.parseDouble(strTkn.nextToken()); 
thurOverHours = Double.parseDouble(strTkn.nextToken()); 

friRegHours = Double.parseDouble(strTkn.nextToken()); 
friOverHours = Double.parseDouble(strTkn.nextToken()); 


} 


public static void calcpay() 
{ 
totalRegHours = monRegHours + tuesRegHours + wedRegHours + thurRegHours + friRegHours; 
totalOverHours = monOverHours + tuesOverHours + wedOverHours + thurOverHours + friOverHours; 

overtimePay = round(totalOverHours * (1.5 * hourlyWage)); 
regularPay = round(hourlyWage * totalRegHours); 
weeklyPay = regularPay + overtimePay; 
} 


public static void printresult() 
{ 
System.out.println("Name: " + firstName + " " + middleInitial + " " + lastName); 
System.out.printf("Weekly Pay: " + "$%.2f\n", weeklyPay); 
//this allows me to print $x.50 instead of $x.5, and not have repeating decimals 
} 


public static double round(double num) 
{ 
// rounding to two decimal places 
num *= 100; 
int rounded = (int) Math.round(num); 
return rounded/100.0; 

对不起如果看起来不好,我的电脑不喜欢复制粘贴

回答

1

这是一个非常简单的错误。您在while循环后放置了分号。

while((st=br.readLine())!=null); 

删除分号,它会正常工作。

+0

现在,它打印数据线罚款,但仍然给我一个空指针,当我尝试计算和使用stringtokenizer ... – xSpartanCx

+0

你根本不需要一个while循环。只需使用st = br.readLine();删除while循环,它会正常工作。退出条件是st = null,当循环退出时,显然它会给出一个空指针异常。 –

+0

现在,它的工作。谢谢! – xSpartanCx