0

This is a college course assignment that creates a ItemRecord class and implements Serializable with getter and setters, then overrides the toString method. Next I created a ItemRecordReport class with a Scanner object mapped to an input file "ItemRecord_text.txt". Created an ObjectOutputStream object mapped to a binary output file "ItemRecord_binary.txt". Created an ObjectInputStream object mapped to the binary file. Everything works. I modified this post by putting in an array in method main and changing the field values with a additional text document. My error is a NullPointerException: This is the 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.

//create program so that objects of class can be serialized, implements interface Serialiable //create constructor with 4 parameters with accompanying get and set methods, Override toString method //create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream //create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods //modify ItemRecord object using toString method

 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

Here is the other test class: This class contain the ObjectOutputStream and ObjectInputStream objects. Therefore method main is here as well as the ItemRecordReport object that I use to call the methods in 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

And here is the text file:

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

This is the new text file to change the field values:

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.
swydell
  • 1,962
  • 8
  • 31
  • 44
  • Post the stack trace. Your question isn't complete without it, or answerable, which is why you got no answers. Edit it into your question. – user207421 Dec 14 '14 at 23:43
  • Thanks for your comment. But what is a stack trace. I'll research it. – swydell Dec 14 '14 at 23:48
  • The stack trace is the output you got that started with `NullPointerException` and continuing to show the lines of code that produced it. Post it all. – user207421 Dec 14 '14 at 23:55
  • Okay. That's what I'll do.Exception in thread "main" java.lang.NullPointerException at ItemRecordReport.main(ItemRecordReport.java:153) ----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete. – swydell Dec 15 '14 at 00:03
  • And tell us which is line 153. – user207421 Dec 15 '14 at 00:07
  • I'm using jgrasp and for some strange reason is is not generating line numbers so I can't tell what is on line 153. – swydell Dec 15 '14 at 00:19
  • You are going to have to solve that, otherwise you will never get anywhere with any of your assignments. – user207421 Dec 15 '14 at 00:20
  • I opened this file in Eclipse and Scanner inFile is on line 153 – swydell Dec 15 '14 at 00:24
  • That wouldn't cause an NPE. You'll find it was `itemRecord[x].setItemNumber("");`. See my answer. – user207421 Dec 15 '14 at 00:30

1 Answers1

0

There are numerous problems here. The NullPointerException comes from the fact that you've initialized the ItemRecord[] array but not any of its elements. You need to add

itemRecord[x] = new ItemRecord();

above line 153.

However there are other problems.

  • You are making up your own error messages instead of printing the much more useful exception messages themselves, or, as you are still debugging, printing the stack trace. For example:

    catch (IOException ioException)
    {
        ioException.printStackTrace();
    }
    
  • You are catching exceptions and proceeding as though they didn't happen. Code that depends on the success of code in a try block should be inside the same try block. For example:

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

    should be

    try
    {
        input = new Scanner(new File("ItemRecord_text.txt"));
        while (input.hasNext())
        // ...
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }//end catch
    
  • You aren't scanning the "ItemRecord.txt" file correctly. It has five columns but you're only scanning four of them. Left as an exercise for the reader.

  • I suggest you get out of the habit of writing

    } // end catch
    

    etcetera. Modern IDEs and proper indentation make this completley unnecessary, and it actually gets in the way in practice and they can often end up wrong. You've probably been taught it in school, and you should certainly do whatever you have to do to get the grades you want, but out here people don't do it much. Haven't seen it for decades.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Although I'm finishing up my first course in Java and hoping to at least to get a passing grade I feel that I'm under the wrong instructor because certain basic things like stack trace should have been covered. But I press on, learning by mistakes. I come to this forum to supplement my course. Thanks for your advice. Actually itemRecord has 4 columns. There are only 4 fields. In the last field or column there is a space between the alpha and numeric characters. I just deleted the space. As far as comments, yes we've been instructed to use comments. I'm taking your suggestions seriously. – swydell Dec 15 '14 at 02:10
  • To be clear, I'm not telling you not to use comments, I'm telling you that labelling every closing brace with a comment is unnecessary and in practice counter-productive. I learned that the hard way. You'll encounter a lot of 'style' advice, now and later. Make up your own mind about it. Some of it is useful;; some nonsense; some just today's fashion and there'll be another one along in a minute. – user207421 Dec 15 '14 at 02:38
  • Can you please tell my why I'm now getting a can't find symbol on the new code you suggested: itemRecord[x] = new ItemRecord(); Because my code was compiling but now it doesn't. – swydell Dec 18 '14 at 20:15
  • You must have removed the no-args constructor `ItemRecord()`. – user207421 Dec 18 '14 at 22:46
  • I removed the no argument constructor ItemRecord(). Now the error is NullPointerException. This assignment has already been turned in and graded. The class compiles but won't run. If I comment out everything after the end of method main it does run. But I'm trying to print out the modified field values and if I could just figure out how to solve the NullPointeException I could learn something. – swydell Dec 19 '14 at 01:40
  • Well now you have an NPE in `ItemRecordReport.endRecord()`, which you haven't posted. – user207421 Dec 19 '14 at 02:59