1 2 Previous Next 17 Replies Latest reply: Apr 2, 2007 8:16 PM by EJP RSS

    Problem with writing and reading using serialization

    807599
      I am having a problem with writing and reading an object that has another object in it. The purpose of the class is to write a order that has multiple items in it. And there will be several orders. This is for an IB project, where one of the requirements is to utilize a hierarchical composite data structure. That is, it is "one that contains more than one element and at least one of the elements is a composite data structure. Examples are, an array or linked list of records, a record that has one field that is another record, or an array". The code is shown below:

      The error produced is

      java.lang.NullPointerException
           at SamsonRubberIndustries.CustomerOrderDetails.createCustOrdDetailsScreen(CustomerOrderDetails.java:150)
           at SamsonRubberIndustries.CustomerOrderDetails$1.run(CustomerOrderDetails.java:78)
           at java.awt.event.InvocationEvent.dispatch(Unknown Source)
           at java.awt.EventQueue.dispatchEvent(Unknown Source)
           at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
           at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
           at java.awt.EventDispatchThread.run(Unknown Source)

      public class CustOrdObject implements Serializable {
      
           public int CustID;
           public int CustOrderID;
           public Object OrderDate;
           public InnerCustOrdObject[] innerCustOrdObj;
           public float GrandTotal;
           
           public int MaxItems;
           
           public CustOrdObject() {}
      
           public CustOrdObject(InnerCustOrdObject[] innerCustOrdObj, 
      int CustID, int CustOrderID, Object OrderDate, 
      float GrandTotal, int innerarrlength, int innerarrpos, int MaxItems) {
      
                this.CustID = CustID;
                this.CustOrderID = CustOrderID;
                this.OrderDate = OrderDate;
                this.GrandTotal = GrandTotal;          
                this.MaxItems = MaxItems;
                this.innerCustOrdObj = new InnerCustOrdObject[MaxItems];
      
           }
           
           public InnerCustOrdObject[] getInnerCustOrdObj() {
                
                return innerCustOrdObj;
                
           }
      
           public void setInnerCustOrdObj(InnerCustOrdObject[] innerCustOrdObj) {
                
                this.innerCustOrdObj = innerCustOrdObj;
                
           }
      
           public int getCustID() {
                
                return CustID;
                
           }
      
           public void setCustID(int custID) {
                
                CustID = custID;
                
           }
      
      
           public int getCustOrderID() {
                
                return CustOrderID;
                
           }
      
           public void setCustOrderID(int custOrderID) {
                
                CustOrderID = custOrderID;
                
           }
      
      
           public Object getOrderDate() {
                
                return OrderDate;
                
           }
      
           public void setOrderDate(Object orderDate) {
                
                OrderDate = orderDate;
                
           }
      
           public void setGrandTotal(float grandTotal) {
                
                GrandTotal = grandTotal;
                
           }
           
           public float getGrandTotal() {
                
                return GrandTotal;
                
           }
      
           public int getMaxItems() {
                
                return MaxItems;
                
           }
      
           public void setMaxItems(int maxItems) {
                
                MaxItems = maxItems;
                
           }
      
      }
      public class InnerCustOrdObject implements Serializable{
           
           public int ItemNumber;
           public float UnitPrice;
           public int QuantityRequired;
           public float TotalPrice;
      
           public InnerCustOrdObject() {}
      
           public InnerCustOrdObject(int ItemNumber, float 
      UnitPrice, int QuantityRequired, float TotalPrice){
      
                this.ItemNumber = ItemNumber;
                this.UnitPrice = UnitPrice;
                this.QuantityRequired = QuantityRequired;
                this.TotalPrice = TotalPrice;
                
           }
      
           public int getItemNumber() {
                
                return ItemNumber;
                
           }
      
           public void setItemNumber(int itemNumber) {
                
                ItemNumber = itemNumber;
                
           }
      
           public int getQuantityRequired() {
                
                return QuantityRequired;
                
           }
      
           public void setQuantityRequired(int quantityRequired) {
                
                QuantityRequired = quantityRequired;
                
           }
      
           public float getTotalPrice() {
                
                return TotalPrice;
                
           }
      
           public void setTotalPrice(float totalPrice) {
                
                TotalPrice = totalPrice;
                
           }
      
           public float getUnitPrice() {
                
                return UnitPrice;
                
           }
      
           public void setUnitPrice(float unitPrice) {
                
                UnitPrice = unitPrice;
                
           }
      
      }
      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      
      import java.text.SimpleDateFormat;
      import java.util.Date;
      
      import javax.swing.*;
      
      import javax.swing.table.DefaultTableCellRenderer;
      import javax.swing.table.DefaultTableModel;
      
      public class CustomerOrderDetails extends CommonFeatures{
      
           //TODO
           private static int MAX_ORDERS = 200;
           private static int MAX_ORDERITEMS = 100;
           private static int MaxRecord;
           private static int CurrentRecord = 1;
           private static int currentItem;
           private static int MaxItems;
           private static boolean FileExists, recFileExists;
      
           private static CustOrdObject[] orderDetails = new CustOrdObject[MAX_ORDERS];
           private static InnerCustOrdObject[] innerCustOrdObj = new InnerCustOrdObject[MAX_ORDERITEMS];     
      
           private static File OrderDetailsFile = new File("CustOrdDetails.dat");
           private static File OrdRecordNumStore = new File("OrdRecordNumStore.txt");
           private static PrintWriter writeFile;
           private static BufferedReader readFile;
           private static ObjectOutputStream objOut;
           private static ObjectInputStream objIn;
      
           //Set format for date
           SimpleDateFormat simpleDF = new SimpleDateFormat("dd MM yyyy");
      
           //--<BEGINNING>--Declaring Interface Variables------------------------------------------//
      
           private JPanel innertoppanel, innercenterpanel, innerbottompanel, innerrightpanel, innerleftpanel;
      
           private JLabel CustIDLbl, CustOrderIDLbl, OrderedDateLbl, GrandTotLbl, ItemNumberLbl,UnitPriceLbl, QuantityReqLbl, TotPriceLbl;
      
           private JTextField CustIDTxt, CustOrderIDTxt, OrderedDateTxt, GrandTotTxt, ItemNumberTxt, UnitPriceTxt, QuantityReqTxt, TotPriceTxt;
      
           private JButton addrecordbtn, savebtn, externalprevbtn, externalnextbtn, internalprevbtn, internalnextbtn, gotorecordbtn, additemreqbtn;
      
           //--<END>--Declaring Interface Variables------------------------------------------------//
      
           public static void main(String[] args) {
      
                final CustomerOrderDetails COD = new CustomerOrderDetails();
      
                java.awt.EventQueue.invokeLater(new Runnable() {
      
                     public void run() {
      
                          try {
      
                               UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                               COD.createCustOrdDetailsScreen();
      
                          } catch (Exception eb) {
      
                               eb.printStackTrace();
      
                          }
      
                     }
      
                });
      
           }
      
           //--<BEGINNING>--Creating CustomerOrderDetails Screen---------------------------------------//
           public JFrame createCustOrdDetailsScreen() {
      
                createDefaultFrame();
                mainframe.setSize(800,500);
      
                createContainerPanel();
                containerpanel.add(createCustOrdDetailsTitle(), BorderLayout.NORTH);
                containerpanel.add(createCustOrdDetailsMainPanel(), BorderLayout.CENTER);
                //containerpanel.add(createCustOrdDetailsLeftNavButtons(), BorderLayout.WEST);
                //containerpanel.add(createCustOrdDetailsRightNavButtons(), BorderLayout.EAST);
                containerpanel.add(createCustOrdDetailsButtons(), BorderLayout.SOUTH);
      
                mainframe.setContentPane(containerpanel);
      
                mainframe.setLocationRelativeTo(null);
                mainframe.setVisible(true);
      
      
                //--<BEGINNING>--Checks to see whether CRecordNumberStore file exists-------------------------------//
                if (OrdRecordNumStore.exists() == true) {
      
                     recFileExists = true;
      
      
                }else {
      
                     recFileExists = false;
      
                }
      
                if (recFileExists == true) {
      
                     MaxRecord = readRecordNumber(); 
      
                     CurrentRecord = MaxRecord;
                     //readOrder();
                     //readInnerOrderRecord(CurrentRecord);
                     System.out.println("Current Record " +CurrentRecord);
                     System.out.println("Max Record " +MaxRecord);
      
                }else{
      
                     MaxRecord = 1;
      
                     writeRecordNumber(MaxRecord);
                     CustOrderIDTxt.setText(""+MaxRecord);
      
                     System.out.println("Current Record " +CurrentRecord);
                     System.out.println("Max Record " +MaxRecord);
      
      
                }
                //--<END>--Checks to see whether CRecordNumberStore file exists--------------------------------------//
      
                if(readOrder() != null){
      
                     orderDetails = (CustOrdObject[]) readOrder();
                   innerCustOrdObj = orderDetails[CurrentRecord].getInnerCustOrdObj();
                     MaxItems = orderDetails[CurrentRecord].getMaxItems();
      
                     if(CurrentRecord > 1 && CurrentRecord < MaxRecord){
      
                          externalnextbtn.setEnabled(true);
                          externalprevbtn.setEnabled(true);
      
                     }
      
                     if(CurrentRecord >= MaxRecord){
      
                          externalnextbtn.setEnabled(false);
      
                     }
      
                     getFieldText(CurrentRecord-1);
      
                }else{
                     
                     
                     orderDetails[CurrentRecord] = new CustOrdObject();
                     currentItem = 1;
                     
                }
      
                return mainframe;
      
           }
           //--<END>--Creating CustomerOrderDetails Screen---------------------------------------------//
      
           public JPanel createCustOrdDetailsTitle(){
      
                createTitlePanel();
      
                titlepanel.setBackground(TxtfontColor);
      
                label.setText("- Customer Order Details -");
                labelpanel.setBackground(TxtfontColor);
                label.setForeground(Color.white);
      
                createbuttonpanel();
                buttonpanel.setBackground(TxtfontColor) ;
      
      
                buttonpanel.add(createReturnToMainMenuButton());
                titlepanel.add(labelpanel, BorderLayout.WEST);
                titlepanel.add(buttonpanel, BorderLayout.EAST);
      
                return titlepanel;
      
           }
      
           public JPanel createCustOrdDetailsMainPanel(){
      
                createmainpanel();
      
                mainpanel.setBackground(TxtfontColor);
      
                mainpanel.setLayout(new BorderLayout());          
                mainpanel.setBorder(BorderFactory.createTitledBorder(""));
      
                mainpanel.add(createInnerTopPanel(), BorderLayout.NORTH);
                mainpanel.add(createInnerCenterPanel(), BorderLayout.CENTER);
                mainpanel.add(createInnerBottomPanel(), BorderLayout.SOUTH);
                mainpanel.add(createInnerRightPanel(), BorderLayout.EAST);
                mainpanel.add(createInnerLeftPanel(), BorderLayout.WEST);
      
                return mainpanel;
      
           }
      
           public JPanel createInnerTopPanel(){
      
                innertoppanel = new JPanel(new GridBagLayout());
                innertoppanel.setBackground(TxtfontColor);
                GridBagConstraints GBC = new GridBagConstraints();
                GBC.fill = GridBagConstraints.HORIZONTAL;
      
                //Setting Font Type and Size
                Font font = new Font("Arial", Font.BOLD, 11);
      
                CustIDLbl = new JLabel("Customer ID");
                CustIDLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                CustIDLbl.setFont(font);
                CustIDLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 1;
                innertoppanel.add(CustIDLbl, GBC);     
      
                CustIDTxt = new JTextField(20);
                CustIDTxt.setEditable(true);
                GBC.gridx = 2;
                GBC.gridy = 1;
                innertoppanel.add(CustIDTxt, GBC);
      
                GBC.gridx = 3;
                GBC.gridy = 1;
                innertoppanel.add(Box.createHorizontalStrut(220), GBC);
      
                OrderedDateLbl = new JLabel("Order Date");
                OrderedDateLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                OrderedDateLbl.setFont(font);
                OrderedDateLbl.setForeground(LblfontColor);
                GBC.gridx = 4;
                GBC.gridy = 1;
                innertoppanel.add(OrderedDateLbl, GBC);     
      
                //Get today's date
                Date todaydate = new Date();
      
                OrderedDateTxt = new JTextField(simpleDF.format(todaydate), 20);
                OrderedDateTxt.setHorizontalAlignment(JTextField.CENTER);
                OrderedDateTxt.setEditable(false);
                GBC.gridx = 5;
                GBC.gridy = 1;
                innertoppanel.add(OrderedDateTxt, GBC);
      
                CustOrderIDLbl = new JLabel("Customer Order ID");
                CustOrderIDLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                CustOrderIDLbl.setFont(font);
                CustOrderIDLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 2;
                innertoppanel.add(CustOrderIDLbl, GBC);
      
                CustOrderIDTxt = new JTextField(20);
                CustOrderIDTxt.setEditable(false);
                GBC.gridx = 2;
                GBC.gridy = 2;
                innertoppanel.add(CustOrderIDTxt, GBC);
      
                return innertoppanel;
           }
      
           public JPanel createInnerCenterPanel(){
      
                innercenterpanel = new JPanel(new GridBagLayout());
                innercenterpanel.setBackground(TxtfontColor);
                innercenterpanel.setBorder(BorderFactory.createLoweredBevelBorder());
                GridBagConstraints GBC = new GridBagConstraints();
                GBC.fill = GridBagConstraints.HORIZONTAL;
      
                //Setting Font Type and Size
                Font font = new Font("Arial", Font.BOLD, 11);
      
                ItemNumberLbl = new JLabel("Item Number");
                ItemNumberLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                ItemNumberLbl.setFont(font);
                ItemNumberLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 1;
                innercenterpanel.add(ItemNumberLbl, GBC);     
      
                ItemNumberTxt = new JTextField(20);
                GBC.gridx = 2;
                GBC.gridy = 1;
                innercenterpanel.add(ItemNumberTxt, GBC);
      
                UnitPriceLbl = new JLabel("Unit Price");
                UnitPriceLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                UnitPriceLbl.setFont(font);
                UnitPriceLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 2;
                innercenterpanel.add(UnitPriceLbl, GBC);     
      
                UnitPriceTxt = new JTextField(20);
                //UnitPriceTxt.setEditable(false);
                GBC.gridx = 2;
                GBC.gridy = 2;
                innercenterpanel.add(UnitPriceTxt, GBC);
      
                QuantityReqLbl = new JLabel("Quantity Required");
                QuantityReqLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                QuantityReqLbl.setFont(font);
                QuantityReqLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 3;
                innercenterpanel.add(QuantityReqLbl, GBC);     
      
                QuantityReqTxt = new JTextField(20);
                //QuantityReqTxt.setEditable(false);
                GBC.gridx = 2;
                GBC.gridy = 3;
                innercenterpanel.add(QuantityReqTxt, GBC);
      
                TotPriceLbl = new JLabel("Total Price");
                TotPriceLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                TotPriceLbl.setFont(font);
                TotPriceLbl.setForeground(LblfontColor);
                GBC.gridx = 1;
                GBC.gridy = 4;
                innercenterpanel.add(TotPriceLbl, GBC);     
      
                TotPriceTxt = new JTextField(20);
                //TotPriceTxt.setEditable(false);
                GBC.gridx = 2;
                GBC.gridy = 4;
                innercenterpanel.add(TotPriceTxt, GBC);
      
                return innercenterpanel;
           }
      
           public JPanel createInnerBottomPanel(){
      
                innerbottompanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
                innerbottompanel.setBackground(TxtfontColor);
      
                //Setting Font Type and Size
                Font font = new Font("Arial", Font.BOLD, 11);
      
                GrandTotLbl = new JLabel("Grand Total");
                GrandTotLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                GrandTotLbl.setFont(font);
                GrandTotLbl.setForeground(LblfontColor);
                innerbottompanel.add(GrandTotLbl);
      
                innerbottompanel.add(Box.createHorizontalStrut(30));
      
                GrandTotTxt = new JTextField(20);
                innerbottompanel.add(GrandTotTxt);
      
                return innerbottompanel;
      
           }
      
           public JPanel createInnerRightPanel(){
      
                innerrightpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                innerrightpanel.setBackground(TxtfontColor);
      
                innerrightpanel.setLayout(new BoxLayout(navrightpanel, BoxLayout.Y_AXIS));
                innerrightpanel.setBorder(BorderFactory.createLoweredBevelBorder());
      
                innerrightpanel.setLayout(new GridBagLayout());          
                GridBagConstraints GBC = new GridBagConstraints();
                GBC.fill = GridBagConstraints.HORIZONTAL;
      
                internalnextbtn = new JButton(createNextButtonIcon());
                GBC.gridx = 1;
                GBC.gridy = 1;
                internalnextbtn.addActionListener(new ActionListener(){
      
                     public void actionPerformed(ActionEvent evt){
      
                          //getInnerFieldText(currentItem);
      
                          internalprevbtn.setEnabled(true);
      
                          if(currentItem < MaxItems){
      
                               ++CurrentRecord;
                               //readOrder();
                               //readInnerOrderRecord(CurrentRecord);
                               setInnerFieldText(currentItem);
                               System.out.println(CurrentRecord);//Checking RECORD_NUM
      
                          }
      
                          if(currentItem == MaxItems){
      
                               internalnextbtn.setEnabled(false);
      
                          }
      
                     }
      
                });
      
                innerrightpanel.add(internalnextbtn, GBC);
      
                return innerrightpanel;
           }
      
           public JPanel createInnerLeftPanel(){
      
                innerleftpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                innerleftpanel.setBackground(TxtfontColor);
      
                innerleftpanel.setBorder(BorderFactory.createLoweredBevelBorder());
                innerleftpanel.setForeground(Color.BLACK);
      
                innerleftpanel.setLayout(new GridBagLayout());          
                GridBagConstraints GBC = new GridBagConstraints();
                GBC.fill = GridBagConstraints.HORIZONTAL;
      
                internalprevbtn = new JButton(createPreviousButtonIcon());
                GBC.gridx = 1;
                GBC.gridy = 1;
                internalprevbtn.addActionListener(new ActionListener(){
      
                     public void  actionPerformed(ActionEvent evt){
      
                          //getInnerFieldText(currentItem);
      
                          internalnextbtn.setEnabled(true);
      
                          if(currentItem == 1){
      
                               internalprevbtn.setEnabled(false);
      
                          }
      
                          if(currentItem > 0){
      
                               --currentItem;
                               //readOrder();
                               setInnerFieldText(currentItem);
      
                          }
      
                     }
      
                });
      
                innerleftpanel.add(internalprevbtn, GBC);
      
      
                return innerleftpanel;
           }
      
           public JPanel createCustOrdDetailsButtons(){
      
                createbuttonpanel();
                buttonpanel.setBackground(TxtfontColor);
      
                externalprevbtn = new JButton(createPreviousButtonIcon());
                externalprevbtn.addActionListener(new ActionListener(){
      
                     public void  actionPerformed(ActionEvent evt){
      
                          getFieldText(CurrentRecord);
      
                          externalnextbtn.setEnabled(true);
      
                          if(CurrentRecord == 1){
      
                               externalprevbtn.setEnabled(false);
      
                          }
      
                          if(CurrentRecord > 0){
      
                               --CurrentRecord;
                               setFieldText(CurrentRecord);
                               System.out.println(CurrentRecord);//Checking RECORD_NUM
      
      
                          }
      
                     }
      
                });
                buttonpanel.add(externalprevbtn);
      
                addrecordbtn = new JButton("Add Record", createAddButtonIcon());
                addrecordbtn.addActionListener(new ActionListener(){
      
                     public void actionPerformed(ActionEvent evt){
      
                          try{
      
                               MaxRecord = readRecordNumber();
                               MaxRecord++;
                               writeRecordNumber(MaxRecord);
      
                               //--<BEGINNING>--Clear Fields-------------------------------------------------------//
      
                               CustIDTxt.setText("");
                               CustOrderIDTxt.setText(""+MaxRecord);
      
                               //Get today's date
                               Date todaydate = new Date();
      
                               OrderedDateTxt.setText(""+simpleDF.format(todaydate));
                               ItemNumberTxt.setText("");
                               UnitPriceTxt.setText("");
                               QuantityReqTxt.setText("");
                               TotPriceTxt.setText("");
                               GrandTotTxt.setText("");
      
                               //--<END>--Clear Fields-------------------------------------------------------------//
      
      
                               externalnextbtn.setEnabled(false);
                               externalprevbtn.setEnabled(true);
                               System.out.println(MaxRecord);
      
      
                          } catch(Exception ec){ec.printStackTrace();}
                     }
      
                });
                buttonpanel.add(addrecordbtn);
      
                savebtn = new JButton("Save Data", createSaveButtonIcon());
                savebtn.addActionListener(new ActionListener(){
      
                     public void actionPerformed(ActionEvent evt){
      
                          setFieldText(CurrentRecord);
                          writeOrder();
                          writeRecordNumber(MaxRecord);
                          System.out.println(CurrentRecord);
                          System.out.println(MaxRecord);
      
                     }
      
                });
                buttonpanel.add(savebtn);
      
                java.net.URL imageURL_AddRowIcon = CommonFeatures.class.getResource("Icons/edit_add.png");
                ImageIcon AddRowIcon = new ImageIcon(imageURL_AddRowIcon);
                additemreqbtn = new JButton("Add Item", AddRowIcon);
                additemreqbtn.addActionListener(new ActionListener(){
      
                     public void actionPerformed(ActionEvent evt){
      
                          try{
      
                               //--<BEGINNING>--Clear Fields-------------------------------------------------------//
      
                               ItemNumberTxt.setText("");
                               UnitPriceTxt.setText("");
                               QuantityReqTxt.setText("");
                               TotPriceTxt.setText("");
      
                               //--<END>--Clear Fields-------------------------------------------------------------//
      
                               //CurrentRecord = MaxRecord;
      
                               currentItem++;
                               setInnerFieldText(currentItem);
      
                               internalnextbtn.setEnabled(false);
                               internalprevbtn.setEnabled(true);
                               System.out.println(MaxRecord);
      
      
                          } catch(Exception ec){ec.printStackTrace();}
                     }
      
                });
                buttonpanel.add(additemreqbtn);
      
                externalnextbtn = new JButton(createNextButtonIcon());
                externalnextbtn.addActionListener(new ActionListener(){
      
                     public void actionPerformed(ActionEvent evt){
      
                          getFieldText(CurrentRecord);
      
                          externalprevbtn.setEnabled(true);
      
                          if(CurrentRecord < MaxRecord){
      
                               ++CurrentRecord;
                               setFieldText(CurrentRecord);
                               System.out.println(CurrentRecord);//Checking RECORD_NUM
      
                          }
      
                          if(CurrentRecord == MaxRecord){
      
                               externalnextbtn.setEnabled(false);
      
                          }
      
                     }
      
                });
                buttonpanel.add(externalnextbtn);
      
      
                return buttonpanel;
      
           }
      
           //TODO
      
           public void setFieldText(int orderID){//TODO
      
                orderDetails[orderID].setCustID(Integer.parseInt(CustIDTxt.getText()));
                orderDetails[orderID].setCustOrderID(Integer.parseInt(CustOrderIDTxt.getText()));
                orderDetails[orderID].setOrderDate(OrderedDateTxt.getText());
                orderDetails[orderID].setInnerCustOrdObj(innerCustOrdObj);
      
                orderDetails[orderID].setMaxItems(MaxItems);
      
                setInnerFieldText(currentItem);
      
                orderDetails[orderID].setGrandTotal(Float.parseFloat(GrandTotTxt.getText()));
      
           }
      
           public void setInnerFieldText(int currentItem){//TODO
      
                innerCustOrdObj[currentItem] = new InnerCustOrdObject();
      
                innerCustOrdObj[currentItem].setItemNumber(Integer.parseInt(ItemNumberTxt.getText()));
                innerCustOrdObj[currentItem].setUnitPrice(Float.parseFloat(UnitPriceTxt.getText()));
                innerCustOrdObj[currentItem].setQuantityRequired(Integer.parseInt(QuantityReqTxt.getText()));
                innerCustOrdObj[currentItem].setTotalPrice(Float.parseFloat(TotPriceTxt.getText()));
      
           }
      
           public void getFieldText(int orderID){
      
                CustIDTxt.setText(Integer.toString(orderDetails[orderID].getCustID()));
                CustOrderIDTxt.setText(Integer.toString(orderDetails[orderID].getCustOrderID()));
                OrderedDateTxt.setText(""+orderDetails[orderID].getOrderDate());          
      
                currentItem = orderDetails[orderID].getMaxItems();
                System.err.println("currentItem" + currentItem);
                getInnerFieldText(currentItem);
      
                GrandTotTxt.setText(Float.toString(orderDetails[orderID].getGrandTotal()));
      
      
           }
      
           public void getInnerFieldText(int currentItem){
      
                ItemNumberTxt.setText(Integer.toString(innerCustOrdObj[currentItem].getItemNumber()));
                UnitPriceTxt.setText(Float.toString(innerCustOrdObj[currentItem].getUnitPrice()));
                QuantityReqTxt.setText(Integer.toString(innerCustOrdObj[currentItem].getQuantityRequired()));
                TotPriceTxt.setText(Float.toString(innerCustOrdObj[currentItem].getTotalPrice()));
      
           }
      
           public void writeOrder(){//TODO
      
      
                try {
      
                     objOut = new ObjectOutputStream(new FileOutputStream(OrderDetailsFile));
                     objOut.writeObject(orderDetails);
                     System.out.println("WORKING!");
                     objOut.flush();
                     objOut.close();
      
                } catch (IOException e) {
      
                     e.printStackTrace();
      
                }
      
           }
      
           public Object readOrder(){
      
                Object temporaryObj;
      
                try{
      
                     objIn = new ObjectInputStream(new FileInputStream(OrderDetailsFile));
                     temporaryObj = objIn.readObject();               
                     CustOrdObject[] blah = (CustOrdObject[]) temporaryObj;
                     System.out.println("Outer: "+blah[1].getCustID());
                     InnerCustOrdObject[] whee = blah[1].getInnerCustOrdObj();
                     System.out.println("Inner: "+whee[1].getItemNumber());
                     objIn.close();
      
                     System.out.println("Read Worky!");
      
                     return temporaryObj;
      
                }catch(Exception e){
      
                     e.printStackTrace(); 
      
                     System.out.println("Read No Worky!");
      
                     return null;
      
      
                }
      
           }
      
      
           public void writeRecordNumber(int MaxRecord){
      
                try{
      
                     objOut = new ObjectOutputStream(new FileOutputStream(OrdRecordNumStore));
                     objOut.writeObject(MaxRecord);
      
                     System.out.println("WORKING!");
      
                     objOut.flush();
                     objOut.close();
      
                }catch(Exception e){e.printStackTrace();}
      
           }
      
           public int readRecordNumber() {
                try {
      
                     objIn = new ObjectInputStream(new FileInputStream(OrdRecordNumStore));
      
                     int temporaryObj = Integer.parseInt(objIn.readObject().toString());
                     objIn.close();
      
                     System.out.println("Read Number Worky!");
      
                     return temporaryObj;
      
                } catch (Exception e) {
      
                     e.printStackTrace();
      
                     System.out.println("Read Number No Worky!");
      
                     return -1;
                }
      
           }
      
      }
      Message was edited by:
      Kilik07

      Message was edited by:
      Kilik07
        • 1. Re: Problem with writing and reading using serialization
          800649
          hi,

          could you post only the code line # 150 of the CustomerOrderDetails class plz ?
          • 2. Re: Problem with writing and reading using serialization
            807599
            thats the line with the error

            innerCustOrdObj = orderDetails[CurrentRecord].getInnerCustOrdObj();
            • 3. Re: Problem with writing and reading using serialization
              800649
              I saw a run method in your code. Does this mean that you use threads ?
              • 4. Re: Problem with writing and reading using serialization
                807599
                Technically speaking not really, All I've done is added the swing display code into the main event dispatch thread and executed it there.
                • 5. Re: Problem with writing and reading using serialization
                  800649
                  add some tests before the code line # 150 to identify the null object like the following:

                  if(myobject==null){
                  System.out.println("myobject is null !!");
                  }

                  also check the array object : is null ? length (if not null)....

                  hth
                  • 6. Re: Problem with writing and reading using serialization
                    807599
                    I tried the following code before line #150:
                    if(orderDetails == null){
                                        
                                        System.err.println("CustomerOrderObj is null !!");
                                        
                                   }else{
                                        
                                        System.err.println("CustomerOrderObj is  not null !!");
                                        
                                   }
                                   if(orderDetails[CurrentRecord].getInnerCustOrdObj() == null){
                                        
                                        System.err.println("InnerCustomerOrderObj is null !!");
                                        
                                   }else{
                                        
                                        System.err.println("InnerCustomerOrderObj is  not null !!");
                                        
                                   }
                    but nothing is output in the console window..

                    Edit: Actually, 'CustomerOrderObj is not null !!' does show up..

                    Message was edited by:
                    Kilik07
                    • 7. Re: Problem with writing and reading using serialization
                      807599
                      altho the error seems to be produced at line #150, i think there's something wrong with my set and get methods...plz check those me tnk u
                      • 8. Re: Problem with writing and reading using serialization
                        EJP
                        if(orderDetails == null){
                        
                                            
                        System.err.println("CustomerOrderObj is null
                        null !!");
                                            
                                       }else{
                                            
                        System.err.println("CustomerOrderObj is not null
                        null !!");
                                            
                                       }
                                       if(orderDetails[CurrentRecord].getInnerCustOrdObj()
                        == null){
                        Before you do this you need to test whether orderDetails[CurrentRecord] is nyull, which it evidently is.
                        • 9. Re: Problem with writing and reading using serialization
                          807599
                          I did the following to check whether orderDetails[CurrentRecord] is null
                          orderDetails = (CustOrdObject[]) readOrder();
                                         if(orderDetails[CurrentRecord] == null){
                                              
                                              System.err.println("CustomerOrderObj 1 is null !!");
                                              
                                         }else{
                                              
                                              System.err.println("CustomerOrderObj 1 is  not null !!");
                                              
                                         }
                                         if(orderDetails == null){
                                              
                                              System.err.println("CustomerOrderObj is null !!");
                                              
                                         }else{
                                              
                                              System.err.println("CustomerOrderObj is  not null !!");
                                              
                                         }
                                         if(orderDetails[CurrentRecord].getInnerCustOrdObj() == null){
                                              
                                              System.err.println("InnerCustomerOrderObj is null !!");
                                              
                                         }else{
                                              
                                              System.err.println("InnerCustomerOrderObj is  not null !!");
                                              
                                         }
                                         innerCustOrdObj = orderDetails[CurrentRecord].getInnerCustOrdObj();
                          It was found to be null...
                          • 10. Re: Problem with writing and reading using serialization
                            807599
                            do u want the commonfeatures class so that u can run it urself and see the problem?

                            plz someone help..i need to get this coding done by today...

                            Message was edited by:
                            Kilik07
                            • 11. Re: Problem with writing and reading using serialization
                              EJP
                              It would make more sense to check orderDetails for null before you check orderDetails[CurrentOrder] for being null, as I suggested above. Otherwise you're just in for another NPE.

                              As to why anything is null that shouldn't be, that's your problem.
                              • 12. Re: Problem with writing and reading using serialization
                                807599
                                orderDetails isn't null, I can read from the file fine when I hardcode CurrentRecord to 1. However, this displays the second record in the array, which it shouldn't be doing as I'm not using a 0-indexed array..
                                • 13. Re: Problem with writing and reading using serialization
                                  EJP
                                  OK, so if orderDetails isn't null you don't need to test for it. But if you need the test you need it to be in the right place and it isn't.
                                  • 14. Re: Problem with writing and reading using serialization
                                    807599
                                    ok i got reading to work to a certain extent... but the prob is i cnt seem to save my innerCustOrdObj proprly...when ever i look for a record using the gotorecordbtn, the outerobject, which is the orderDetails, seems to change but the innerCustOrdObj remains the same... heres the new code..
                                    public class CustomerOrderDetails extends CommonFeatures{
                                    
                                         //TODO
                                         private static int MAX_ORDERS = 200;
                                         private static int MAX_ORDERITEMS = 100;
                                         private static int MaxRecord;
                                         private static int CurrentRecord = 1;
                                         private static int currentItem;
                                         private static int MaxItems = 1;
                                         private static boolean FileExists, recFileExists;
                                         private static boolean RecordExists;
                                    
                                         private static CustOrdObject[] orderDetails = new CustOrdObject[MAX_ORDERS];
                                         private static InnerCustOrdObject[] innerCustOrdObj = new InnerCustOrdObject[MAX_ORDERITEMS];     
                                    
                                         private static File OrderDetailsFile = new File("CustOrdDetails.ser");
                                         private static File OrdRecordNumStore = new File("OrdRecordNumStore.txt");
                                         private static PrintWriter writeFile;
                                         private static BufferedReader readFile;
                                         private static ObjectOutputStream objOut;
                                         private static ObjectInputStream objIn;
                                    
                                         //Set format for date
                                         SimpleDateFormat simpleDF = new SimpleDateFormat("dd MM yyyy");
                                    
                                         //--<BEGINNING>--Declaring Interface Variables------------------------------------------//
                                    
                                         private JPanel innertoppanel, innercenterpanel, innerbottompanel, innerrightpanel, innerleftpanel;
                                    
                                         private JLabel CustIDLbl, CustOrderIDLbl, OrderedDateLbl, GrandTotLbl, ItemNumberLbl,UnitPriceLbl, QuantityReqLbl, TotPriceLbl;
                                    
                                         private JTextField CustIDTxt, CustOrderIDTxt, OrderedDateTxt, GrandTotTxt, ItemNumberTxt, UnitPriceTxt, QuantityReqTxt, TotPriceTxt;
                                    
                                         private JButton addrecordbtn, savebtn, externalprevbtn, externalnextbtn, internalprevbtn, internalnextbtn, gotorecordbtn, additemreqbtn;
                                    
                                         //--<END>--Declaring Interface Variables------------------------------------------------//
                                    
                                         public static void main(String[] args) {
                                    
                                              final CustomerOrderDetails COD = new CustomerOrderDetails();
                                    
                                              java.awt.EventQueue.invokeLater(new Runnable() {
                                    
                                                   public void run() {
                                    
                                                        try {
                                    
                                                             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                                                             COD.createCustOrdDetailsScreen();
                                    
                                                        } catch (Exception eb) {
                                    
                                                             eb.printStackTrace();
                                    
                                                        }
                                    
                                                   }
                                    
                                              });
                                    
                                         }
                                    
                                         //--<BEGINNING>--Creating CustomerOrderDetails Screen---------------------------------------//
                                         public JFrame createCustOrdDetailsScreen() {
                                    
                                              createDefaultFrame();
                                              mainframe.setSize(800,500);
                                    
                                              createContainerPanel();
                                              containerpanel.add(createCustOrdDetailsTitle(), BorderLayout.NORTH);
                                              containerpanel.add(createCustOrdDetailsMainPanel(), BorderLayout.CENTER);
                                              //containerpanel.add(createCustOrdDetailsLeftNavButtons(), BorderLayout.WEST);
                                              //containerpanel.add(createCustOrdDetailsRightNavButtons(), BorderLayout.EAST);
                                              containerpanel.add(createCustOrdDetailsButtons(), BorderLayout.SOUTH);
                                    
                                              mainframe.setContentPane(containerpanel);
                                    
                                              mainframe.setLocationRelativeTo(null);
                                              mainframe.setVisible(true);
                                    
                                    
                                              //--<BEGINNING>--Checks to see whether CRecordNumberStore file exists-------------------------------//
                                              if (OrdRecordNumStore.exists() == true) {
                                    
                                                   recFileExists = true;
                                    
                                    
                                              }else {
                                    
                                                   recFileExists = false;
                                    
                                              }
                                    
                                              if (recFileExists == true) {
                                    
                                                   MaxRecord = readRecordNumber(); 
                                    
                                                   CurrentRecord = MaxRecord;
                                    
                                                   //readOrder();
                                                   //readInnerOrderRecord(CurrentRecord);
                                                   System.out.println("Current Record " +CurrentRecord);
                                                   System.out.println("Max Record " +MaxRecord);
                                    
                                              }else{
                                    
                                                   MaxRecord = 1;
                                    
                                                   writeRecordNumber(MaxRecord);
                                                   CustOrderIDTxt.setText(""+MaxRecord);
                                    
                                                   System.out.println("Current Record " +CurrentRecord);
                                                   System.out.println("Max Record " +MaxRecord);
                                    
                                    
                                              }
                                              //--<END>--Checks to see whether CRecordNumberStore file exists--------------------------------------//
                                    
                                              if(readOrder() != null){
                                    
                                    
                                                   orderDetails = (CustOrdObject[]) readOrder();
                                                   //CurrentRecord--;
                                                   //System.out.println("Current Rec Here"+CurrentRecord);
                                                   if(orderDetails[CurrentRecord] == null){
                                    
                                                        System.err.println("CustomerOrderObj 1 is null !!");
                                    
                                                   }else{
                                    
                                                        System.err.println("CustomerOrderObj 1 is  not null !!");
                                    
                                                   }
                                                   if(orderDetails[CurrentRecord].getInnerCustOrdObj() == null){
                                    
                                                        System.err.println("InnerCustomerOrderObj is null !!");
                                    
                                                   }else{
                                    
                                                        System.err.println("InnerCustomerOrderObj is  not null !!");
                                    
                                                   }
                                                   innerCustOrdObj = orderDetails[CurrentRecord].getInnerCustOrdObj();
                                                   MaxItems = orderDetails[CurrentRecord].getMaxItems();
                                    
                                                   if(CurrentRecord > 1 && CurrentRecord < MaxRecord){
                                    
                                                        externalnextbtn.setEnabled(true);
                                                        externalprevbtn.setEnabled(true);
                                    
                                                   }
                                    
                                                   if(CurrentRecord >= MaxRecord){
                                    
                                                        externalnextbtn.setEnabled(false);
                                    
                                                   }
                                    
                                                   getFieldText(CurrentRecord);
                                                   getInnerFieldText(MaxItems);
                                    
                                              }else{
                                    
                                    
                                                   orderDetails[CurrentRecord] = new CustOrdObject();
                                                   currentItem = 1;
                                    
                                              }
                                    
                                              return mainframe;
                                    
                                         }
                                         //--<END>--Creating CustomerOrderDetails Screen---------------------------------------------//
                                    
                                         public JPanel createCustOrdDetailsTitle(){
                                    
                                              createTitlePanel();
                                    
                                              titlepanel.setBackground(TxtfontColor);
                                    
                                              label.setText("- Customer Order Details -");
                                              labelpanel.setBackground(TxtfontColor);
                                              label.setForeground(Color.white);
                                    
                                              createbuttonpanel();
                                              buttonpanel.setBackground(TxtfontColor) ;
                                    
                                    
                                              buttonpanel.add(createReturnToMainMenuButton());
                                              titlepanel.add(labelpanel, BorderLayout.WEST);
                                              titlepanel.add(buttonpanel, BorderLayout.EAST);
                                    
                                              return titlepanel;
                                    
                                         }
                                    
                                         public JPanel createCustOrdDetailsMainPanel(){
                                    
                                              createmainpanel();
                                    
                                              mainpanel.setBackground(TxtfontColor);
                                    
                                              mainpanel.setLayout(new BorderLayout());          
                                              mainpanel.setBorder(BorderFactory.createTitledBorder(""));
                                    
                                              mainpanel.add(createInnerTopPanel(), BorderLayout.NORTH);
                                              mainpanel.add(createInnerCenterPanel(), BorderLayout.CENTER);
                                              mainpanel.add(createInnerBottomPanel(), BorderLayout.SOUTH);
                                              mainpanel.add(createInnerRightPanel(), BorderLayout.EAST);
                                              mainpanel.add(createInnerLeftPanel(), BorderLayout.WEST);
                                    
                                              return mainpanel;
                                    
                                         }
                                    
                                         public JPanel createInnerTopPanel(){
                                    
                                              innertoppanel = new JPanel(new GridBagLayout());
                                              innertoppanel.setBackground(TxtfontColor);
                                              GridBagConstraints GBC = new GridBagConstraints();
                                              GBC.fill = GridBagConstraints.HORIZONTAL;
                                    
                                              //Setting Font Type and Size
                                              Font font = new Font("Arial", Font.BOLD, 11);
                                    
                                              CustIDLbl = new JLabel("Customer ID");
                                              CustIDLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              CustIDLbl.setFont(font);
                                              CustIDLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 1;
                                              innertoppanel.add(CustIDLbl, GBC);     
                                    
                                              CustIDTxt = new JTextField(20);
                                              CustIDTxt.setEditable(true);
                                              GBC.gridx = 2;
                                              GBC.gridy = 1;
                                              innertoppanel.add(CustIDTxt, GBC);
                                    
                                              GBC.gridx = 3;
                                              GBC.gridy = 1;
                                              innertoppanel.add(Box.createHorizontalStrut(220), GBC);
                                    
                                              OrderedDateLbl = new JLabel("Order Date");
                                              OrderedDateLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              OrderedDateLbl.setFont(font);
                                              OrderedDateLbl.setForeground(LblfontColor);
                                              GBC.gridx = 4;
                                              GBC.gridy = 1;
                                              innertoppanel.add(OrderedDateLbl, GBC);     
                                    
                                              //Get today's date
                                              Date todaydate = new Date();
                                    
                                              OrderedDateTxt = new JTextField(simpleDF.format(todaydate), 20);
                                              OrderedDateTxt.setHorizontalAlignment(JTextField.CENTER);
                                              OrderedDateTxt.setEditable(false);
                                              GBC.gridx = 5;
                                              GBC.gridy = 1;
                                              innertoppanel.add(OrderedDateTxt, GBC);
                                    
                                              CustOrderIDLbl = new JLabel("Customer Order ID");
                                              CustOrderIDLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              CustOrderIDLbl.setFont(font);
                                              CustOrderIDLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 2;
                                              innertoppanel.add(CustOrderIDLbl, GBC);
                                    
                                              CustOrderIDTxt = new JTextField(20);
                                              //CustOrderIDTxt.setEditable(false);
                                              GBC.gridx = 2;
                                              GBC.gridy = 2;
                                              innertoppanel.add(CustOrderIDTxt, GBC);
                                    
                                              return innertoppanel;
                                         }
                                    
                                         public JPanel createInnerCenterPanel(){
                                    
                                              innercenterpanel = new JPanel(new GridBagLayout());
                                              innercenterpanel.setBackground(TxtfontColor);
                                              innercenterpanel.setBorder(BorderFactory.createLoweredBevelBorder());
                                              GridBagConstraints GBC = new GridBagConstraints();
                                              GBC.fill = GridBagConstraints.HORIZONTAL;
                                    
                                              //Setting Font Type and Size
                                              Font font = new Font("Arial", Font.BOLD, 11);
                                    
                                              ItemNumberLbl = new JLabel("Item Number");
                                              ItemNumberLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              ItemNumberLbl.setFont(font);
                                              ItemNumberLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 1;
                                              innercenterpanel.add(ItemNumberLbl, GBC);     
                                    
                                              ItemNumberTxt = new JTextField(20);
                                              GBC.gridx = 2;
                                              GBC.gridy = 1;
                                              innercenterpanel.add(ItemNumberTxt, GBC);
                                    
                                              UnitPriceLbl = new JLabel("Unit Price");
                                              UnitPriceLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              UnitPriceLbl.setFont(font);
                                              UnitPriceLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 2;
                                              innercenterpanel.add(UnitPriceLbl, GBC);     
                                    
                                              UnitPriceTxt = new JTextField(20);
                                              //UnitPriceTxt.setEditable(false);
                                              GBC.gridx = 2;
                                              GBC.gridy = 2;
                                              innercenterpanel.add(UnitPriceTxt, GBC);
                                    
                                              QuantityReqLbl = new JLabel("Quantity Required");
                                              QuantityReqLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              QuantityReqLbl.setFont(font);
                                              QuantityReqLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 3;
                                              innercenterpanel.add(QuantityReqLbl, GBC);     
                                    
                                              QuantityReqTxt = new JTextField(20);
                                              //QuantityReqTxt.setEditable(false);
                                              GBC.gridx = 2;
                                              GBC.gridy = 3;
                                              innercenterpanel.add(QuantityReqTxt, GBC);
                                    
                                              TotPriceLbl = new JLabel("Total Price");
                                              TotPriceLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              TotPriceLbl.setFont(font);
                                              TotPriceLbl.setForeground(LblfontColor);
                                              GBC.gridx = 1;
                                              GBC.gridy = 4;
                                              innercenterpanel.add(TotPriceLbl, GBC);     
                                    
                                              TotPriceTxt = new JTextField(20);
                                              TotPriceTxt.setEditable(false);
                                              TotPriceTxt.addFocusListener(new FocusAdapter(){
                                    
                                                   public void focusGained(FocusEvent evt){
                                    
                                                        TotPriceTxt.setText(""+Integer.parseInt(UnitPriceTxt.getText())*Integer.parseInt(QuantityReqTxt.getText()));
                                    
                                                   }
                                    
                                    
                                              });
                                              GBC.gridx = 2;
                                              GBC.gridy = 4;
                                              innercenterpanel.add(TotPriceTxt, GBC);
                                    
                                              return innercenterpanel;
                                         }
                                    
                                         public JPanel createInnerBottomPanel(){
                                    
                                              innerbottompanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
                                              innerbottompanel.setBackground(TxtfontColor);
                                    
                                              //Setting Font Type and Size
                                              Font font = new Font("Arial", Font.BOLD, 11);
                                    
                                              GrandTotLbl = new JLabel("Grand Total");
                                              GrandTotLbl.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
                                              GrandTotLbl.setFont(font);
                                              GrandTotLbl.setForeground(LblfontColor);
                                              innerbottompanel.add(GrandTotLbl);
                                    
                                              innerbottompanel.add(Box.createHorizontalStrut(30));
                                    
                                              GrandTotTxt = new JTextField(20);
                                              innerbottompanel.add(GrandTotTxt);
                                    
                                              return innerbottompanel;
                                    
                                         }
                                    
                                         public JPanel createInnerRightPanel(){
                                    
                                              innerrightpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                                              innerrightpanel.setBackground(TxtfontColor);
                                    
                                              innerrightpanel.setLayout(new BoxLayout(navrightpanel, BoxLayout.Y_AXIS));
                                              innerrightpanel.setBorder(BorderFactory.createLoweredBevelBorder());
                                    
                                              innerrightpanel.setLayout(new GridBagLayout());          
                                              GridBagConstraints GBC = new GridBagConstraints();
                                              GBC.fill = GridBagConstraints.HORIZONTAL;
                                    
                                              internalnextbtn = new JButton(createNextButtonIcon());
                                              GBC.gridx = 1;
                                              GBC.gridy = 1;
                                              internalnextbtn.addActionListener(new ActionListener(){
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        getInnerFieldText(currentItem);
                                    
                                                        internalprevbtn.setEnabled(true);
                                    
                                                        if(currentItem < MaxItems){
                                    
                                                             ++currentItem;
                                                             orderDetails[CurrentRecord].getInnerCustOrdObj();
                                                             setInnerFieldText(currentItem);
                                                             System.out.println("Current Item" + currentItem);
                                    
                                                        }
                                    
                                                        if(currentItem == MaxItems){
                                    
                                                             internalnextbtn.setEnabled(false);
                                    
                                                        }
                                    
                                                   }
                                    
                                              });
                                    
                                              innerrightpanel.add(internalnextbtn, GBC);
                                    
                                              return innerrightpanel;
                                         }
                                    
                                         public JPanel createInnerLeftPanel(){
                                    
                                              innerleftpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
                                              innerleftpanel.setBackground(TxtfontColor);
                                    
                                              innerleftpanel.setBorder(BorderFactory.createLoweredBevelBorder());
                                              innerleftpanel.setForeground(Color.BLACK);
                                    
                                              innerleftpanel.setLayout(new GridBagLayout());          
                                              GridBagConstraints GBC = new GridBagConstraints();
                                              GBC.fill = GridBagConstraints.HORIZONTAL;
                                    
                                              internalprevbtn = new JButton(createPreviousButtonIcon());
                                              GBC.gridx = 1;
                                              GBC.gridy = 1;
                                              internalprevbtn.addActionListener(new ActionListener(){
                                    
                                                   public void  actionPerformed(ActionEvent evt){
                                    
                                                        getInnerFieldText(currentItem);
                                    
                                                        internalnextbtn.setEnabled(true);
                                    
                                                        if(currentItem == 1){
                                    
                                                             internalprevbtn.setEnabled(false);
                                    
                                                        }
                                    
                                                        if(currentItem > 0){
                                    
                                                             --currentItem;
                                                             orderDetails[CurrentRecord].getInnerCustOrdObj();
                                                             setInnerFieldText(currentItem);
                                                             System.out.println("Current Item" + currentItem);
                                    
                                                        }
                                    
                                                   }
                                    
                                              });
                                    
                                              innerleftpanel.add(internalprevbtn, GBC);
                                    
                                    
                                              return innerleftpanel;
                                         }
                                    
                                         public JPanel createCustOrdDetailsButtons(){
                                    
                                              createbuttonpanel();
                                              buttonpanel.setBackground(TxtfontColor);
                                    
                                              externalprevbtn = new JButton(createPreviousButtonIcon());
                                              externalprevbtn.addActionListener(new ActionListener(){
                                    
                                                   public void  actionPerformed(ActionEvent evt){
                                    
                                                        getFieldText(CurrentRecord);
                                    
                                                        externalnextbtn.setEnabled(true);
                                    
                                                        if(CurrentRecord == 1){
                                    
                                                             externalprevbtn.setEnabled(false);
                                    
                                                        }
                                    
                                                        if(CurrentRecord > 0){
                                    
                                                             --CurrentRecord;
                                                             setFieldText(CurrentRecord);
                                                             System.out.println("Current Record " + CurrentRecord);//Checking RECORD_NUM
                                    
                                    
                                                        }
                                    
                                                   }
                                    
                                              });
                                              buttonpanel.add(externalprevbtn);
                                    
                                              addrecordbtn = new JButton("Add Record", createAddButtonIcon());
                                              addrecordbtn.addActionListener(new ActionListener(){
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        try{
                                    
                                                             MaxRecord = readRecordNumber();
                                                             MaxRecord++;
                                                             CurrentRecord = MaxRecord;
                                                             orderDetails[CurrentRecord] = new CustOrdObject();
                                                             writeRecordNumber(MaxRecord);
                                                             
                                                             MaxItems = 1;
                                                             innerCustOrdObj[MaxItems] = new InnerCustOrdObject();
                                    
                                                             //--<BEGINNING>--Clear Fields-------------------------------------------------------//
                                    
                                                             CustIDTxt.setText("");
                                                             CustOrderIDTxt.setText(""+MaxRecord);
                                    
                                                             //Get today's date
                                                             Date todaydate = new Date();
                                    
                                                             OrderedDateTxt.setText(""+simpleDF.format(todaydate));
                                                             ItemNumberTxt.setText("");
                                                             UnitPriceTxt.setText("");
                                                             QuantityReqTxt.setText("");
                                                             TotPriceTxt.setText("");
                                                             GrandTotTxt.setText("");
                                    
                                                             //--<END>--Clear Fields-------------------------------------------------------------//
                                    
                                                             externalnextbtn.setEnabled(false);
                                                             externalprevbtn.setEnabled(true);
                                                             System.out.println(MaxRecord);
                                    
                                    
                                                        } catch(Exception ec){ec.printStackTrace();}
                                                   }
                                    
                                              });
                                              buttonpanel.add(addrecordbtn);
                                    
                                              savebtn = new JButton("Save Data", createSaveButtonIcon());
                                              savebtn.addActionListener(new ActionListener(){
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        setFieldText(CurrentRecord);
                                                        setInnerFieldText(MaxItems);
                                                        writeOrder();
                                                        writeRecordNumber(MaxRecord);
                                                        System.out.println(CurrentRecord);
                                                        System.out.println(MaxRecord);
                                    
                                                   }
                                    
                                              });
                                              buttonpanel.add(savebtn);
                                    
                                              java.net.URL imageURL_AddRowIcon = CommonFeatures.class.getResource("Icons/edit_add.png");
                                              ImageIcon AddRowIcon = new ImageIcon(imageURL_AddRowIcon);
                                              additemreqbtn = new JButton("Add Item", AddRowIcon);
                                              additemreqbtn.addActionListener(new ActionListener(){
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        try{
                                    
                                                             //--<BEGINNING>--Clear Fields-------------------------------------------------------//
                                    
                                                             ItemNumberTxt.setText("");
                                                             UnitPriceTxt.setText("");
                                                             QuantityReqTxt.setText("");
                                                             TotPriceTxt.setText("");
                                    
                                                             //--<END>--Clear Fields-------------------------------------------------------------//
                                    
                                                             //CurrentRecord = MaxRecord;
                                    
                                                             MaxItems++;
                                                             innerCustOrdObj[MaxItems] = new InnerCustOrdObject();
                                                             System.out.println("Max Items "+MaxItems);
                                                             currentItem = MaxItems;
                                                             orderDetails[CurrentRecord].setMaxItems(MaxItems);
                                                             ///setInnerFieldText(currentItem);
                                    
                                                             internalnextbtn.setEnabled(false);
                                                             internalprevbtn.setEnabled(true);
                                    
                                    
                                                        } catch(Exception ec){ec.printStackTrace();}
                                                   }
                                    
                                              });
                                              buttonpanel.add(additemreqbtn);
                                    
                                              externalnextbtn = new JButton(createNextButtonIcon());
                                              externalnextbtn.addActionListener(new ActionListener(){
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        getFieldText(CurrentRecord);
                                    
                                                        externalprevbtn.setEnabled(true);
                                    
                                                        if(CurrentRecord < MaxRecord){
                                    
                                                             ++CurrentRecord;
                                                             setFieldText(CurrentRecord);
                                                             System.out.println(CurrentRecord);//Checking RECORD_NUM
                                    
                                                        }
                                    
                                                        if(CurrentRecord == MaxRecord){
                                    
                                                             externalnextbtn.setEnabled(false);
                                    
                                                        }
                                    
                                                   }
                                    
                                              });
                                              buttonpanel.add(externalnextbtn);
                                    
                                    
                                    
                                              gotorecordbtn = new JButton("Go To Record", createGotoButtonIcon());
                                              gotorecordbtn.addActionListener(new ActionListener() {
                                    
                                                   public void actionPerformed(ActionEvent evt){
                                    
                                                        /*
                                                         * The text from the GotorecordTxt textfield will be taken and assigned
                                                         * to a temporary integer variable called Find.  
                                                         */
                                                        int Find = Integer.parseInt(CustOrderIDTxt.getText());
                                    
                                                        for(int j=1; j <= MaxRecord; j++){
                                    
                                                             /*
                                                              * Using a for loop, each record can be read using the readCustRecord
                                                              * method. 
                                                              */
                                    
                                                             getFieldText(j);
                                    
                                                             /*
                                                              * An if condition is utilized to check whether the temporary stored variable, Find,
                                                              * matches a field in a record. If this record is found, then using the RecordExists
                                                              * which was declared at the top, either a true or false statement can be assigned
                                                              * 
                                                              * If the record exists, then a true statement will be assigned, if not a false 
                                                              * statement will be assigned.
                                                              */
                                    
                                                             if(orderDetails[j].getCustOrderID() == Find){
                                    
                                                                  RecordExists = true;
                                                                  break;
                                    
                                                             }else{
                                    
                                                                  RecordExists = false;
                                    
                                                             }
                                    
                                                        }
                                                        if(RecordExists == false){
                                    
                                                             /*
                                                              * If the RecordExists is assigned a false statement, then a message will be 
                                                              * displayed to show that the record does not exist.
                                                              */
                                    
                                                             JOptionPane.showMessageDialog(null, "Record Does Not Exist!", "Error Message", JOptionPane.ERROR_MESSAGE, createErrorIcon());
                                    
                                                        }else{
                                                             
                                                             
                                                             getFieldText(Find);
                                                             
                                                             
                                                        }
                                    
                                                   }
                                    
                                              });
                                              buttonpanel.add(gotorecordbtn);
                                    
                                              return buttonpanel;
                                    
                                         }
                                    
                                         //TODO
                                    
                                         public void setFieldText(int orderID){//TODO
                                    
                                              orderDetails[orderID].setCustID(Integer.parseInt(CustIDTxt.getText()));
                                              orderDetails[orderID].setCustOrderID(Integer.parseInt(CustOrderIDTxt.getText()));
                                              orderDetails[orderID].setOrderDate(OrderedDateTxt.getText());
                                              orderDetails[orderID].setInnerCustOrdObj(innerCustOrdObj);
                                    
                                              orderDetails[orderID].setMaxItems(MaxItems);
                                    
                                              setInnerFieldText(currentItem);
                                    
                                              orderDetails[orderID].setGrandTotal(Float.parseFloat(GrandTotTxt.getText()));
                                    
                                         }
                                    
                                         public void setInnerFieldText(int currentItem){//TODO
                                    
                                              innerCustOrdObj[currentItem] = new InnerCustOrdObject();
                                    
                                              innerCustOrdObj[currentItem].setMaxItems(MaxItems);
                                    
                                              innerCustOrdObj[currentItem].setItemNumber(Integer.parseInt(ItemNumberTxt.getText()));
                                              innerCustOrdObj[currentItem].setUnitPrice(Float.parseFloat(UnitPriceTxt.getText()));
                                              innerCustOrdObj[currentItem].setQuantityRequired(Integer.parseInt(QuantityReqTxt.getText()));
                                              innerCustOrdObj[currentItem].setTotalPrice(Float.parseFloat(TotPriceTxt.getText()));
                                    
                                         }
                                    
                                         public void getFieldText(int orderID){
                                    
                                              CustIDTxt.setText(Integer.toString(orderDetails[orderID].getCustID()));
                                              CustOrderIDTxt.setText(Integer.toString(orderDetails[orderID].getCustOrderID()));
                                              OrderedDateTxt.setText(""+orderDetails[orderID].getOrderDate());          
                                    
                                              currentItem = orderDetails[orderID].getMaxItems();
                                              orderDetails[orderID].getInnerCustOrdObj();
                                              System.err.println("currentItem" + currentItem);
                                              //getInnerFieldText(currentItem);
                                    
                                              GrandTotTxt.setText(Float.toString(orderDetails[orderID].getGrandTotal()));
                                    
                                    
                                         }
                                    
                                         public void getInnerFieldText(int currentItem){
                                    
                                              ItemNumberTxt.setText(Integer.toString(innerCustOrdObj[currentItem].getItemNumber()));
                                              UnitPriceTxt.setText(Float.toString(innerCustOrdObj[currentItem].getUnitPrice()));
                                              QuantityReqTxt.setText(Integer.toString(innerCustOrdObj[currentItem].getQuantityRequired()));
                                              TotPriceTxt.setText(Float.toString(innerCustOrdObj[currentItem].getTotalPrice()));
                                    
                                         }
                                    
                                         public void writeOrder(){//TODO
                                    
                                    
                                    
                                              try {
                                    
                                                   objOut = new ObjectOutputStream(new FileOutputStream(OrderDetailsFile));
                                                   objOut.writeObject(orderDetails);
                                                   System.out.println("WORKING!");
                                                   objOut.flush();
                                                   objOut.close();
                                    
                                              } catch (IOException e) {
                                    
                                                   e.printStackTrace();
                                    
                                              }
                                    
                                         }
                                    
                                         public Object readOrder(){
                                    
                                              Object temporaryObj;
                                    
                                              try{
                                    
                                                   objIn = new ObjectInputStream(new FileInputStream(OrderDetailsFile));
                                                   temporaryObj = objIn.readObject();               
                                                   CustOrdObject[] blah = (CustOrdObject[]) temporaryObj;
                                                   /*               System.out.println("Outer: "+blah[1].getCustID());
                                                   InnerCustOrdObject[] whee = blah[1].getInnerCustOrdObj();
                                                   System.out.println("Inner: "+whee[1].getItemNumber());*/
                                                   objIn.close();
                                    
                                                   System.out.println("Read Worky!");
                                    
                                                   return temporaryObj;
                                    
                                              }catch(Exception e){
                                    
                                                   e.printStackTrace(); 
                                    
                                                   System.out.println("Read No Worky!");
                                    
                                                   return null;
                                    
                                    
                                              }
                                    
                                         }
                                    
                                    
                                         public void writeRecordNumber(int MaxRecord){
                                    
                                              try{
                                    
                                                   objOut = new ObjectOutputStream(new FileOutputStream(OrdRecordNumStore));
                                                   objOut.writeObject(MaxRecord);
                                    
                                                   System.out.println("WORKING!");
                                    
                                                   objOut.flush();
                                                   objOut.close();
                                    
                                              }catch(Exception e){e.printStackTrace();}
                                    
                                         }
                                    
                                         public int readRecordNumber() {
                                              try {
                                    
                                                   objIn = new ObjectInputStream(new FileInputStream(OrdRecordNumStore));
                                    
                                                   int temporaryObj = Integer.parseInt(objIn.readObject().toString());
                                                   objIn.close();
                                    
                                                   System.out.println("Read Number Worky!");
                                    
                                                   return temporaryObj;
                                    
                                              } catch (Exception e) {
                                    
                                                   e.printStackTrace();
                                    
                                                   System.out.println("Read Number No Worky!");
                                    
                                                   return -1;
                                              }
                                    
                                         }
                                    
                                    }
                                    Message was edited by:
                                    Kilik07
                                    1 2 Previous Next