Results 1 to 8 of 8
  1. #1
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default Null Pointer exception

    for (BaseCell c : cells) {
    if (c != null && c.isAlive())
    c.eat();
    if (c.type == "AnimalCell"){
    BaseCell a;
    a = new PlantCell();
    int k=c.getLocation();
    a = BuscarLista(k);
    a.setFoodLevel(a.getFoodLevel() - 1);
    a.doStarvation();
    }

    }

    Hi, Sometimes when I run this code, gives me this error:

    Exception in thread "main" java.lang.NullPointerException
    at Sim.run(Sim.java:117)
    at Sim.main(Sim.java:175)

    It usually happens 1 in 5 times I run. I donīt know which is the problem.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by diegoyj View Post
    Java Code:
    for (BaseCell c : cells) {
    			 if (c != null && c.isAlive())
    				 c.eat();
    			 	if (c.type == "AnimalCell"){
    			 		BaseCell a;
    					a = new PlantCell();
    			 		int k=c.getLocation();
    			 		a = BuscarLista(k);
    			 		a.setFoodLevel(a.getFoodLevel() - 1);
    			 		a.doStarvation();
    			 		}
    			 		
    			 	}
    Hi, Sometimes when I run this code, gives me this error:

    Exception in thread "main" java.lang.NullPointerException
    at Sim.run(Sim.java:117)
    at Sim.main(Sim.java:175)

    It usually happens 1 in 5 times I run. I donīt know which is the problem.
    You indented the second "if ( ... )" statement as if it were part of the "if (c != null ...)" statement; it isn't because you forgot a few curly braces. You also can't compare Strings for equality with the == operator, use the .equals() method for that. Read the API documentation for the String class.

    kind regards,

    Jos

    ps. next time use [ code ] ... [ /code ] tags; I only saw your indentation when I wanted to reply.
    Last edited by JosAH; 01-29-2010 at 02:28 PM.

  3. #3
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok, To campare strings i use the .equals() method , and have the same error.
    The error is in the instruction 9.
    a.setFoodLevel(a.getFoodLevel() - 1);
    Last edited by diegoyj; 01-29-2010 at 03:21 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by diegoyj View Post
    Ok, To campare strings i use the .equals() method , and have the same error.
    Did you also fix your indentation?

    kind regards,

    Jos

  5. #5
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    I add two new brackets.
    for (BaseCell c : cells) {
    if (c != null && c.isAlive()){
    c.eat();
    if (c.type.equals("AnimalCell")){
    BaseCell a;
    a = new PlantCell();
    int k=c.getLocation();
    a = BuscarLista(k);
    a.setFoodLevel(a.getFoodLevel() - 1);
    a.doStarvation();
    }
    }
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by diegoyj View Post
    I add two new brackets.
    Java Code:
     for (BaseCell c : cells) {
    			 if (c != null && c.isAlive()){
    				 c.eat();
    			 	if (c.type.equals("AnimalCell")){
    			 		BaseCell a;
    					a = new PlantCell();
    			 		int k=c.getLocation();
    			 		a = BuscarLista(k);
    			 		a.setFoodLevel(a.getFoodLevel() - 1);
    			 		a.doStarvation();
    			 		}
    			 }
    		}
    Please use those [ code ] ... [ /code ] tags and be a bit more informative please; don't make us guess; what is the line where that NullPointerException was thrown?

    kind regards,

    Jos

  7. #7
    diegoyj is offline Member
    Join Date
    Jan 2010
    Posts
    7
    Rep Power
    0

    Default

    The error is in the instruction 9.
    a.setFoodLevel(a.getFoodLevel() - 1);

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by diegoyj View Post
    The error is in the instruction 9.
    a.setFoodLevel(a.getFoodLevel() - 1);
    Most likely your error is in the previous line: "a = BuscarLista(k);" Check if that method (that should start with a lowercase letter) returns null. System.out.println( ... ) is your friend here.

    kind regards,

    Jos

Similar Threads

  1. Null pointer exception
    By talha06 in forum JDBC
    Replies: 5
    Last Post: 07-14-2009, 01:12 AM
  2. Help with null pointer exception
    By gammaman in forum New To Java
    Replies: 4
    Last Post: 07-14-2009, 12:23 AM
  3. Null Pointer Exception
    By andre1011 in forum Advanced Java
    Replies: 4
    Last Post: 02-07-2009, 03:30 AM
  4. Null Pointer Exception
    By ScKaSx in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 11:27 AM
  5. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 01: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
  •