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 10 of 10

Thread: why am I getting NullPointerException.

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default why am I getting NullPointerException.

    guys, why am I getting NullPointerException when initializing the array? I ran debug mode and productName is null

    public class Main {
     
    	public static void main(String[] args) {
     
    		Product[] myProducts = new Product[3];
     
    		Supplier ikea = new Supplier();	
    		ikea.supplierName = "ikea";
    		myProducts[0].productName = "chair";
    		myProducts[0].supplierName = ikea;
     
    		myProducts[1].productName = "table";
    		myProducts[1].supplierName = ikea;
     
    		Supplier javaBlackBelt = new Supplier();
    		javaBlackBelt.supplierName = "javaBlackBelt";
    		myProducts[2].productName = "OO Exam";
    		myProducts[2].supplierName = javaBlackBelt;
     
    		for(Product p: myProducts) {
    			System.out.println(p.productName+" is from "+p.supplierName);
    		}		
     
    	}
     
    }

    public class Product {
     
    	String productName;
    	Supplier supplierName;	
     
    }

    public class Supplier {
     
    	String supplierName;		
     
    }
    Last edited by mia_tech; October 24th, 2012 at 01:50 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: why am I getting NullPointerException.

    You're initializing an array which can hold Product objects, it doesn't actually create any Product objects. You need to manually create them.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: why am I getting NullPointerException.

    myProducts[0].supplierName = ikea;
    That code won't compile because ikea is not declared anywhere.

  4. #4
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: why am I getting NullPointerException.

    ok I fixed the code, I think it should look like this

    /** Code removed by moderator */
    Last edited by jps; October 24th, 2012 at 02:09 AM. Reason: spoonfeeding

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: why am I getting NullPointerException.

    @mia_tech please read the problem with spoonfeeding

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: why am I getting NullPointerException.

    ok, now I know what spoon-feeding is... I guess by removing my code is a sign that it was correct. Am asking b/c I would like to hear about a better implementation or at least a confirmation that it was correct

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: why am I getting NullPointerException.

    Quote Originally Posted by mia_tech View Post
    ok, now I know what spoon-feeding is... I guess by removing my code is a sign that it was correct. Am asking b/c I would like to hear about a better implementation or at least a confirmation that it was correct
    I owe you an apology. I didn't realize you were the OP posting your own code. I am so sorry. I thought someone else fixed the code. Please post it again and forgive my mistake.

  8. #8
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: why am I getting NullPointerException.

    Quote Originally Posted by jps View Post
    I owe you an apology. I didn't realize you were the OP posting your own code. I am so sorry. I thought someone else fixed the code. Please post it again and forgive my mistake.
    that was kind of strange, since I get no feed back weather my solution is correct or not. Anyways, here it goes again. If someone could take a look at it and let me know if is correct

    public class Main {
     
    	public static void main(String[] args) {
     
    		Product[] myProducts = new Product[3];		
     
    		Supplier ikea = new Supplier();	
    		ikea.supplierName = "ikea";
    		Product chair = new Product();
    		chair.productName = "chair";
    		chair.supplier = ikea;
    		myProducts[0] = chair;
     
    		Product table = new Product();
    		table.productName = "table";
    		table.supplier = ikea;
    		myProducts[1] = table;
     
    		Supplier javaBlackBelt = new Supplier();
    		javaBlackBelt.supplierName = "javaBlackBelt";
    		Product ooProgramming = new Product();
    		ooProgramming.productName = "javaBlackBelt";
    		ooProgramming.supplier = javaBlackBelt;
    		myProducts[2] = ooProgramming;
     
    		for(Product p: myProducts) {
    			System.out.println(p.productName+" is from "+p.supplier.supplierName);
    		}		
     
    	}
     
    }

    public class Product {
     
    	String productName;
    	Supplier supplier;	
     
    }

    public class Supplier {
     
    	String supplierName;	
     
    }

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: why am I getting NullPointerException.

    that was kind of strange, since I get no feed back weather my solution is correct or not.
    Yes, removing the code was my mistake. It was late and I thought there was one name on the OP and ya, it shouldn't have been removed. I read over a hundred (and answer most) of questions a day on multiple forums, sometimes mistakes happen I guess.




    If someone could take a look at it and let me know if is correct
    Does it compile? Does it run? Are there any error messages? Does it perform as expected? Writing programs is much more than getting code typed up. Troubleshooting and testing are part of the development process. The first thing to do after writing code is try to break it. Or to put it in better words, try to prove that the code is broken.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: why am I getting NullPointerException.

    Does it compile and run correctly? As far as I can tell, it looks correct, but I didn't try running it.

    I would suggest fully utilizing Object-Oriented design and define constructors to build complete objects. You should also declare your fields as private and use getters/setters to access the fields.

    Here's an example getter/setter:
    public class Example
    {
        private String var;
     
        public void setVar(String value)
        {
            var = value;
        }
     
        public String getVar()
        {
            return var;
        }
    }

Similar Threads

  1. NullPointerException
    By Alket in forum Member Introductions
    Replies: 1
    Last Post: June 7th, 2012, 08:09 AM
  2. NullPointerException
    By deathmatex in forum Exceptions
    Replies: 4
    Last Post: March 27th, 2012, 04:54 AM
  3. [SOLVED] NullPointerException
    By macko in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 21st, 2011, 12:35 PM
  4. [SOLVED] NullPointerException
    By javapenguin in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 1st, 2010, 01:10 AM
  5. NullPointerException
    By bbr201 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 29th, 2010, 08:06 PM