java - NullPointerException in LinkedHashMap -
i'm creating game. when i'm reading linkedhashmap, gives me npe.
i fill linkedhashmap hm this:
for (string s : line.split("")) { if (s.contains("*")) { hm.put(new coordinates(xa-32, ya), "gray"); } else if (s.contains("#")) { hm.put(new coordinates(xa-32, ya), "black"); } // other code... } later try color hashmap this, , npe:
if ((keycode == keyevent.vk_left || keycode == keyevent.vk_a) && ispainted && hm.get(new coordinates(x - 32, y)).equalsignorecase("gray")) { x -= 32; } full code here:
on line hm.get(new coordinates(x, y - 32)).equalsignorecase("gray")), not possible hm contains newly created coordinates.
when create new object, example coordinates c = new coordinates(x, y - 32);, in memory created object , variable c holds reference memory, not object itself.
because of it, @ code :
coordinates c1 = new coordinates(x, y - 32); //c1 holds reference memory, "a8wgge8h" coordinates c2 = new coordinates(x, y - 32); //c2 holds reference memory, someting "a8w12238h" if (c1 != c2){ system.out.println("yes, true, c1 not c2, there 2 objects same properties, not same, human twins - same, 2 people exists"); } therefore cant find in hm.get(new coordinates(x, y - 32)), because not coordinate has same x,y, looks coordinate same reference memory. , cannot exists, because create new object, java associated new memory address, abnbn147 , list/set looks object address abnbn147, cannot stored there, because have created it.
this hm.get(new coordinates(x, y - 32)) return null. if call method on null, ends nullpointerexception, happens calling method equalsignorecase on null object talking about.
Comments
Post a Comment