Java map.get null pointer exception
Jongware Zhu Sijia Zhu Sijia 1 3 3 bronze badges. Use camel casing in Java fileChar instead of FileChar for variables and methods, it looks like FileChar is a reference to a static class. If this is the case then that is the beginning of the problem. I have change to uppercase of first letter to lowercasse, I think the problem might be the valueof char? I not sure and I dont know how to fix — Zhu Sijia.
Post the full stack trace error , not just the pointer exception — Kafros. Exception in thread "main" java. NullPointerException at Main.
Add a comment. Active Oldest Votes. If you haven't put a value for the key 'a', you cannot expect to get a value for the key 'a'; For example if I do: hmap. Kafros Kafros 11 11 bronze badges. ValueOf FileArray[j] , char works fine. NullPointerException — Zhu Sijia. Task :desktop: Desktop Launcher. Exception In Initializer Error. Caused by: java. Null Pointer Exception at com. At the point when catTexture is initialized the Gdx.
I am guessing that your TacoCat is extending ApplicationAdapter or Application , and you need to override the create method and load your resources or instanciate your Cat object in that method. This is because the libGDX runtime needs to initialize first and things like texture loading must run on the OpenGL thread. Note that you don't have to manually call create , the libGDX runtime will call that for you.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Null pointer exception in libgdx Ask Question. Asked 2 days ago. Active today. Viewed 32 times. NullPointerException - if the specified key is null and this map does not permit null keys optional. I am sure there is a good reason for this decision, but I don't know exactly what it is.
Shouldn't containsKey always return false even if the map doesn't allow null values as keys? Null clearly isn't a key in the map if the map doesn't allow keys, so I would think it should return false. The particular instance I am thinking of is a TreeMap.
I changed a Map instance from a HashMap to a TreeMap for performance reasons, and this caused a subtle bug, because containsKey for HashMap does not throw a null pointer exception. I feel like there is not a good reason for this type of change to cause a subtle bug like this. Edit I am aware of the optional nature of the NullPointerException. Its optional nature does not, in my opinion, explain why it is allowed. To give a little more context as to why I am confused about this, containsKey is essentially comparing keys via equality.
I am, to some degree, coming from a functional background, wherein in say, Haskell the equivalent of containsKey would have an equality constraint on it, and would be valid for all values which can be compared via equality. The java docs for Object. Therefore, I would think that containsKey must return false if the map does not support null keys, since the implementation "should" be equivalent to iterating over the keys with k.
Your argument applies evenly to the ClassCastException : Shouldn't containsKey always return false even if the map doesn't allow that type of value as keys? Answer: Instead of forcing implementations to check the given key for validity, the API documented behavior allows an exception to be thrown for invalid key values.
Note that both exceptions are listed as " optional ", which means that is it up to the implementation whether to do that. The implementation may choose to simply return false , or it may choose to throw one of those exceptions. Implementations will probably choose to not add special logic for this, so if the algorithm "natively" throws exception, then so be it.
The API was documented to allow this default behavior. HashMap doesn't care about the object type, so will never throw ClassCastException. The logic of containsKey for a map that doesn't support null keys may or may not do something that throws NullPointerException if the given key is null, and that is a valid results, so the logic doesn't have to waste extra code to handle it.
You use containsKey on a map that supports a null key. As the user of this function you expect to get either true or false.
0コメント