hello all i tried to fetch data from databse and try to stored it in an array and i want to compare new value that come from database with array if this new value is already stored in this array i like to executed new query if not in array i want add this in array and execute another query
in java

iam trying code like this but it say error in if statment

 String fgroupname=null;
             String fetchmname=null;
             String name[]=new String[50];
             String groupname[]=new String[50];


             int i = 0,j=0;
             name[i]=null;
              Statement stgc = connect.createStatement();
                 ResultSet rsgc = stgc.executeQuery("SELECT  ledger.groups, Group_create.Name, ledger.ledger_name FROM ledger left JOIN Group_create ON ledger.groups=Group_create.groups ");

                while(rsgc.next())
                {
                    groupname[j]=rsgc.getString(1);
                   fetchmname=rsgc.getString(2);
                   System.err.println(groupname[j]);

                    for(i=0;i<name.length;i++)
                    {
                        System.err.println("i am in first for");

                        for(j=0;j<groupname.length;j++)
                     {
                         System.err.println("i am in seconed for");
                     if(name[i].equals(groupname[j]))
                     {   
                      System.err.println("i am in if");
                     }
                     else                                        
                     {
                        fgroupname= groupname[j];

                         ResultSet rsdkg = st.executeQuery("select currbal_type,sum(curr_bal),groups from ledger where groups='"+fgroupname+"' or groups='"+fetchmname+"' group by currbal_type,groups"); 
                        while(rsdkg.next())
                        { 
                    System.out.println(""+rsgc.getString(1));


                }

                     name[i+1]=groupname[j];
                     groupname[j]=null;
                     i++;

                     }
                     }
                }
                }

What EXACTLY is the error message?

well ... duh.

do you see any reason why this:

name[i]=null;
followed by this:
if(name[i].equals(groupname[j]))
might throw a NullPointerException?

yes friend this is giving null pointer exception

here is output of this :-

BANK OCC A/C
i am in first for
i am in seconed for
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at transaction.trialBalance.trial_bal(trialBalance.java:154)
    at transaction.trialBalance.<init>(trialBalance.java:38)

** plese tell me what now i do to solve this null pointer exception**

not set the value of name[i] to null.
you are calling he equals method on a non-instantiated variable (null).
I have no idea what you are using that if for anyway. the name array is empty.
what do you think you are comparing?

thanks friend i solved this problem by using boolean value by making true false thanks for your advice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.