0

我收到以下错误:

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 
4

2 回答 2

0
Input date: Exception in thread "main" java.lang.NullPointerException
    at Project03.GetMonthInfo(Project03Driver.java:89)

从堆栈跟踪:您还没有初始化类的stdin实例。您在函数内部的调用正在调用,您正在尝试在其中执行:按原样,您不能在此调用。在类上下文中按如下方式对其进行初始化:BufferedReaderProject03app.appMain();main(args)GetMonthInfo();Integer.parseInt(stdin.readLine())stdinnullreadLine()Project03

BufferedReader stdin =  new BufferedReader(new InputStreamReader(System.in));
于 2013-11-10T22:55:00.637 回答
0

堆栈跟踪可以很好地指示问题所在 - 变量stdin尚未初始化:

stdin = new BufferedReader(new InputStreamReader(System.in));
于 2013-11-10T22:55:57.640 回答