Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: using OOP in java

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using OOP in java

    Hi,

    I have 2 classes:
    1) productType that have name and price of the productType and an empty consturcor.
    2) superMarket that have name of supermarket and arraylist of productTypes (
    private ArrayList<ProductType> products;
    ) and an empty constructor.

    In each class I have function that get input from console and should store it into each class variables.

    in productType i have function:
    	public void getFromUser() throws IOException
    	{
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
    		System.out.println("Enter product name:");
    		name = br.readLine();
     
    		System.out.println("Enter price:");
    		price = Integer.parseInt(br.readLine());
    	}

    in supermarket i have function:
    		public void getFromUser() throws IOException
    		{
    			int n=0;
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			ProductType p = new ProductType();
     
    			System.out.println("Enter supermarket details: ");
    			System.out.println("Enter name: ");
    			this.name = br.readLine();
     
    			System.out.println("How many products do you have: ");
    			n = Integer.parseInt(br.readLine());
     
    			for(int i=0;i<n;i++)
    			{
    				products.add(i,p);
    				products.get(i).getFromUser();				
    			}
    		}

    the main is something like:
    	public static void main(String[] args) throws IOException{
     
    		SuperMarket s1 = new SuperMarket();
    		SuperMarket s2 = new SuperMarket();
     
    		s1.getFromUser();
    	}

    The problem is when i get to line "products.add(i,p)" I get java.lang.NullPointerException
    In the debug mode I can see that when I get to this line the "products" is null.

    why do you think this happening, when I do "new SuperMarker()" in the main it should run the empty constructor and create new arraylist...
    ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using OOP in java

    why do you think this happening,
    The products variable that is being used has not been assigned a value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using OOP in java

    why not? the function is in class Supermarket that have an empty constructor:
    public void SuperMarket()
    {
    this.name = null;
    this.products = new ArrayList<ProductType>();
    this.quentitys = new ArrayList<int[]>();
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using OOP in java

    Is the code in post#3 executed? It was not included in the original post so it was not found when a Search of the code for where the variable: products was used. You need to post all the relevant if you expect good answers.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using OOP in java

    ok, this is the full source's

    ProductType.class

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.FileReader;
    import java.io.IOException;
     
    public class ProductType {
     
    	private String name;
    	private double price;
     
    	// constructors
    	public void ProductType()
    	{
    		this.name = null;
    		this.price = 0;
    	}
     
    	public void ProductType(String name,double price)
    	{
    		this.name = name;
    		this.price = price;
    	}
     
    	// Getters and Setters
    	public String getName()
    	{
    		return name;
    	}
     
    	public void setName(String name)
    	{
    		this.name = name;
    	}
     
    	public double getPrice()
    	{
    		return price;
    	}
     
    	public void setPrice(double price)
    	{
    		this.price = price;
    	}
     
    	public void printProd()
    	{
    		System.out.println("Product name: " + name + " Price is: " + price);
    	}
     
    	public void getFromUser() throws IOException
    	{
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
    		System.out.println("Enter product name:");
    		name = br.readLine();
     
    		System.out.println("Enter price:");
    		price = Integer.parseInt(br.readLine());
    	}
    }

    SuperMarket.java

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
     
    public class SuperMarket {
     
    		private String name;
    		private ArrayList<ProductType> products;
    		private ArrayList<int[]> quentitys;
     
    		// constructors
    		public void SuperMarket()
    		{
    			this.name = null;
    			this.products = new ArrayList<ProductType>();
    			this.quentitys = new ArrayList<int[]>();
    		}
     
    		public void SuperMarket(String name, ArrayList<ProductType> products,ArrayList<int[]> quentitys)
    		{
    			this.name = name;
    			this.products = products;
    			this.quentitys = quentitys;
    		}
     
    		// Getters and Setters
    		public String getName()
    		{
    			return name;
    		}
     
    		public void setName(String name)
    		{
    			this.name = name;
    		}
     
    		public ArrayList<ProductType> getProducts()
    		{
    			return this.products;
    		}
     
    		public void setProducts(ArrayList<ProductType> products)
    		{
    			this.products = products;
    		}
     
    		public ArrayList<int[]> getQuentitys()
    		{
    			return this.quentitys;
    		}
     
    		public void setQuentitys(ArrayList<int[]> quentitys)
    		{
    			this.quentitys = quentitys;
    		}
     
    		public void printSuper()
    		{
    			System.out.println("Super Market name: " + name);
    			System.out.println("Products and quentity: ");
    			for(int i=0;i<products.size();i++)
    			{
    				System.out.println("Product " + i+1 + " : " + " Name: " + products.get(i).getName() + 
    						           " Price: " + products.get(i).getPrice() + " Quentity: " + quentitys.get(i));
    			}
    		}
     
    		public void printUnderPrice(double price)
    		{
    			for(int i=0;i<products.size();i++)
    			{
    				if(products.get(i).getPrice()<price)
    				{
    					System.out.println("Product " + i+1 + " : " + " Name: " + products.get(i).getName() + 
    					           " Price: " + products.get(i).getPrice() + " Quentity: " + quentitys.get(i));					
    				}
    			}
    		}
     
    		public void getFromUser() throws IOException
    		{
    			int n=0;
    			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    			ProductType p = new ProductType();
     
    			System.out.println("Enter supermarket details: ");
    			System.out.println("Enter name: ");
    			this.name = br.readLine();
     
    			System.out.println("How many products do you have: ");
    			n = Integer.parseInt(br.readLine());
     
    			this.products = new ArrayList<ProductType>();
     
    			for(int i=0;i<n;i++)
    			{
    				products.add(i,p);
    				products.get(i).getFromUser();				
    			}
    		}
    }

    SuperMarket_demo.java

    import java.io.IOException;
     
    public class SuperMarket_demo {
     
    	public static void main(String[] args) throws IOException{
     
    		SuperMarket s1 = new SuperMarket();
    		SuperMarket s2 = new SuperMarket();
     
    		s1.getFromUser();
     
    		s1.printSuper();
     
    	}
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using OOP in java

    Can you also post the full contents of the error message?

    Did you see this question?
    Is the code in post#3 executed?

    Add a println() statement to make sure.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using OOP in java

    from the console window:
    "
    Enter supermarket details:
    Enter name:
    my
    How many products do you have:
    2
    Exception in thread "main" java.lang.NullPointerException
    at SuperMarket.getFromUser(SuperMarket.java:97)
    at SuperMarket_demo.main(SuperMarket_demo.java:10)
    "
    I think the empty(deafult) constructor of SuperMarker do not work and do not defint the products arrayList, but I dont understand why...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using OOP in java

    Did you see this question?
    Is the code in post#3 executed?

    Add a println() statement to make sure.

    Do you know how to write a constructor? See the tutorial:
    http://docs.oracle.com/javase/tutori...structors.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using OOP in java

    by post 3 do you mean - SuperMarket_demo.java? if is - so it do execute...

    about the constructor, this is the empty constructor

    public void SuperMarket()
    {
    this.name = null;
    this.products = new ArrayList<ProductType>();
    this.quentitys = new ArrayList<int[]>();
    }

    --- Update ---

    I found my problem!!! I wrote "void" by mistake in the constructor prototype so it dosen't get into it.....

    Thnk's Norm.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: using OOP in java

    it do execute
    But it was not being executed because it was not a constructor.
    It's so easy to hope that code is doing what you want it to do. Often adding a println() statement that shows what the code is actually doing(or not doing) helps you see the problems in code.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2013
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: using OOP in java

    you are right, thnk's for your help.

Similar Threads

  1. want to do a project with java on OOP need help
    By Sakib in forum Java Theory & Questions
    Replies: 1
    Last Post: March 21st, 2013, 12:06 PM
  2. java oop
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 06:50 AM
  3. OOP and java
    By ~Kyo~ in forum Object Oriented Programming
    Replies: 6
    Last Post: January 22nd, 2013, 04:24 AM
  4. Is Java 100% OOP
    By kbbaloch in forum Object Oriented Programming
    Replies: 5
    Last Post: December 29th, 2011, 12:54 AM
  5. [SOLVED] OOP java assignment
    By enkei in forum Object Oriented Programming
    Replies: 1
    Last Post: April 27th, 2011, 11:16 AM