Results 1 to 6 of 6
  1. #1
    jammededed is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default Passing an object argument to a method, but I am unable to call methods on the arg

    Hi! I'm new to Java (about a year's experience). I'm making a stock control system for a school project, but I've encountered a problem.

    I am trying to pass an object of type Product p to my editProduct method, however trying to call p.getName(); doesn't work and throws a NullPointerException. The same kind of thing works for my displayRecord method (in a different class) and I can call .getName() on Product p, also passed as an argument to that method. Below is my editProduct class. The NullPointerExcepion is being thrown at line 61 (i.e., nameField.setText(p.getName());).

    I don't know if I explained right, so here's a line thing of how the classes relate:
    Search >>>(field.getText())>>> displayRecord(Product p) >>>editProduct(p)>>> EditProduct

    And as a side note: adding the line p = new Product(); fixes it and successfully runs the class (including the Save and Quit parts) but obviously I want it to specifically refer to the Product I pass to the method. I'm new to Java and programming in general so any help offered is very very much appreciated.

    Java Code:
        import java.awt.BorderLayout;
        import java.awt.Dimension;
        import java.awt.GridLayout;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import java.awt.event.FocusEvent;
        import java.awt.event.FocusListener;
        import java.util.ArrayList;
        import javax.swing.BorderFactory;
        import javax.swing.Box;
        import javax.swing.BoxLayout;
        import javax.swing.JButton;
        import javax.swing.JFrame;
        import javax.swing.JLabel;
        import javax.swing.JOptionPane;
        import javax.swing.JPanel;
        import javax.swing.JTextField;
        
        public class EditProduct extends JFrame implements FocusListener, ActionListener {
        
            final private StockList stocks;
            final private ArrayList<Product> list;
            JPanel top = new JPanel();
            JPanel bot = new JPanel();
            JLabel nameLabel = new JLabel();
            JLabel costLabel = new JLabel();
            JLabel quantityLabel = new JLabel();
            JTextField nameField = new JTextField();
            JTextField costField = new JTextField();
            JTextField quantityField = new JTextField();
            JButton save = new JButton();
            JButton backToMenu = new JButton();
            private GridLayout topLayout;
        
            public EditProduct() {
                stocks = new StockList();
                list = stocks.getList();
            }
        
            public void editProduct(Product p) {
                this.setTitle("Editing a Product");
                this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                this.setPreferredSize(new Dimension(500, 250));
        
                topLayout = new GridLayout(3, 2, 5, 5);
                top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));
                top.setLayout(topLayout);
        
                bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
                bot.add(Box.createHorizontalGlue());
                bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));
        
                nameLabel.setText("Name:  ");
                costLabel.setText("Cost:  ");
                quantityLabel.setText("Quantity:  ");
                top.add(nameLabel);
                top.add(costLabel);
                top.add(quantityLabel);
        
                nameField.setText(p.getName());
                costField.setText(String.valueOf(p.getCost()));
                quantityField.setText(String.valueOf(p.getQuantity()));
                top.add(nameField);
                top.add(costField);
                top.add(quantityField);
        
                nameField.addFocusListener(this);
                costField.addFocusListener(this);
                quantityField.addFocusListener(this);
        
                save.setText("Save");
                save.addActionListener(this);
                backToMenu.setText("Quit");
                backToMenu.addActionListener(this);
        
                bot.add(save);
                bot.add(Box.createRigidArea(new Dimension(10, 0)));
                bot.add(backToMenu);
        
                this.add(top);
                this.add(bot, BorderLayout.SOUTH);
                this.pack();
                this.setLocationRelativeTo(null);
                this.setVisible(true);
            }
        
            @Override
            public void focusGained(FocusEvent e) {
                if (e.getSource() == nameField) {
                    nameField.setText("");
                } else if (e.getSource() == costField) {
                    costField.setText("");
                } else if (e.getSource() == quantityField) {
                    quantityField.setText("");
                }
            }
        
            @Override
            public void focusLost(FocusEvent fe) {
                //do nothing
            }
        
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == save) {
                    String newName = nameField.getText();
                    double newCost = Double.parseDouble(costField.getText());
                    int newQty = Integer.parseInt(quantityField.getText());
                    stocks.editProduct(newName, newCost, newQty);
                    this.dispose();
                    JOptionPane.showMessageDialog(null, "Changes have been saved!", "Saved!", JOptionPane.PLAIN_MESSAGE);
                } else if (e.getSource() == backToMenu) {
                    new CreateDisplay();
                    this.dispose();
                }
            }
        }
    I'm asking a question because I don't understand how Product p could possibly be null, because the argument is passed through my DisplayRecord class, which also takes a Product p argument and works. In that class, I have declared Product prod = p; and prod is what I am passing to editProduct.

    I'm sorry if this is a silly question or if I'm asking it wrongly. I don't really visit forums much ;u; I appreciate literally any help offered to me. Thank you in advance!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Passing an object argument to a method, but I am unable to call methods on the ar

    The object p may be null. Do you ever initialize it? From where to you call editProduct?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jammededed is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default Re: Passing an object argument to a method, but I am unable to call methods on the ar

    Ah! A respondent! I'm so relieved. This is such a stressful problem because I suck at programming.

    I call editProduct from another class DisplayRecord:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.IOException;
    import java.util.ArrayList;
    
    public class DisplayRecord extends JFrame implements ActionListener {
    
        final private StockList stocks;
        final private ArrayList<Product> list;
        JFrame showWindow;
        private JPanel top, bot;
    
        private JPanel barcodePanel1 = new JPanel();
        private JPanel barcodePanel2 = new JPanel();
        private JPanel namePanel1 = new JPanel();
        private JPanel namePanel2 = new JPanel();
        private JPanel descPanel1 = new JPanel();
        private JPanel descPanel2 = new JPanel();
        private JPanel compPanel1 = new JPanel();
        private JPanel compPanel2 = new JPanel();
        private JPanel ratingPanel1 = new JPanel();
        private JPanel ratingPanel2 = new JPanel();
        private JPanel costPanel1 = new JPanel();
        private JPanel costPanel2 = new JPanel();
        private JPanel quantityPanel1 = new JPanel();
        private JPanel quantityPanel2 = new JPanel();
        private JLabel barcodeLabel = new JLabel();
        private JLabel nameLabel = new JLabel();
        private JLabel descLabel = new JLabel();
        private JLabel compLabel = new JLabel();
        private JLabel ratingLabel = new JLabel();
        private JLabel costLabel = new JLabel();
        private JLabel quantityLabel = new JLabel();
        private GridLayout displayLayout;
        JButton edit = new JButton("Edit");
        JButton backToMenu = new JButton("Back to Menu");
        JButton delete = new JButton("Delete");
        private Product prod;
    
        public DisplayRecord() {
            stocks = new StockList();
            list = stocks.getList();
            try {
                stocks.load();
            } catch (IOException ex) {
                System.out.println("Cannot load file");
            }
        }
    
        public void displayRecord(Product p) {
            this.setTitle("Displaying one record");
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.setPreferredSize(new Dimension(500, 350));
            final Product prod = p;
    
            top = new JPanel();
            displayLayout = new GridLayout(7, 2, 2, 2);
            top.setLayout(displayLayout);
            top.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 5));
    
            bot = new JPanel();
            bot.setLayout(new BoxLayout(bot, BoxLayout.LINE_AXIS));
            bot.add(Box.createHorizontalGlue());
            bot.setBorder(BorderFactory.createEmptyBorder(20, 5, 5, 5));
            
            barcodeLabel.setText("Barcode:  ");
            nameLabel.setText("Name:  ");
            descLabel.setText("Description:  ");
            compLabel.setText("Developer:  ");
            ratingLabel.setText("EU Rating:  ");
            costLabel.setText("Cost:  ");
            quantityLabel.setText("Quantity in Stock:  ");
            
            JLabel barcodeField = new JLabel(Long.toString(p.getBarcode()));
            JLabel nameField = new JLabel(p.getName());
            JLabel descField = new JLabel(p.getDesc());
            JLabel compField = new JLabel(p.getCompany());
            JLabel ratingField = new JLabel(p.getRating());
            JLabel costField = new JLabel(Double.toString(p.getCost()));
            JLabel quantityField = new JLabel(Integer.toString(p.getQuantity()));
    
            barcodePanel1.add(barcodeLabel);
            barcodePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            barcodePanel2.add(barcodeField);
            barcodePanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            namePanel1.add(nameLabel);
            namePanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            namePanel2.add(nameField);
            namePanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            descPanel1.add(descLabel);
            descPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            descPanel2.add(descField);
            descPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            compPanel1.add(compLabel);
            compPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            compPanel2.add(compField);
            compPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            ratingPanel1.add(ratingLabel);
            ratingPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            ratingPanel2.add(ratingField);
            ratingPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            costPanel1.add(costLabel);
            costPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            costPanel2.add(costField);
            costPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
            quantityPanel1.add(quantityLabel);
            quantityPanel1.setBorder(BorderFactory.createLineBorder(Color.black));
            quantityPanel2.add(quantityField);
            quantityPanel2.setBorder(BorderFactory.createLineBorder(Color.black));
    
            top.add(barcodePanel1);
            top.add(barcodePanel2);
            top.add(namePanel1);
            top.add(namePanel2);
            top.add(descPanel1);
            top.add(descPanel2);
            top.add(compPanel1);
            top.add(compPanel2);
            top.add(ratingPanel1);
            top.add(ratingPanel2);
            top.add(costPanel1);
            top.add(costPanel2);
            top.add(quantityPanel1);
            top.add(quantityPanel2);
    
            edit.addActionListener(this);
            delete.addActionListener(this);
            backToMenu.addActionListener(this);
    
            bot.add(edit);
            bot.add(Box.createRigidArea(new Dimension(10, 0)));
            bot.add(delete);
            bot.add(Box.createRigidArea(new Dimension(10, 0)));
            bot.add(backToMenu);
    
            this.add(top);
            this.add(bot, BorderLayout.SOUTH);
            this.setLocationRelativeTo(null);
            this.pack();
            this.setVisible(true);
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == edit) {
                EditProduct ed = new EditProduct();
                ed.editProduct(prod);
                this.dispose();
            } else if (e.getSource() == delete) {
                DeleteRecord del = new DeleteRecord();
                del.deleteRecord(prod);
                this.dispose();
            } else if (e.getSource() == backToMenu) {
                new CreateDisplay();
                this.dispose();
            }
        }
    }
    The final Product prod = p; line was necessary so I could call editProduct using Product p. This class works perfectly. I tried adding the same line to my editProduct class but it didn't work and kept the exception. I'm not sure how else I need to intialise it. I need the object passed to displayRecord and subsequently to editProduct to be the same one. Thank you so much for your time!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Passing an object argument to a method, but I am unable to call methods on the ar

    You need to print the value of p at the beginning of each method it is passed as an argument. Somewhere along the line you are passing a value which is either null or references a null value incorrectly.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jammededed is offline Member
    Join Date
    Feb 2015
    Posts
    3
    Rep Power
    0

    Default Re: Passing an object argument to a method, but I am unable to call methods on the ar

    I did that and it works! Thank you so so much for your generous help. All I had to do was declare final Product prod; at the beginning of each class and initialise it to p at the beginning of each method (obviously, the methods that take p). My editProduct methods aren't saving correctly but I'm sure I can fix that! Thank you, again!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Passing an object argument to a method, but I am unable to call methods on the ar

    Quote Originally Posted by jammededed View Post
    All I had to do was declare final Product prod; at the beginning of each class and initialise it to p at the beginning of each method (obviously, the methods that take p).
    That wouldn't compile. A final variable can only be assigned once, and a final field must be initialized before the constructor returns (or before any constructor returns, in case of multiple constructors).

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 1
    Last Post: 10-23-2012, 03:47 PM
  2. Replies: 1
    Last Post: 11-29-2011, 01:30 PM
  3. Passing method as argument
    By susieferrari in forum New To Java
    Replies: 26
    Last Post: 06-13-2011, 10:43 AM
  4. passing hashmap from a method and call it within main
    By rajuchacha007 in forum New To Java
    Replies: 2
    Last Post: 04-06-2010, 09:41 AM
  5. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 12:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •