Error using getView() with a Listview - android

I'm am a newbie with Android development and using the following example to do something similar: https://stackoverflow.com/a/11626706/5724649
But am getting the following error: "Attempt to invoke virtual method 'java.lang.Object java.util.ArrayList.get(int)' on a null object reference"when I try to set one of the radio button. Also mSource (which is an ArrayList) is null within getView(), so I see why I could get this error, but not sure as to how I should pass the arraylist to getView(). Please help.

The error is about you calling a method on an object which currently "null".
This will also throw the same error:
String a = null;
a.length(); // <<< This will cause an error, because a is null.
You should make sure the following line:
mSource = new ArrayList<RowObject>();
is running before any of these lines:
mSource.get(position).setFirstChecked(true);
mSource.get(position).setFirstChecked(false);
if (mSource.get(position).isFirstChecked()) {

Related

Android Crashlytics Null Pointer exception, on a non null var

Craslitycs send a crash event of null pointer exception
NullPointerException: Attempt to invoke interface method 'java.lang.Object[] java.util.Collection.toArray()' on a null object reference
that refers to mutablelist.addAll(it.list), where it.list is an object from an api response.
When I try to check if this can be null, (it.list != null), AndroidStudio suggest Condition 'it.list != null' is always 'true' so, why this crash? And how can i fix it?
Thanks
You can check your list is empty or not by using kotlin collections function.
Use below function
ArrayList.isNullOrEmpty()
Reference link:
Kotlin collection

Context issue with Fragment

Beginner here.
I'm trying to implement a spinner with images using this guide;
https://dzone.com/articles/custom-spinner-for-android-application
I have it working correctly if implemented in my MainActivity.
The issues arise when I try to implement this within my fragment "Fragment1".
The code to initialize the spinner is as follows :
moodSpinner.adapter = MoodArrayAdapter(this, listOf(
Mood(R.drawable.angry, "Angry"),
Mood(R.drawable.happy, "Happy"),
Mood(R.drawable.playful, "Playful"),
Mood(R.drawable.wondering, "Wondering")
)
)
If that code is in my mainactivity.kt, and the spinner object with id moodSpinner in activity_main.xml, everything works fine. If I move the code to my fragment.kt, and the spinner into the fragment.xml, it fails.
I suspect the issue is related to 'this' being used as the context.
"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference"
I tried changing 'this' to 'this.requireActivity()' but no change.
Thanks.
Fixed. To those suffering the same problem of context within a fragment, I solved it by doing two things.
First, I placed the above code into the 'onViewCreated' method, and not in the 'onCreate' method.
Secondly, I replaced 'this' with 'requireActivity().baseContext' to pass the correct context to the ArrayAdapter.
Revised code looks like ;
moodSpinner.adapter = MoodArrayAdapter(requireActivity().baseContext, listOf(
Mood(R.drawable.angry, "Angry"),
Mood(R.drawable.happy, "Happy"),
Mood(R.drawable.playful, "Playful"),
Mood(R.drawable.wondering, "Wondering")
)

Attempt to invoke virtual method java.lang.String com.activeandroid.TableInfo.getIdName()

I am trying to copy values of one class1 object to class2 object using active android. I have written a clone function for this. When I instantiate class2 object, I get above mentioned exception. Here is my model
#Table(name = "table_config")
public class ConfigurationForServerSEND extends Model implements Serializable, Cloneable
{
// bunch of variables and getters/setters
}
Whenever I call this clone method, exception gets thrown. Here is the first line of code
ConfigurationForServerSEND config = new ConfigurationForServerSEND();
Here is what I get
class package_name.views.fragments.MyFragment
java.lang.NullPointerException: Attempt to invoke virtual
method 'java.lang.String com.activeandroid.TableInfo.getIdName()'
on a null object reference
at com.activeandroid.Model.<init>(Model.java:55)
Please note that I add this class configuration builder of active android, like this configurationBuilder.addModelClasses(MatchConfigurationForServerSEND.class);
What could be wrong? Why am I getting this exception when trying to instantiate class2 object. It was working yesterday (I use both Android 7, Android 8 with API level 26). Thanks for anything you share.

Getting a null pointer exception while calling getTimeStamp from firebase database

The first time i run the app after uninstalling & reinstalling sometimes it crashes due to
viewHolder.setTimestamp(DateUtils.getRelativeTimeSpanString((long) model.getTimestamp()).toString());
giving an error
Exception java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
what should i do to solve this issue as after crashing once the next time everything works fine.If you need more codes to guide me then just ask.
you can use it in try catch
like this way.
try
{
viewHolder.setTimestamp(DateUtils.getRelativeTimeSpanString((long) model.getTimestamp()).toString());
}
catch(Exception e)
{
}

String to Integer Android

I`ve problem with converting String to Integer.
public SharedPreferences abc;
abc = getApplicationContext().getSharedPreferences("Trening",0);
Integer i = Integer.parseInt(abc.getString("T1",null).toString());
The Error is:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference
I learn to program for 3 days and hope you can help me and explain how to fix it, so I can learn.
Sorry for my english. :)
If abc.getString("T1", null) returns null (this will happen if there is no Key-Value-Pair with the key "T1" in your SharedPreferences) then you try to get the String of null. This causes the NullPointerException. You should define another defaultValue which can be converted into a String.
Apart from that, you can leave toString() out because getString() returns a String:
Integer i = Integer.parseInt(abc.getString("T1", "-1"));
When you are using null as second argument in abc.getString("T1", null), actualy you are saying:
if "T1" value is not set, return null as default.
So you have parsInt(null) and it causes NullPointerExceprion.
You can replace abc.getString("T1", null) with abc.getString("T1", "0")
It will returns string "0" as default value of "T1" (if "T1" is null). So you have parsInt("0") that it works proprely.
So the problem basically is that the variable abc is null, therefore the getSharedPreferences call fails to retrieve a valid reference.

Categories

Resources