0

I'm creating an ArrayList of type B:

ArrayList<B> cert= new ArrayList<B>;
B a  = util.getCerts(path).iterator().next();
this.cert.add(a);
this.certNode(certs);

I'm getting a null pointer exception when I try to set the value:

void certNode(ArrayList<B> certResp) 
{       
    ArrayList<RespNode> exp = new ArrayList<RespNode>();
    for (int i = 0; i< certResp.size(); i++) {  
        exp.get(i).setxxx(certResp.get(i).getxxx());
        exp.get(i).setxxx(certResp.get(i).getxxx().toString());     
    }
 }

Any help would be great!

2
  • Don't add value with .get(i).set. To set a value you already need a value at this position. What you want to do is to ADD a value. Just replace with exp.add(new RespNodde(certResp.get(i).getxxx())); Commented Nov 25, 2014 at 13:24
  • Not sure whether this is your problem, but since it has not yet been mentioned in any of the answers: Note that you create cert, but then you try to use this.cert, which is not the same! Commented Nov 25, 2014 at 13:25

5 Answers 5

2

Since you just created the ArrayList instance exp, exp.get(i) doesn't exist, so you can't call exp.get(i).setxxx(...).

EDIT :

Try :

void certNode(ArrayList<B> certResp) {      
    ArrayList<RespNode> exp = new ArrayList<RespNode>();
    for (int i = 0; i< certResp.size(); i++) {  
        exp.add(certResp.get(i).getxxx());     
    }
 }

It's hard to be sure without knowing the return value of certResp.get(i).getxxx(), but if it returns RespNode, the code above would add that RespNode instance of the list.

Sign up to request clarification or add additional context in comments.

6 Comments

Is there any other way i can set the values
Wouldn't the exception in the case of an empty List be IndexOutOfBoundException?
exp.addAll() is not possible first arrayList is ArrayList<B> and second list is of type ArrayList<RespNode>
Again you are setting value in same list ArrayList<B> and not other one'
exp.addAll(certResp)-- I tried doing this but it says it is not applicable to ArrayList<>
|
1

The problem is here : exp.get(i). exp is the newly created ArayList, so it's empty, so there is a null at index i

1 Comment

The loop is on certResp, which is the method's parameter and might not be empty !
0

Your list 'exp' is empty. In the for-loop you get a value of null; calling a method from null will cause the NullPointerException.

You have to put values in the list before you try to access them.

Comments

0
    void certNode(ArrayList<B> certResp) {      
        ArrayList<RespNode> exp = new ArrayList<RespNode>();
        for (int i = 0; i< certResp.size(); i++) {  
            exp.add(new RespNode()); // <----- 
            exp.get(i).setxxx(certResp.get(i).getxxx());
            exp.get(i).setxxx(certResp.get(i).getxxx().toString());     
        }
     }

btw you can improve syntax

    void certNode(ArrayList<B> certResp) {      
        ArrayList<RespNode> exp = new ArrayList<RespNode>();
        for (B b : certResp){
          RespNode resp = new RespNode();
          resp.setxxxx(b.getxxxx());
          exp.add(resp);
        }
     }

Comments

0

As you are creating new empty ArrayList i.e []

exp.get(i) 

gives you null

Instead you can use add method of ArrayList

void certNode(ArrayList<B> certResp) {
    //Here new empty list is created i.e exp=[] //no elements inside
    //Suppose your certResp=[B@12312,B@123834] i.e two B class objects     
    ArrayList<RespNode> exp = new ArrayList<RespNode>();

    for (int i = 0; i< certResp.size(); i++) {  //Size of certResp=2 as it contains 2 B objects  
        RespNode res=new RespNode(); //Create a RespNode class as you want to add of certResp ArrayList<RespNode>

        res.setxxx(certResp.get(i)); //Take value of xxx method of certResp.get(i) i.e B object xxx method and set into res.setXXX or RespNode class

        exp.add(res); //add res object into ArrayList
    }
 }

4 Comments

I added comments for particular lines
Thanks Shoaib! and also can i change the return type of this ArrayList<RespNode>
I mean instead of void certNode i want to return this as ArrayList<RespNode>
Depends on your class what you are using. java-tips.org/java-se-tips/java.util/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.