Javascript nullpointer fixing