![](/img/trans.png)
[英]“Exception in thread ”main“ java.lang.NullPointerException”
[英]Exception in thread “main” java.lang.NullPointerException
我收到以下錯誤:
Input date: Exception in thread "main" java.lang.NullPointerException
at Project03.GetMonthInfo(Project03Driver.java:89)
at Project03.appMain(Project03Driver.java:63)
at Project03Driver.main(Project03Driver.java:18)
我認為這可能是指我對Integer.parseInt(stdin.readLine());
不太確定。 這是我第一次編寫Java。 這是我的代碼:
import java.io.*;
public class Project03Driver
{
public static void main(String [] args) throws IOException
{
Project03 app;
app = new Project03();
app.appMain();
}
} // end class Project03Driver
class Project03
{
// Instance (global) data declarations
/* Output iCode, uSold, uCost, uPrice
Output extCost, extPrice, iProfit */
/* Output totExtCost, totExtPrice, totProfit, totTax
Output avgProfit, lowUsold, lowUsoldIcode */
float iCode;
float uSold;
float uCost;
float uPrice;
float extCost;
float extPrice;
float iProfit;
float totExtCost;
float totExtPrice;
float totProfit;
float totTax;
float avgProfit;
float lowUsold;
float lowUsoldIcode;
// additional variables needed for compile
float totUsold;
int date;
int taxRate;
int profitRate;
BufferedReader stdin;
void appMain() throws IOException
{
//Output assignment
System.out.println("Assignment: Project #3" + "Written by: Evan Tanguma");
InitReport();
GetMonthInfo();
DisplayMonthInfo();
while (iCode != 0)
{
ProcItem();
}
DisplaySummary();
}
void InitReport() throws IOException
{
totUsold = 0;
totExtCost = 0;
totExtPrice = 0;
lowUsold = 100;
iCode= -11; //need to set to a value which is not the flag
}
void GetMonthInfo() throws IOException
{
//Input date, taxRate, profitRate
System.out.print("Input date: ");
date = Integer.parseInt(stdin.readLine());
System.out.print("Input taxRate: ");
taxRate = Integer.parseInt(stdin.readLine());
System.out.print("Input profitRate: ");
profitRate = Integer.parseInt(stdin.readLine());
}
void DisplayMonthInfo()
{
//Output date, taxRate, profitRate
System.out.println("The value of date is: " + date);
System.out.println("The value of taxRate is: " + taxRate);
System.out.println("The value of profitRate is: " + profitRate);
}
void ProcItem() throws IOException
{
GetIcode();
if (iCode != 0)
{
GetItemDetails();
CalculateDetailTotals();
UpdateLows();
DisplayItemDetails();
}
}
void GetIcode() throws IOException
{
// user input iCode
System.out.print("Input iCode: ");
iCode = Integer.parseInt(stdin.readLine());
}
void GetItemDetails() throws IOException
{
// user input uSold, uCost
System.out.print("Input uSold: ");
uSold = Integer.parseInt(stdin.readLine());
System.out.print("Input uCost: ");
uCost = Integer.parseInt(stdin.readLine());
}
void CalculateDetailTotals()
{
uPrice = uCost * (1 + profitRate);
extCost = uSold * uCost;
extPrice = uSold * uPrice;
iProfit = extPrice - extCost;
totUsold = totUsold + uSold;
totExtCost = totExtCost + extCost;
totExtPrice = totExtPrice + extPrice;
}
void UpdateLows()
{
if(uSold < lowUsold)
{
lowUsold = uSold;
lowUsoldIcode = iCode;
}
}
void DisplayItemDetails()
{
/* Output iCode, uSold, uCost, uPrice
Output extCost, extPrice, iProfit */
System.out.println("iCode is: " + iCode);
System.out.println("uSold is: " + uSold);
System.out.println("uCost is: " + uCost);
System.out.println("uPrice is: " + uPrice);
System.out.println("extCost is: " + extCost);
System.out.println("extPrice is: " + extPrice);
System.out.println("iProfit is: " + iProfit);
}
void DisplaySummary()
{
/* Output totExtCost, totExtPrice, totProfit, totTax
Output avgProfit, lowUsold, lowUsoldIcode */
System.out.println("totExtCost is: " + totExtCost);
System.out.println("totExtPrice is: " + totExtPrice);
System.out.println("totProfit is: " + totProfit);
System.out.println("totTax is: " + totTax);
System.out.println("avgProfit is: " + avgProfit);
System.out.println("lowUsold is: " + lowUsold);
System.out.println("lowUsoldIcode is: " + lowUsoldIcode);
}
} // end of class
Input date: Exception in thread "main" java.lang.NullPointerException
at Project03.GetMonthInfo(Project03Driver.java:89)
從堆棧跟蹤中:您尚未初始化Project03
類的BufferedReader
的stdin
實例。 您對app.appMain();
調用app.appMain();
在main(args)
函數內部正在調用GetMonthInfo();
,您要在其中執行Integer.parseInt(stdin.readLine())
:由於stdin
為null
,因此無法對此調用readLine()
。 在Project03
類上下文中對其進行初始化,如下所示:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
stacktrace可以很好地指示問題出在哪里—尚未初始化變量stdin
:
stdin = new BufferedReader(new InputStreamReader(System.in));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.