Null pointer exception on consecutive execution of the test script - android

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
The values are getting extracted to the array and when printed, the proper values are getting inseted to the array and are even getting passed to another class.
But the execution of the junit test case will be successful only for 1 or 2 times. But from then i am getting a null pointer exception. Please help me. Am at a fix!!
The first value while getting inserted to the ArrayList itself is null. Here is the Code:
for(int k=1;k<=4;k+=2)
{
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++)
{
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d(" ","Values of the Strings are"+Actualvalues[i].toString());
}
}
The value getting inserted at the first line within the for loop itself is null. Hence the problem.
But I am not getting, why null value is being inserted at 0th position. Please help me out for solutions.

In your sharedLevel class, change:
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++)
to:
for(int i=0; i < sharefirstlevel.Actualvalues.length; i++)
Since your counter starts at 0, it should less than the length of your array.

Related

unable to retrieve values from two different arraylist

i am having two different arraylist of diffrent size,my problem is that i have to retrieve values from both the list and show that on to a textview,when i am using two loops it is first completing the inner loop and than executing outer loop and the vales are getting printed two times and if using break to break the inner loop it is completely ignoring inner loop after 1st loop.
ArrayList<LocationDto>location = new ArrayList<>();
education<EducationDto> = new ArrayList<>();
if (education.size() != 0) {
for (int j = 0; j < education.size(); j++) {
for (int k = 0; k < location.size(); k++) {
if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")" + " Located at " + location.get(k).getLocationName());
} else if (education.get(j).getSpecializationTitle().equalsIgnoreCase("") && !location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + " Located at " + location.get(j).getLocationName());
} else if (!education.get(j).getSpecializationTitle().equalsIgnoreCase("") && location.get(k).getLocationName().equalsIgnoreCase("")) {
tvEducation.append(education.get(j).getEducationTitle() + "(" + education.get(j).getSpecializationTitle() + ")");
} else {
tvEducation.append(education.get(j).getEducationTitle());
}
if (j != education.size() - 1) {
tvEducation.append(" , ");
}
break;
}
}
} else {
tvEducation.setText("Not Specified");
tvEducation.setTextColor(getResources().getColor(R.color.color_three));
}
what should i do now?
The break statement terminates a loop regardless of whether it has completed or not, so your problem is that you've put a break directly inside your inner for loop without some sort of condition. Therefore the first time the loop runs it will reach the break which is why it's terminating on the first iteration!
You probably mean to put the break inside the last if statement?

Which Json structure suitable for this work?

This function generate dummy data for this Structure .
private void createDummyData() {
for (int i = 1; i <= 10; i++) {
VerticalDataModel dm = new VerticalDataModel();
dm.setSectionTitle("Section " + i);
ArrayList<SingleItemModel> singleItem = new ArrayList<SingleItemModel>();
for (int j = 0; j <= 10; j++) {
singleItem.add(new SingleItemModel("Item " + j, "URL " + j));
}
dm.setSingleItemModelha(singleItem);
allSampleData.add(dm);
}
Know for generating this data from internet , i need json structor for this work .(I use volley library)
Thanks <3
{
data : [
{
"title":"section1",
"items":[{"name":"item1","url":"url1"},{"name":"item2","url":"url2"}]
},
{
"title":"section2",
"items":[{"name":"item3","url":"url3"},{"name":"item4","url":"url4"}]
}
]
}
so basically you have array of objects which contains title and items as its property. Again items is another array of objects which contains name and url as its property.

Get first argument in live-template

Recently i was creating my own live-template, i was customizing for loop, here is how default for loop is given in live-template.
for(int $INDEX$ = 0; $INDEX$ < $LIMIT$; $INDEX$++) {
$END$
}
but i want my method's first argument inplace of $LIMIT$, how can i do that?
public void getList(ArrayList<String> list)
{
}
then my for loop shoulde be
for(int i = 0; i < list.size; i++) {
...
}
I have seen template of logm, but it is printing all arguments of method
groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
You can add the following live template:
for(int $INDEX$ = 0; $INDEX$ < $VAR$.size(); $INDEX$++){
$END$
}
Then go to right button "Edit variables" and put the following predefined methods in the expressions fields:
You can find all predefined methods in Jetbrains documentation

Displaying values to a TextView

hi i have problem in displaying a value into my TextView..
For example i will input 1,2,3,4 then i like to display the output in this manner in my TextView..How can i do that? please help me, thank you in advance
1 appeared 1 times
2 appeared 1 times
3 appeared 1 times
4 appeared 1 times
here's my code:
String []values = ( sum.getText().toString().split(","));
double[] convertedValues = new double[values.length];
Arrays.sort(convertedValues);
int i=0;
int c=0;
while(i<values.length-1){
while(values[i]==values[i+1]){
c++;
i++;
}
table.setText(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
table.setText(values[i] + " appeared " + c + " times");
Make your textView to support multipleLines and after that create in code a StringBuffer and append to it the results, something like
resultString.append(result).append(" appeared").append(c).append(" times\n");
after that you set text for textView like:
textView.setText(resultString.toString());
Here is the idea :
// this is test string, you can read it from your textView
String []values = ( "2, 1, 3, 5, 1, 2".toString().split(","));
int [] intValues = new int[values.length];
// convert string values to int
for (int i = 0; i < values.length; ++i) {
intValues[i] = Integer.parseInt(values[i].trim());
}
// sort integer array
Arrays.sort(intValues);
StringBuilder output = new StringBuilder();
// iterate and count occurrences
int count = 1;
// you don't need internal loop, one loop is enough
for (int i = 0; i < intValues.length; ++i) {
if (i == intValues.length - 1 || intValues[i] != intValues[i + 1]) {
// we found end of "equal" sequence
output.append(intValues[i] + " appeared " + count + " times\n");
count = 1; // reset count
} else {
count++; // continue till we count all equal values
}
}
System.out.println(output.toString()); // prints what you extected
table.setText(output.toString()); // display output

Passing String Array to A Robotium Class

I am automating a product using Robotium. In a module I have to validate the data derived.
For that I am using this code:
class sharefirstlevel {
public void A {
for(int k=1;k<=4;k+=2) {
ExpectedResult = solo.clickInList(k);
for (int i = 0; i < ExpectedResult.size(); i++) {
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
solo.scrollListToLine(0, k);
ExpectedResult=solo.clickInList(1);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
ExpectedResult = solo.clickInList(2);
Actualvalues[i] += ExpectedResult.get(i).getText().toString() + " ";
Log.d("xyz","Values of the Strings are"+Actualvalues[i].toString());
}
}
}
}
Its extracting the values selected to an array.
Now when its derived, to validate I am using this code:
class sharedLevel {
public void B {
for(int i=0; i <= sharefirstlevel.Actualvalues.length; i++) {
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("xyz","Values are:"+sharefirstlevel.Actualvalues[i]);
}
}
}
But the thing is in the first part of code proper values are extracted to the Array.
In the second part of code when I am passing the array values, I am not getting the values but the memory allocation object. Hence the test case is failing.
Please help me. Am at a fix !!
How about using
assertTrue(Arrays.equals(expected, actual));
EDIT: This answer is for the question for which the bounty was raised and not the edited question (https://stackoverflow.com/posts/18334315/revisions)
I solved the Problem. The change which I had to make in Class B was
for(int i=1;i<sharefirstlevel.Actualvalues.length;i++){
Log.d("Pratibha","Values are:"+sharefirstlevel.Actualvalues[i]);
actual=solo.searchText(sharefirstlevel.Actualvalues[i]);
assertEquals(expected, actual);
Log.d("TAG","Values are:"+sharefirstlevel.Actualvalues[i]);
}
And hence I am getting the values of the array in class B. Because the index started from 0, and I din't have any text values at 0th position, It was returning garbage values.
Thanks all for the responses.

Categories

Resources