0

这是一个大学课程作业,它创建一个ItemRecord类并 Serializable使用 getter 和 setter 实现,然后覆盖该toString方法。接下来,我创建了一个ItemRecordReport类,其Scanner对象映射到输入文件“ItemRecord_text.txt”。创建了一个ObjectOutputStream映射到二进制输出文件“ItemRecord_binary.txt”的对象。创建了一个ObjectInputStream映射到二进制文件的对象。一切正常。我通过在方法 main 中放入一个数组并使用附加的文本文档更改字段值来修改这篇文章。我的错误是 NullPointerException:这是堆栈跟踪

Item   Cost     Quantity  Description
Number
A100  $ 99.99   10        Canon PowerShot-135
A200  $149.99   50        Panasonic-Lumix T55
A300  $349.99   20        Nikon- D3200 DSRL
A400  $280.99   30        Sony- DSC-W800
A500  $ 97.99   20        Samsung- WB35F
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.endRecord(ItemRecordReport.java:165)
at ItemRecordReport.main(ItemRecordReport.java:156)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

//创建程序,使类的对象可以序列化,实现接口Serialiable //创建带有4个参数的构造函数,附带get和set方法,覆盖toString方法 //创建5条记录的文本文件,创建Scanner对象,ObjectOutputStream和ObjectInputStream //创建新的ItemRecord对象,使用对象的set方法改变ItemRecord对象中所有字段的值 //使用toString方法修改ItemRecord对象

 import java.io.Serializable;
 public class ItemRecord implements Serializable

 {  //declare 4 fields with private access
 private String itemNumber;
 private double cost;
 private int quantity;
 private String description;


 //constructor with no arguments call other constructor with default values
 public ItemRecord()
 {
 this("",0.0,0,"");

 }//end constructor with no arguments
//constructor with 4 arguments
public ItemRecord(String number, double price, int quant, String desc)
{
  setItemNumber(number);
  setCost(price);
  setQuantity(quant);
  setDescription(desc);
 }//end constructor

 public void setItemNumber(String number)
 {
  itemNumber = number;
 }//end setItemNumber
 public String getItemNumber()
 {
  return itemNumber;
 }//end getItemNumber

 public void setCost(double price)
{
  cost = price;
}//end seCost
public double getCost()
{
  return cost;
}//end getCost

public void setQuantity(int quant)
{
  quantity = quant;
}//end setQuantity
public int getQuantity()
{
  return quantity;
}//end getQuantity

public void setDescription(String desc)
{
  description = desc;
}//end setDescription
public String getDescription()
{
  return description;
}//end getDescription
public static void printHeading()
{
System.out.println("Item   Cost     Quantity  Description");
System.out.print("Number");
System.out.println();
}

public String toString()
{
 return this.getItemNumber()+String.format("  $%6.2f   ",+this.getCost())+String.format("%4d       ",+this.getQuantity())+this.getDescription();
}
}//end class ItemRecord

这是另一个测试类:该类包含ObjectOutputStreamObjectInputStream对象。因此 main 方法以及ItemRecordReport我用来调用 main 中的方法的对象都在这里。

import java.io.*;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

public class ItemRecordReport
{  private ObjectOutputStream output;//writes to binary 
private ObjectInputStream binInFile;//reads binary
private Scanner input;

public void openInputBinaryFile()
 {//create a ObjectInputStream object that will read a binary file
  try
  {
    binInFile  = new ObjectInputStream(new FileInputStream("ItemRecord_binary.txt"));

  }//end try
  catch (IOException ioException)
  {
     System.out.println("Error opening file.");
  }//end catch 
  }//end openInputBinaryFile

  public void openOutputBinaryFile()
  {  
 //creating a binary objectOutputStream text file
  try
  {
     output = new ObjectOutputStream(
     new FileOutputStream("ItemRecord_binary.txt"));
   }//end try
   catch (IOException ioException)
   {
     System.err.println("Error opening file");
  }

  }//end openOutputBinaryFile
  public void readInputTextRecords()
  {
     ItemRecord record;
     String itemNumber;
     double cost;
     int quantity;
     String description;


     try
     {
       input  = new Scanner(new File("ItemRecord_text.txt"));
     }

      catch (IOException e)
     {
        System.out.println("Error file not found");
     }//end catch

      while(input.hasNext())
        {
        try{
              itemNumber = input.next();
              cost = input.nextDouble();
              quantity = input.nextInt();
              description = input.nextLine();

              record = new ItemRecord(itemNumber,cost,quantity,description);

              //writing into the binary text file
              output.writeObject(record);
           }

           catch (IOException ioException)
           {
              System.out.println("Error writing to binary file.");
              return;
           }
        }  
    }//end readInputRecords

   public void readInputBinaryRecords()
   {
     openInputBinaryFile();
     ItemRecord otherRecord;

     try
     {   while (true)
        {
           otherRecord = (ItemRecord) binInFile.readObject(); //use down cast
           System.out.println(otherRecord.toString());
        }
     }
      catch (EOFException e)
     {
        return;
     }//end catch
      catch (ClassNotFoundException e)
     {
        System.err.println("Unable to create object.");
     }//end catch 
     catch (IOException ioException)
     {
        System.err.println("Error reading from binary file.");
     }//end catch 

   }//readInputBinaryRecords
   public void closeBinaryOutputFile()
  {
  try
  {  if (output !=null )
        output.close();
  }//end try
  catch ( IOException ioException)
  {
     System.out.println( "Error closing file");
     System.exit(1);
  }//end catch
   }//end closeBinaryOutputFile

  public void closeBinaryInputFile()
 {
  try
  {  if (binInFile != null )
        binInFile.close();
     else
        System.out.println("No records were written to the binary file!");
  }//end try
  catch ( IOException ioException)
  {
     System.out.println( "Error closing file");
     System.exit(1);
  }//end catch
  }//end closeBinaryFile

 public static void main(String[] args)
{
 ItemRecordReport object = new ItemRecordReport();

 ItemRecord.printHeading();//print heading
 object.openOutputBinaryFile();//opens the binary file
 object.readInputTextRecords();//reads text file and writes to binary file
 object.closeBinaryOutputFile();//closes the binaryOutputFile

 object.openInputBinaryFile();//opens binary file
 object.readInputBinaryRecords();
 object.closeBinaryInputFile();
 Scanner inFile;
 ItemRecord[] itemRecord = new ItemRecord[5];


  itemRecord[x] = new ItemRecord();


  for (int x = 0; x <= 4; x++){
     itemRecord[x].setItemNumber("");
     itemRecord[x].setCost(0);
     itemRecord[x].setQuantity(0);
     itemRecord[x].setDescription("");
  }

  try
  {
     inFile = new Scanner(new File("itemRecord.txt"));

     while(inFile.hasNext())
      {
      for(int count =0; count<=4; count++)
        {
          itemRecord[count].setItemNumber(inFile.next());
          itemRecord[count].setCost(inFile.nextDouble());
          itemRecord[count].setQuantity(inFile.nextInt());
          itemRecord[count].setDescription(inFile.next());

     }//end for loop 
   }//end while

  }
  catch (IOException e)
  {
     System.out.println("Error file not found");
  }//end catch
  try
  {
  ObjectOutputStream binOutFile = new ObjectOutputStream
  (new FileOutputStream("ItemRecord_binary.txt"));
  }//end try
  catch (IOException e)
  {
  System.err.println("error opening file");
  }
  System.out.println(itemRecord.toString());



 }//end method main 



}//end class ItemRecordReport

这是文本文件:

A100 99.99 10 Canon PowerShot-135
A200 149.99 50 Panasonic-Lumix T55
A300 349.99 20 Nikon- D3200 DSRL
A400 280.99 30 Sony- DSC-W800
A500 97.99 20 Samsung- WB35F

这是更改字段值的新文本文件:

B100 98.00 10 ABC1010
B200 97.00 15 DEF1020
B300 96.00 10 GHI1030
B400 95.00 05 JKL1040
B500 94.00 01 MNO1050

Error stack trace
Item   Cost     Quantity  Description
Number
A100  $ 99.99   10        Canon PowerShot-135
A200  $149.99   50        Panasonic-Lumix T55
A300  $349.99   20        Nikon- D3200 DSRL
A400  $280.99   30        Sony- DSC-W800
A500  $ 97.99   20        Samsung- WB35F
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.endRecord(ItemRecordReport.java:165)
at ItemRecordReport.main(ItemRecordReport.java:156)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.
4

1 回答 1

0

这里有很多问题。这NullPointerException是因为您已经初始化了ItemRecord[] 数组,但没有初始化它的任何元素。您需要添加

itemRecord[x] = new ItemRecord();

在第 153 行之上。

然而还有其他问题。

  • 您正在编写自己的错误消息,而不是自己打印更有用的异常消息,或者,当您仍在调试时,打印堆栈跟踪。例如:

    catch (IOException ioException)
    {
        ioException.printStackTrace();
    }
    
  • 您正在捕获异常并继续进行,就好像它们没有发生一样。依赖于 try 块中代码成功的代码应该在同一个 try 块内。例如:

    try
    {
        input = new Scanner(new File("ItemRecord_text.txt"));
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }//end catch
    while (input.hasNext())
    // ...
    

    应该

    try
    {
        input = new Scanner(new File("ItemRecord_text.txt"));
        while (input.hasNext())
        // ...
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }//end catch
    
  • 您没有正确扫描“ItemRecord.txt”文件。它有五列,但您只扫描其中的四列。留给读者作为练习。

  • 我建议你改掉写作的习惯

    } // end catch
    

    等等。现代 IDE 和适当的缩进使这完全没有必要,实际上它在实践中会妨碍它们,而且它们经常会出错。你可能在学校就被教过,你当然应该做任何你必须做的事情来获得你想要的成绩,但是这里的人做的不多。几十年没见过了。

于 2014-12-15T00:14:58.590 回答