Results 1 to 8 of 8
  1. #1
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Angry Complete NullPointerException Confusion

    Hey everyone. Before I explain my problem, I'll explain what this class does.

    I have this class that works with a sequential-access file. It is storing the instance variables of a custom class I made called Products. Each product has three string values as instance variables. In the application, there are different options the user can chose. I am having problem with the add option. I assume everything is working fine, but I have one problem. There is an ArrayList called products that is supposed to contain all the product objects corresponding to the .txt file. When my addProduct(Product product) method tried to add to this collection it throws a NullPointerException. This must mean that my collection is somehow null when it reaches that part of the code. The getProducts() method is supposed to fill this collection. Could someone help me?

    Here's the code:
    Java Code:
    import java.util.*;
    import java.io.*;
    import java.nio.file.*;
    
    public final class ProductBinaryFile implements ProductDAO
    {
        private ArrayList<Product> products = null;
        private Path productsPath = null;
        private File productsFile = null;
    
        private final String FIELD_SEP = "\t";
    
        public ProductBinaryFile()
        {
            productsPath = Paths.get("products.bin");
            productsFile = productsPath.toFile();
            products = this.getProducts();
        }
    
        public ArrayList<Product> getProducts()
        {
            // if the products file has already been read, don't read it again
            if (products != null)
                return products;        
    
            products = new ArrayList<>();        
            
            if (Files.exists(productsPath))  // prevent the FileNotFoundException
            {
                try (DataInputStream in = new DataInputStream(
                         new FileInputStream(productsFile)))
                {
                    // read all products stored in the file
                    // into the array list
                String code = in.readUTF();
                String description = in.readUTF();
                String price = in.readUTF();
     
    
     
                products.add(new Product(
                    code, description, Double.parseDouble(price))); 
                }
                catch(IOException e)
                {
                    System.out.println(e);
                    return null;
                }
            }
            return products;            
        }
    
        public Product getProduct(String code)
        {
            for (Product p : products)
            {
                if (p.getCode().equals(code))
                    return p;
            }
            return null;
        }
    
        public boolean addProduct(Product p)
        {
            products.add(p);
            return this.saveProducts();
        }
    
        public boolean deleteProduct(Product p)
        {
            products.remove(p);
            return this.saveProducts();
        }
    
        public boolean updateProduct(Product newProduct)
        {
            // get the old product and remove it
            Product oldProduct = this.getProduct(newProduct.getCode());
            int i = products.indexOf(oldProduct);
            products.remove(i);
    
            // add the updated product
            products.add(i, newProduct);
    
            return this.saveProducts();
        }
    
        private boolean saveProducts()
        {        
    	    try (DataOutputStream out = new DataOutputStream(
                                   new BufferedOutputStream(
                                   new FileOutputStream(productsFile))))
            {
    
                // write all products in the array list
                // to the file
                for (Product p : products)
                {
                    out.writeUTF(p.getCode() + FIELD_SEP);
                    out.writeUTF(p.getDescription() + FIELD_SEP);
                    out.writeUTF(Double.toString(p.getPrice()));
                }
            }
            catch(IOException e)
            {
                System.out.println(e);
                return false;
            }
    
            return true;
        }	
    }
    And here's the stack trace:
    Java Code:
    Enter a command: add
    
    Enter product code: java
    Enter product description: learn about java yippie
    Enter price: 19.99
    Exception in thread "main" java.lang.NullPointerException
    	at ProductBinaryFile.addProduct(ProductBinaryFile.java:63)
    	at ProductMaintApp.addProduct(ProductMaintApp.java:90)
    	at ProductMaintApp.main(ProductMaintApp.java:34)
    Java Result: 1

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Complete NullPointerException Confusion

    Exception in thread "main" java.lang.NullPointerException
    at ProductBinaryFile.addProduct(ProductBinaryFile.jav a:63)
    Look at line 63 and find the null value. Then backtrack in the code to see where is came from.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: Complete NullPointerException Confusion

    Quote Originally Posted by Norm View Post
    Look at line 63 and find the null value. Then backtrack in the code to see where is came from.
    I tried doing that. Where it came from should be working perfectly. I can't really fin the null value; I don't think it's very obvious. Should I post the rest of my code?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Complete NullPointerException Confusion

    What variable has a null value on line 63? Use a println() to print out the all variable's used on line 63 values to see.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: Complete NullPointerException Confusion

    Quote Originally Posted by Norm View Post
    What variable has a null value on line 63? Use a println() to print out the all variable's used on line 63 values to see.
    The ArrayList<Products> products variable is null.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Complete NullPointerException Confusion

    Ok, now find out why. I debug by using lots of println() statements. Add a println() statement that prints out the value of products after every statement that assigns a value to products to see where it is getting a null value.

    Also add a call to the printStackTrace() method in all the catch blocks so you will see any errors that happen.
    Last edited by Norm; 07-13-2014 at 02:38 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    coolman50544 is offline Member
    Join Date
    Apr 2014
    Location
    Miami, FL
    Posts
    47
    Rep Power
    0

    Default Re: Complete NullPointerException Confusion

    Quote Originally Posted by Norm View Post
    Ok, now find out why. I debug by using lots of println() statements. Add a println() statement that prints out the value of products after every statement that assigns a value to products to see where it is getting a null value.

    Also add a call to the printStackTrace() method in all the catch blocks so you will see any errors that happen.
    Thanks for the advice. I successfully fixed my application, and learned why the variable was staying with a null value.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default Re: Complete NullPointerException Confusion

    Glad you found it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Need help, in confusion.
    By luppiex in forum New To Java
    Replies: 15
    Last Post: 11-13-2013, 07:23 AM
  2. [HELP] bit string confusion
    By aufis in forum New To Java
    Replies: 1
    Last Post: 03-07-2011, 04:42 PM
  3. Confusion here @@' Help!
    By pleasurelyours in forum New To Java
    Replies: 7
    Last Post: 06-09-2010, 03:42 PM
  4. Tic Tac Toe confusion
    By jigglywiggly in forum New To Java
    Replies: 15
    Last Post: 04-12-2009, 01:47 AM
  5. Confusion over arrays
    By dbashby in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 09:38 PM

Tags for this Thread

Posting Permissions

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