nodeadman123
nodeadman123

Reputation: 3

Not able to add the contents of two lists in java

public class runnnn {

    static List abc= new ArrayList();
    static List def = new ArrayList();
    static List ghi = new ArrayList();
    public static void main(String[] args) {
        Form pForm = null;
        Form pForm1 = null;
        Form pForm2 = null;
        Form pForm3 = null;
        Form pForm4 = null;
        Form pForm5 = null;
        int i;
        int mn = 0;;
        for(i=0;i<5;i++)
        {
            pForm = new Form();
            String asd = "asdasd"+ i;
            pForm.setPhase(asd);
            pForm.setCount(i);
            abc.add(pForm);
        }

        for(i=0;i<3;i++)
        {
            pForm3 = new Form();
            String asd = "asdasd"+ i;
            pForm.setPhase(asd);
            pForm.setCount(i);
            def.add(pForm3);
        }

        for(int j=0;j<abc.size();j++){
            pForm1=null;
            pForm1=(Form)abc.get(j);
            pForm4 = null;
            mn=pForm1.getCount();
            for(int k=0;k<def.size();k++){
                pForm2=null;
                pForm2=(Form)def.get(k);
                if(pForm1.getPhase() == pForm2.getPhase()){
                    mn = mn + pForm2.getCount();
                }
            }
            pForm4.setPhase(pForm.getPhase());
            pForm4.setCount(mn);
            ghi.add(pForm4);
        }


        for(int j=0;j<ghi.size();j++){
            pForm5=null;
            pForm5=(Form)ghi.get(j);
            System.out.println(pForm5.getPhase()+"  "+pForm5.getCount());
        }    
    }

}

here this is the error i am getting

pForm4.setPhase(pForm.getPhase());

Null pointer access: The variable pForm4 can only be null at this location

i am trying to add the count of one list to another the second list is a subset of the first

Upvotes: 0

Views: 69

Answers (1)

Loki
Loki

Reputation: 4130

You never invoke
pForm4 = new Form();

From your code:

 pForm4 = null;
    mn=pForm1.getCount();
    for(int k=0;k<def.size();k++){
        pForm2=null;
        pForm2=(Form)def.get(k);
        if(pForm1.getPhase() == pForm2.getPhase()){
            mn = mn + pForm2.getCount();
        }
    }
    pForm4.setPhase(pForm.getPhase());

You're setting pForm4 to null and try to call a method? That's not working.

Upvotes: 3

Related Questions