How to get last object from arraylist in doChangeActivities? - java

Here is my code, i want to get last object from arraylist to get and set it for PrintActivity, can i?enter image description here

if(arrayList != null && !arrayList.isEmpty()){
arrayList.get(arrayList.size() - 1);
}

Related

How to avoid the null values in String array in java..?

String[] m_cPackageName;
int m_size;
int j=0;
List<ApplicationInfo> installedList = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
m_size = installedList.size();
m_cPackageName=new String[m_size];
for (PackageInfo pi : pkginfoList) {
try {
m_appinfo = packageManager.getApplicationInfo(pi.packageName, 0);
if ((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// equal to zoo means system apps, not equal is third party installed apps
m_cPackageName[j]=pi.packageName;
j++;
}
} catch (NameNotFoundException e) {
Log.d(getClass().getSimpleName(), "Name not found", e);
}
Here I'm getting total size of installedList is 56..After filling the value in array it display in null values. how can i eliminate the null values.. Any one Help for me..
OUTPUT :
m_cPackageName=String[56];
m_cPackageName[0]="Myvalue"
m_cPackageName[1]="null"
m_cPackageName[2]="null"
.
.
.
m_cPackageName[55]="null"
Less than m_size elements are being added to the array (of size m_size), which results in unassigned null elements at the end.
This is because j is only increased sometimes - inside a conditional ((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0), and only when an exception is not thrown and caught.
A simple solution is to use an ArrayList, then only add the "approved" elements. Because a List/ArrayList grows on demand, there are no trailing null (unassigned) elements as might be found in a fixed-size array.
// List, not array
List<String> m_cPackageName = new ArrayList<String>();
// ..
if ((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// Only added to list sometimes, but m_cPackageName.size() is
// always the count of elements that HAVE been added.
m_cPackageName.add(pi.packageName);
}
// Then, if you really need an array, which will now be the proper size
// and not contain null elements because the size is computed based on the
// accepted elements in m_cPackageName which is m_cPackageName.size() ..
String[] arrayOfPackageNames = m_cPackageName.toArray(new String[0]);
add null check before inserting element to array.
if ((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
if(pi.packageName!=null){
m_cPackageName[j]=pi.packageName;
j++;
}
}
if (((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)&& (pi.packageName != null &&!pi.packageName.equals("")) {
// equal to zoo means system apps, not equal is third party installed apps
m_cPackageName[j]=pi.packageName;
j++;
}
You are allocating the array based on the size of installedList, but you are iterating over pkginfoList. A better way to do this is to create a List<String> packageNames = new ArrayList<String>();, and then add to that list in your loop.
That way, you don't have worry about counting or size.
if ((m_appinfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
// equal to zoo means system apps, not equal is third party installed apps
if(pi.packageName!=null){
m_cPackageName[j]=pi.packageName;
j++;
}
}
If you don't need to disambiguate empty and null strings, consider storing nulls as empty; something like this:
m_cPackageName[j]=pi.packageName == null ? "" : pi.packageName;
This might reduce the risk of things blowing up as NullPointerException-s further down.

Java giving out of bounds exception when calling function twice?

I'm trying to find out if there is a variable that exists at some point in an ArrayList, but, when calling the function that does this, twice, I get an java.lang.IndexOutOfBoundsException. But when the function is called only once, it doesn't give an error, even though, when calling the function twice, they check to see if different indexes exist, not the same one.
Code;
//package mj.mjo.Vars;
public boolean varExists(int index){
return mjo_vars.get(index) != null;
}
Note, mjo here is a variable, with vars being another variable that is the mj.mjo.Vars class
//package mj.play.StudioCanvas;
int nonsys = mjo.vars.setVar("TEST", "LOLOLOL", false); // returns 1
int yessys = mjo.vars.setVar("SYSVARTEST", "WOOHO!", true); // returns 2
System.out.println("DOES THE VAR \"TEST\" EXIST? " + mjo.vars.varExists(nonsys));
System.out.println("DOES THE VAR \"TEST\" EXIST? " + mjo.vars.varExists(yessys));
The error indicates that the value of index that you pass to get() is greater than or is equal to the number of elements in the list. Change your code as follows to avoid the exception:
public boolean varExists(int index){
return index >= 0
&& index < mjo_vars.size()
&& mjo_vars.get(index) != null;
}
In general, the error means that something is wrong with your indexing scheme: list and array indexes in Java start at zero, and end at size()-1, inclusive. If passing 2 triggers the exception, but passing 1 is OK, then the list has only two elements - at indexes 0 and 1.

Getting error when checking combobox for null value

I got a combobox, and a submit button, when the button is submitted, i want to check if the combobox value was null. Im using this code:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem().equals(null)) {
infoLabel.setText("Combo box value was null");
}
i am getting this error when i press the submit button:
java.lang.NullPointerException
how can i fix this?
You can not call equals on null. Instead simply use == null.
Something like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
Should work.
You can not give the null reference to equals(), do it like this:
ComboBox.setSelectedItem(null);
if (ComboBox.getSelectedItem() == null) {
infoLabel.setText("Combo box value was null");
}
And a remark that has nothing to do with your question: I suggest using the Java Naming Convention, which would lead to your combo box being named comboBox (and not ComboBox).
The condition should be :
ComboBox.getSelectedItem() != null
or
ComboBox.getSelectedItem().toString().equals("")
This checks if what is selected in the Combobox is null or empty
Another way of doing this is leaving the first item empty, then check for the selected index against 0 i.e
ComboBox.getSelectedIndex() != 0
Thanks

How to access NULL value

while(i<word.length)
{
ans=swn.extract(word[i], pos[i]);
if(ans== null)
polarvalue[i]= " ";
else
polarvalue[i]=ans;
i++;
System.out.println(ans);
}
Hi, Friends this is my code and the swn.extracts a value which can be null so the ANS contains the null value and when i try to access it gives NULlPOinterException is there any way that i can check the NULL value and change it to any other value.? But if i removes the whole If..else section it gives no error and prints the "NULL" in the output...
If i remove the whole If..else section then the code prints the null
value.
If above is true, mean your polarvalue[] array is null and you are trying to assigned the ith position value by using polarvalue[i] that's way, it's throwing null pointer exception.
Do a null check of polarvalue[] array before assigned.
Your code is dangerous, It seems it can throw NPE every where like
while(i<word.length) // do a null check
ans=swn.extract(word[i], pos[i]); // do a null check
polarvalue[i]= " "; // do a null check
polarvalue[i]=ans; // do a null check
Do a null check it will take few minutes but reduce your most valuable time .

NullPointerException in method

I have a method that gets returns a type SENSOR
In the bold is where I am getting a runtime NullPointerException, cannot understand why.
public Sensor getSensorAt(int x,int y,GridMap grid)
{
/*go through sensor storage array
* for eachsensor index call the get x get y method for that
* compare it to the x,y of the robot
*
*/
for(int i=0;i<s1.length;i++){
if(s1[i].getX() == x){ <======= NullpointerException
if(s1[i].getY()== y){
return s1[i];
}
}
}
return null;
}
You did not show us where s1 is created, but it looks like s1 does not have anything in it for some index i.
I tend to write my for loops like so to make code like this a bit cleaner
Object result = null;
for(int i=0;i<s1.length;i++){
Object current = s1[i]; // Replace Object with whatever your array actually contains
if(current.getX() == x && current.getY() == y) {
result = current;
break; // if you only need the first match
}
}
return result;
Things like formatting are important and will help you prevent bugs in the first place, and make them easier to find when they do happen....
Some of the elements in the array s1 is null, and when you are trying to invoke a method on that null object you are getting NPE.
Hope it helps you.

Categories

Resources