Results 1 to 8 of 8
Thread: Null Pointer exception
- 01-29-2010, 01:43 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
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.
- 01-29-2010, 02:25 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
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.
- 01-29-2010, 03:16 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
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.
- 01-29-2010, 03:18 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
- 01-29-2010, 03:29 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
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();
}
}
}
- 01-29-2010, 03:42 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
- 01-29-2010, 04:10 PM #7
Member
- Join Date
- Jan 2010
- Posts
- 7
- Rep Power
- 0
The error is in the instruction 9.
a.setFoodLevel(a.getFoodLevel() - 1);
- 01-29-2010, 04:17 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,381
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Null pointer exception
By talha06 in forum JDBCReplies: 5Last Post: 07-14-2009, 01:12 AM -
Help with null pointer exception
By gammaman in forum New To JavaReplies: 4Last Post: 07-14-2009, 12:23 AM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
Null Pointer Exception
By ScKaSx in forum New To JavaReplies: 1Last Post: 01-24-2009, 11:27 AM -
Null Pointer Exception
By Jacinth in forum New To JavaReplies: 4Last Post: 01-22-2009, 01:47 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks