I'm getting the following error:
我得到了以下错误:
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)
I think it may be referring to my use of Integer.parseInt(stdin.readLine());
我认为它可能指的是我使用Integer.parseInt(stdin.readLine());
Not completely sure. This is my first time ever writing java. Here's my code:
不完全确定。这是我第一次编写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
2 个解决方案
#1
0
Input date: Exception in thread "main" java.lang.NullPointerException
at Project03.GetMonthInfo(Project03Driver.java:89)
From the stack trace: you haven't initialized stdin
instance of BufferedReader
of Project03
class. Your invocation of app.appMain();
inside the main(args)
function is calling GetMonthInfo();
,inside of which you are trying to execute Integer.parseInt(stdin.readLine())
: as stdin
is null
, you can't invoke readLine()
on this. Initialize it as follows in the Project03
class context:
从堆栈跟踪:您还没有初始化Project03类的BufferedReader的stdin实例。你调用app.appMain();在main(args)函数中调用GetMonthInfo();在这个函数中,您试图执行Integer.parseInt(stdin.readLine()):由于stdin为null,因此不能在这个函数中调用readLine()。在Project03类上下文中初始化:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
#2
0
The stacktrace is a good indicator of where the problem exists — the variable stdin
has yet to be initialized:
stacktrace是问题存在的一个很好的指示器——变量stdin还没有初始化:
stdin = new BufferedReader(new InputStreamReader(System.in));
#1
0
Input date: Exception in thread "main" java.lang.NullPointerException
at Project03.GetMonthInfo(Project03Driver.java:89)
From the stack trace: you haven't initialized stdin
instance of BufferedReader
of Project03
class. Your invocation of app.appMain();
inside the main(args)
function is calling GetMonthInfo();
,inside of which you are trying to execute Integer.parseInt(stdin.readLine())
: as stdin
is null
, you can't invoke readLine()
on this. Initialize it as follows in the Project03
class context:
从堆栈跟踪:您还没有初始化Project03类的BufferedReader的stdin实例。你调用app.appMain();在main(args)函数中调用GetMonthInfo();在这个函数中,您试图执行Integer.parseInt(stdin.readLine()):由于stdin为null,因此不能在这个函数中调用readLine()。在Project03类上下文中初始化:
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
#2
0
The stacktrace is a good indicator of where the problem exists — the variable stdin
has yet to be initialized:
stacktrace是问题存在的一个很好的指示器——变量stdin还没有初始化:
stdin = new BufferedReader(new InputStreamReader(System.in));