简体   繁体   English

无法从ArrayList获取值

[英]Unable to get values from ArrayList

I have an ArrayList which takes an object. 我有一个带对象的ArrayList。

Handler.java

public ArrayList<LinkedInAccountObject> getAllLinkedInUsersFromDatabase(){

    LinkedInAccountObject lao = new LinkedInAccountObject();
    counter++;
    lao.setAccountId(rs.getLong("account_id"));

    lao.setLinkedInAccountId(rs.getString("linkedin_account_id"));
    lao.setParentId(rs.getLong("parent_id"));
    lao.setFirstName(rs.getString("first_name"));

    lao.setLinkedInAccountId(rs.getString("linkedin_account_id"));
    lao.setEmail(rs.getString("email"));
    lao.setAccessToken(rs.getString("access_token"));
    lao.setExpiresOn(rs.getLong("expires_on"));

    laoarray.add(counter, lao);
}

My PageLoader.java uses this object to set values 我的PageLoader.java使用此对象设置值

ArrayList<LinkedInAccountObject> laoarray = lndb.getAllLinkedInUsersFromDatabase();

    for (LinkedInAccountObject lao : laoarray) {
    LinkedInPageObject lpo = new LinkedInPageObject();
    lpo.setCompanyID(lao.getParentId());
    lpo.setComment(lao.getComment());
    //lpo.setDescription(obj.getString("description"));
    //lpo.setTitle(obj.getString("title"));
    }

But im unable to use the lao object to get the details, getting an null pointer. 但是我无法使用lao对象获取详细信息,从而获得了空指针。

when i print the lao object, it gives the result as below, 当我打印老挝对象时,它给出如下结果,

array objects[null, LinkedInAccountObject [accountId=xxx, parentId=xx, expiresOn=xxx]] 数组对象[null,LinkedInAccountObject [accountId = xxx,parentId = xx,expiresOn = xxx]]

I would suggest that you replace your loop in the second code with the following: 我建议您将第二个代码中的循环替换为以下内容:

Iterator<LinkedInAccountObject> iterator = laoarray.iterator();
while(iterator.hasNext()){
    LinkedInAccountObject lao = iterator.next()
    LinkedInPageObject lpo = new LinkedInPageObject();
    lpo.setCompanyID(lao.getParentId());
    lpo.setComment(lao.getComment());
    //remaining of your code.
}

EDIT 编辑

From all the comments that have been posted here I feel that you have initialised the counter variable as 0 . 从这里发布的所有评论中,我觉得您已经将counter变量初始化为0 In your method getAllLinkedInUsersFromDatabase() you are looping through a loop using rs.hasNext() . 在您的方法getAllLinkedInUsersFromDatabase()您正在使用rs.hasNext()遍历一个循环。 Now if this is correct, Then when the first data is being stored, your method creates a new instance of LinkedInAccountObject in lao . 现在,如果这是正确的话,那么当存储第一个数据时,您的方法将在lao创建一个LinkedInAccountObject的新实例。 Then it goes on to increment counter by 1 . 然后继续将counter1 So in this case counter was 0 now it becomes 1 . 因此,在这种情况下, counter现在为0因此变为1

After all your code that you uploaded being executed in the last line it executes laoarray.add(counter, lao); 在上一行执行laoarray.add(counter, lao);所有上传的代码之后,它会执行laoarray.add(counter, lao); . Here counter being at 1 adds the lao object to the laoarray at position index 1, leaving position index 0 as null . 这里counter为1会将lao对象添加到位置索引为1的laoarray上,位置索引0为null Then it repeats till the end of the ResultSet . 然后重复直到ResultSet的结尾。 This means if your rs variable returned 10 rows, they would be added to laoarray in indices 1 to 10 with 0 index being null. 这意味着,如果您的rs变量返回10行,则会将它们添加到索引110 laoarray中,其中0 index为空。

You could verify if this is happening or not by simply System.out.println("laoarray length = "+laoarray.size()); 您可以通过System.out.println("laoarray length = "+laoarray.size());来验证是否正在发生这种情况System.out.println("laoarray length = "+laoarray.size()); in your 'PageLoader.java' after you have initialised the laoarray variable. 初始化laoarray变量后,在“ PageLoader.java”中laoarray

If this is correct then you could remove the counter++; 如果正确,那么您可以删除counter++; in the method getAllLinkedInUsersFromDatabase() from its present location and set it after laoarray.add(counter, lao); 在方法getAllLinkedInUsersFromDatabase()中从其当前位置开始,并在laoarray.add(counter, lao);之后进行设置laoarray.add(counter, lao); as shown below: 如下所示:

LinkedInAccountObject lao = new LinkedInAccountObject();
//counter++; remove it from here.
lao.setAccountId(rs.getLong("account_id"));

lao.setLinkedInAccountId(rs.getString("linkedin_account_id"));
lao.setParentId(rs.getLong("parent_id"));
lao.setFirstName(rs.getString("first_name"));

lao.setLinkedInAccountId(rs.getString("linkedin_account_id"));
lao.setEmail(rs.getString("email"));
lao.setAccessToken(rs.getString("access_token"));
lao.setExpiresOn(rs.getLong("expires_on"));

laoarray.add(counter, lao);
counter++;//place it here

Does you counter start at -1? 您是否从-1开始计数? You do counter++ before adding to the arraylist. 在添加到arraylist之前,您需要先进行counter ++操作。 This would mean that the first entry is never filled. 这意味着第一个条目永远不会被填充。

You could edit the code to start at index 0, or in the for loop check if the lao isen't null before handeling it. 您可以编辑代码以从索引0开始,或者在for循环中检查老挝在处理之前是否不为null。

This code doesn't seem complete but my guess is that you modify counter before adding the item so if you have an array and start with counter = 0 you add the first element to position 1 and position 0 remains null. 这段代码似乎并不完整,但是我猜想是在添加项目之前先修改计数器,所以如果您有一个数组并以counter = 0开头,则将第一个元素添加到位置1,位置0保持为空。

Make it: 做了:

LinkedInAccountObject lao = new LinkedInAccountObject();
.... fill object values ......
laoarray.add(counter, lao);
counter++; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM