So my activity depends on a character sequence sent to it as such
id = bundle.getCharSequence("id").toString();
This works - except every now and then if I exit the activity and return to it the data I need from
item = realm.where(LogEntry.class).contains("id", id).findFirst();
Gets garbage collect? (not sure) - but I get a lot of errors such as
java.lang.NullPointerException: Attempt to invoke virtual method 'double dataObjects.LogEntry.getLatitude()' on a null object reference
Referring that item is now null which leads to the activity being empty.
I thought making them global would fix this, but obviously it has not. My question is - is there any way to preserve this bundled information?
Try use breakpoints to see the content of "id" and you should try use:
args.putString("id",stringValue); in your arg settings
and retrieve using:
String id = args.getString("id");
Anyway, the problem seems to be LogEntry object, wich is null, the code that you provided doesn't let us know how are you creating the LogEntry object. Use the Run tab to see wich line of your code is making that exception and use breakpoints to see in what part the asignation/creation of LogEntry you are getting null.
Related
Similar questions on stackoverflow that resemble my situation are: First and Second.
The answers to the First question suggest initializing the ArrayList before using it. The Second question does not have an answer, however, the author of the question says that 'after trying a few times, the error solved automatically.
Initially, I hadn't initialized my ArrayList and got the above-mentioned error. However, after initializing too, I get the same error after a few iterations. To clarify more, the following is my situation.
ArrayList1 stores values from different sensors where the value on the first index is the preferable value in my project. Every second (one set of data sampled at every second), the values in the ArrayList1 changes, and I am trying to save the value at index = 0 of the ArrayList1 to the next ArrayList (ArrayList2) by appending it with the following code snippet.
ArrayList2.add(Float.valueOf(String.valueOf(ArrayList1.get(0))));
Note that, both the ArrayLists are declared and initialized as Public. My code runs perfectly for the first FOUR iterations and throws the Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference error after that. I verified it by printing both the ArrayLists on the console.
Am I missing something here?
I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
Hey, how much gas is left in the tank of the car?
What are you talking about, we don't have a car.
That is exactly what is happening in your program. You wanted to call a function like _car.getGasLevel(); but there is no car, the variable _car is null.
Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.
Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual null value to where it originally came from, to find what the problem is and what the solution might be.
null can have different meanings: variables not set to another value will be null, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.
How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is null, find out which one.
Fix it
Once you know which variable it is, find out how it ended up being null. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set to null. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.
simply the variable/function you are trying to access from the class does not exist
someClass.xyz();
above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass does not exist
The following will work fine
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// create an instance of the class
final someClass = SomeClass();
// access the xyz function
someClass.xyz();
}
I have some code and when I run it produces an error, saying:
NoSuchMethod: the method 'XYZ' was called on null
What does that mean and how do I fix it?
Why do I get this error?
Example
As a real world comparison, what just happened is this conversation:
Hey, how much gas is left in the tank of the car?
What are you talking about, we don't have a car.
That is exactly what is happening in your program. You wanted to call a function like _car.getGasLevel(); but there is no car, the variable _car is null.
Obviously, in your program it might not be a car. It could be a list or a string or anything else really.
Technical explanation
You are trying to use a variable that is null. Either you have explicitly set it to null, or you just never set it at all, the default value is null.
Like any variable, it can be passed into other functions. The place where you get the error might not be the source. You will have to follow the leads from the actual null value to where it originally came from, to find what the problem is and what the solution might be.
null can have different meanings: variables not set to another value will be null, but sometimes null values are used by programmers intentionally to signal that there is no value. Databases have nullable fields, JSON has missing values. Missing information may indeed be the information itself. The variable bool userWantsPizzaForDinner; for example might be used for true when the user said yes, false when the user declined and it might still be null when the user has not yet picked something. That's not a mistake, it's intentionally used and needs to be handled accordingly.
How do I fix it?
Find it
Use the stack trace that came with the error message to find out exactly which line the error was on. Then set a breakpoint on that line. When the program hits the breakpoint, inspect all the values of the variables. One of them is null, find out which one.
Fix it
Once you know which variable it is, find out how it ended up being null. Where did it come from? Was the value never set in the first place? Was the value another variable? How did that variable got it's value. It's like a line of breadcrumbs you can follow until you arrive at a point where you find that some variable was never set, or maybe you arrive at a point where you find that a variable was intentionally set to null. If it was unintentional, just fix it. Set it to the value you want it to have. If it was intentional, then you need to handle it further down in the program. Maybe you need another if to do something special for this case. If in doubt, you can ask the person that intentionally set it to null what they wanted to achieve.
simply the variable/function you are trying to access from the class does not exist
someClass.xyz();
above will give the error
NoSuchMethod: the method 'xyz' was called on null
because the class someClass does not exist
The following will work fine
// SomeClass created
// SomeClass has a function xyz
class SomeClass {
SomeClass();
void xyz() {
print('xyz');
}
}
void main() {
// create an instance of the class
final someClass = SomeClass();
// access the xyz function
someClass.xyz();
}
I have the following line of code in onCreateView() method of my Fragment. It warns me that the expression to createPinPresenter.setLoginResult() can be null.
So I ask AS to generate the null check and it does this.
Even after the auto generated code, AS still complains the same expression being null. It obviously cannot be null inside the check.
Am I missing something obvious here or is this a bug?
Edit: I'm using AS version 2.2.3
This is correct, how does AS know if, for example, getParcelable() will return the same value? it is just a syntactic control, not a semantic control.
A function could return, for example, null if the number of times it has been called is odd: in that case the warning is correct.
Think this:
if (getNextValue() != null)
value = getNextValue();
If getNextValue() increments an index, at the end of an array it could return null: the error is pretty obvious, and it's actually what the control tries to prevent in your code.
The only solution is to store the result of getParcelable(KEY_LOGIN_RESULT) in a temporary variable, in that case AS will correctly manage it.
And of course... the fact that this is an autogenerated code, is actually a bug, I think, of the autogeneration, that is less smart than the control.
Following statement is not executing on some devices.
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
Is there any alternate method for above statement. On some devices it works fine but on other devices it gives exception.
Edit: My application is developed in tab. OnActivity results is used for getting picture from camera activity. Dont know where the resides but when I puts the above statement in try catch then no force close occurs and exception is shown there.
The problem is not really with the casting, but the assumption that the Object returned by getLocalActivityManager().getCurrentActivity() is of type AddEvent. It would be difficult to say in what scenario does the getLocalActivityManager().getCurrentActivity() return an Object that is of not the AddEvent type without looking at your implementation.
The following piece of code will check if the returned Object is indeed of type AddEvent:
if(getLocalActivityManager().getCurrentActivity() instanceof AddEvent){
AddEvent act1 = (AddEvent) getLocalActivityManager().getCurrentActivity();
}
But in your case, it is recommended to check in what scenario does the getLocalActivityManager().getCurrentActivity() return an instance of fable.eventippo.Home as against fable.eventippo.AddEvent.
I am not sure where do you write this line but logcat output says: You are trying to cast Home Activity into AddEvent and this is why its giving you ClassCastException.
FYI, getLocalActivityManager().getCurrentActivity(); is returning Home.