I have a function which is supposed to work with given ArrayList<String> and return the output as ArrayList<Integer>. The problem is NullPointerException is raised on hourList.add(hourInt); and I don't know why.
public ArrayList<Integer> takeTime(ArrayList<String> logs) {
ArrayList<Integer> hourList=null;
Integer hourInt;
for(String line: logs) {
String[] matrix = line.split(" ");
String[] hour = matrix[3].split(":");
// System.out.print(hour[0]+"\n");
String s = hour[0].replaceFirst("^0+(?!$)", "");
hourInt = Integer.parseInt(s);
System.out.print(hourInt+",");
hourList.add(hourInt); // <--- NullPointerException here
}
return hourList;
}
System.out.print(hourInt+",");:
0,0,0,0,1,1,2,3,4,4,4,4,4,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10
You need to construct your ArrayList while declaring it:
ArrayList<Integer> hourList = new ArrayList<>();
Otherwise hourList.add will give NPE since reference hourList is null.
You have to initialize the ArrayList:
List<Integer> hourList = new ArrayList<Integer>;
You are trying to call a function of a null object. Try:
ArrayList<Integer> hourList=new ArrayList<Integer>();
Related
here I get Student id get correct format but their status i got last value only
my java bean code
String[] arr1=new String[1000]; //stuid
if(aa!=null)
{
arr1=aa.split(",");
}
String[] arr = new String[1000]; //status
if(ss!=null){
arr = ss.split(",");
}
for(int j=0;j<arr1.length;j++)
{
for(int i=0;i<arr.length;i++)
{
bb.setStuid(arr1[j]);
bb.setStatus(arr[i]);
bb.setSid(sid);
bb.setCid(cid);
bb.setTtid(ttid);
bb.setDate(date);
bb.setDid(did);
bb.setHour(hour);
}
bb=ad.AddAttendance(bb);
}
return bb;
}
see my images
and my mysql inserted value is
result page in mysql database (wrong value)
But, what I want exactly
correct data show in front end
You are always looping the entire arr array for each of the arr1 element. And that's why for each stuid, status is the last value (since the last value is stored inside bb.setStatus() when the inner loop completes for one studId).
You have to use single loop. And I think you will get your desired result.
String[] arr1=new String[1000]; //stuid
if(aa!=null)
{
arr1=aa.split(",");
}
String[] arr = new String[1000]; //status
if(ss!=null){
arr = ss.split(",");
}
for(int j=0;j<arr1.length;j++)
{
bb.setStuid(arr1[j]);
bb.setStatus(arr[j]); // It will take status of j'th studId
bb.setSid(sid);
bb.setCid(cid);
bb.setTtid(ttid);
bb.setDate(date);
bb.setDid(did);
bb.setHour(hour);
}
bb=ad.AddAttendance(bb);
}
return bb;
}
Hope this will help.
Hi hope this will work.
You should only use single for loop.
String[] arr1=new String[1000]; //stuid
if(aa!=null)
{
arr1=aa.split(",");
}
String[] arr = new String[1000]; //status
if(ss!=null){
arr = ss.split(",");
}
for(int j=0;j<arr1.length;j++)
{
bb.setStuid(arr1[j]);
bb.setStatus(arr[j]);
bb.setSid(sid);
bb.setCid(cid);
bb.setTtid(ttid);
bb.setDate(date);
bb.setDid(did);
bb.setHour(hour);
bb=ad.AddAttendance(bb);
}
return bb;
}
I have 2 Arrays.
One Array has Strings, which i look for.
static String[] namesToLookFor = { "NR", "STAFFELNR", "VONDATUM"};
the otherArray has Strings, which i got from a *.csv file.
indexString = indexReader.readLine();
indexArray = indexString.split(";");
My Goal is to system.out.println() the Values which are the indexArray[] and NOT in the namesToLookFor[].
For example:
namesToLookFor = {"NR"};
indexArray = {"HELLO","NR"};
//Any Algorithm here...
So in this case"HELLO" should be printed out, since it is NOT in the namesToLookFor[] Array.
If you are using java8 you can do the following
List<String> list = Arrays.asList(namesToLookFor);
Arrays.stream(indexArray)
.filter(item -> !list.contains(item))
.forEach(System.out::println);
You could iterate over your indexArray and check for each element if its contained in your namesToLookFor Array:
String[] namesToLookFor = {"NR"};
String[] indexArray = {"HELLO","NR"};
List<String> excludedNames = Arrays.asList(namesToLookFor);
for(String s : indexArray) {
if (!excludedNames.contains(s)) {
System.out.println(s);
}
}
Will output only "HELLO".
// Put array into set for better performance
Set<String> namesToFilter = new HashSet<>(Arrays.asList("NR", "STAFFELNR"));
String[] indexArray = indexReader.readLine().split(";");
// Create list with unfiltered values and remove unwanted ones
List<String> resultList = new ArrayList<>(indexArray);
resultList.removeAll(namesToFilter);
// Do with result whatever you want
for (String s : resultList)
System.out.println(s);
With Array you can use contains function but after converting it to be ArrayList, the contains function will check if the ArrayList contains a specific value.
for (int i =0; i<indexArray.length; i++) {
if (!Arrays.asList(namesToLookFor).contains(indexArray[i]))
System.out.println(indexArray[i]);
}
I have this two ArrayLists. I need to iterate through the stationNames Array and get the index. Then return the value of the same index of stationIDs array. How do i achieve this? Please help me. The existing solutions on stackoverflow didn't work on this. I am new to android development.
When i iterate through the Array like the following code it give an error saying Array type expected; found: java.util.ArrayList<java.lang.String>
ArrayList<String> stationNames = new ArrayList<String>();
ArrayList<String> stationIDs = new ArrayList<String>();
stationName = "Hello"
int index = -1;
try {
for (int i = 0; i < stationNames.length; i++) {
if (stationNames[i].equals(stationName)) {
index = i;
break;
}
}
}
catch (Exception e){
Log.d("Excetion!",e.getLocalizedMessage());
}
return stationIDs[index];
stationNames here is not an array , but an ArrayList. So, you cannot use .length on it, use .size() :
Even simpler to do :
return stationNames.indexOf(stationName);
if it is found in the list will return its position or -1 if not.
Without all the hazzle of try catches and for loops. I could do the same task with one line of code, like following...
int output = stationIDs.get(stationNames.indexOf(stationName));
Thank you all for your help!
Just use indexOf method of the ArrayList object.
ArrayList<String> stationNames = new ArrayList<String>();
ArrayList<String> stationIDs = new ArrayList<String>();
String stationName = "Hello"
int index = stationNames. indexOf(stationName);
if(index!=-1){
String value= stationIDs[index];
}
public String[][] fetchData()
{
String[][] data = null;
int counter = 0;
while (counter < 10){
data[counter] = new String[] {"abc"};
counter++;
}
return data;
}
Getting the error in this loop.
Please let me know where i am wrong
You need to allocate memory to data.
String[][] data = new String[ROW][COLUMN].
Read this
String[][] data = null;
==> you have a null pointer exception when you try to write in data
You might do
String[][] data = new String[10][];
You get a NPE because you explicitly set data to null:
String[][] data = null;
You need to allocate the number of rows first:
String[][] data = new String[][NUMBER_OF_ROWS];
data[counter] = new String[] {"abc"};
Here you are putting "abc" to array, but why you're using array if it has only one cell?
data[counter] = new String("sample string");
would be enough. And ofc you need also to declare "data" as one-dimensional array.
i have this code:
public class Test{
arrayList<String> list = new ArrayList<String>();
String[][] temp_list;
public static void main(String[] args)
{
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
i want to transfer the first item in 'list' into temp_list[0].compiling is success but i got error when i run it.this is the error:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:this line=>temp_list[0] = temp.split(" ");)
anyone can help me?
This is because you haven't allocated any 2D-array for temp_list. (Which array should the result of split be stored in?)
Here's a working version of your snippet.
import java.util.ArrayList;
public class Test {
static ArrayList<String> list = new ArrayList<String>();
static String[][] temp_list;
public static void main(String[] args) {
list.add("hello wold");
// allocate memory for 10 string-arrays.
temp_list = new String[10][]; <-----------
String temp = list.get(0);
temp_list[0] = temp.split(" ");
}
}
This code would will not compile since list is declared as a member variable of the class but main is a static method.
As written, list has nothing added too so the call to list.get(0) will throw an Exception (not null pointer though).
The array temp_list is not allocated (no new) in the code given so trying assign into it will throw a null pointer exception.
You need to initialize temp_list before you use it. You need to specify the size of the array. For example:
int sizeOfArray = 5;
String[][] temp_list = new String[sizeOfArray][];