Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Rest of the code not mentioned here are correct...

import java.io.File;
import javax.swing.JOptionPane;

class abc
{
    // Fall is a directory of files
    for(File fr:Fall.listFiles())
                        {
                             // jtxt is an object of JTextArea()
                            if((jtxt.getText().contains("select ")) && (jtxt.getText().contains(" from ")) && (!jtxt.getText().contains(",")) && (jtxt.getText().contains(fr.getName())) && (ke.getKeyCode()==10))
                            {
                              String strField=jtxt.getText().substring(7).strip();
                                String crunch="";
                                char []p=strField.toCharArray();
                                char []a=new char[p.length];
                                for(int i=0;i<p.length;i++)
                                {
                                    if(p[i]!=' ')
                                    {
                                        crunch +=p[i];
                                    }
                                    if(p[i]==' ')
                                    {
                                        break;
                                    }
                                }
                                a=crunch.toCharArray();
                                String [] cell=new String[1];
                                for(int i=0;i<a.length;i++)
                                {
                                    cell[0]+=a[i];
                                }
                                String [] cc=new String[cell.length];
                                cc[0]=cell[0].substring(4);
                            
                                JOptionPane.showMessageDialog(null, cc[0]);
                            
                                try
                                {
                                
                                    FileReader fread=new FileReader(fr.getAbsolutePath()+"//"+"tbl");
                                    BufferedReader in=new BufferedReader(fread);
                                    String sql="";int val=0;int k=0;
                                    while((sql=in.readLine())!=null)
                                    {
                                     
                                        char [] pp=sql.toCharArray();
                                        char []aa=new char[pp.length];
                                        String []field=new String[p.length];
                                        int j=0;
                                        for(int i=0;i<pp.length;i++)
                                        {
                                            if(pp[i]!='-')
                                            {
                                                aa[i]=pp[i];
                                                field[j]+=aa[i];
                                            }
                                            if(pp[i]=='-')
                                            {
                                                j++;
                                            }
                                        }
                                    
                                        
                                        for(int i=0;i<j;i++)
                                        {
                                            if(cell[0].equals(field[i]))
                                            {
                                                val=i;
                                            }
                                        }
                                        k++;
                                    }
                                    int ss=0;int t=0;
                                    try
                                    {
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);
                                        JOptionPane.showMessageDialog(null, val);
                                        String str="";
                                        ss=(int)in.lines().count();
                                        while((str=in.readLine())!=null)
                                        {
                                            t=in.readLine().length();
                                        }
                                        fread.close();
                                        in.close();
                                    }                                    
                                    catch(IOException e)
                                    {
                                        
                                    }
                                     JOptionPane.showMessageDialog(null,"ss "+ ss);
                                    String [][] g=new String[ss][t+250];
                                    String [][] Unit=new String[ss][t+250];
                                    String[] s=new String[ss];
                                    try
                                    {
                                        
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);int y=0;
                                        String str="";int n=0;int jj=0;
                                        while((str=in.readLine())!=null)
                                        {
                                            //JOptionPane.showMessageDialog(null, str);
                                            char[] u=str.toCharArray();
                                            char[] b=new char[u.length];
                                            
                                            
                                            for(int i=0;i<u.length;i++)
                                            {
                                                if(u[i]!='-')
                                                {
                                                    b[i]=u[i];
                                                    Unit[y][jj]+=b[i];
                                                }
                                                if(u[i]=='-')
                                                {
                                                  JOptionPane.showMessageDialog(null,"unit : "+ Unit[y][jj]);
                                                    jj++;JOptionPane.showMessageDialog(null, "y : "+y);
                                                }
                                            }
                                            y++;
                                             
                                        }
                                        str="";
                                        in.close();
                                        fread.close();n=0;
                                        fread=new FileReader(fr.getAbsolutePath()+"//"+"data");
                                        in=new BufferedReader(fread);int b=1;int c=0;
                                        if(y==ss)
                                        {
                                        while(n<ss)
                                            {                                     
                                                for(int i=0;i<jj-1;i++)
                                                {                                                     
                                                    if(i==val)
                                                    {
                                                        g[n][i]=Unit[n][i];
                                                        JOptionPane.showMessageDialog(null, "g[c][i] : "+g[n][i]);
                                                        // the problem is here g[n][i] shows the first value correctly. but thenafter 
                                                        // it shows the values to be null, although the values are present in the file.
                                                    }                                                
                                                }                                               
                                                n++;c++;
                                            } 
                                        
                                        fread.close();
                                        in.close();
                                        n=0;
                                        
                                          
                                        
                                        
                                         TableViewStatic tvs=new TableViewStatic(g,cc,fr.getName());
                                           JTextFrame jf=new JTextFrame(tvs,jtxt,"SQL NoteBook");
                                           //These methods are correct
                                        }
                                    }
                                    catch(IOException e)
                                    {
                                            
                                    }
                                }
                                catch(IOException e)
                                {
                                    
                                }
                            }
                        }
}


What I have tried:

I cannot understand why Unit[n][i] becomes null just after showing the first value, when records are present in the file
Posted
Comments
Richard MacCutchan 4-Feb-24 8:52am    
The problemwill depend on the data being processed. You should use the debugger to trace it step by step as it reads each record.

1 solution

As Richard has said, it's going to depend on the data processed, and we have no access to your file system - the debugger is (as always) your best friend here as it can show you exactly what is going on while your program runs.
So, it's going to be up to you.

Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

But do yourself two big favours:
1) Stop using really short variables names: use meaningful names instead of "a", "p", "jj", "y", "c", and so on and your code becomes both much more readable, but more reliable too as it's more obvious when you use the wrong variable by mistake ...

2) Stop swallowing exceptions.
Java
catch(IOException e)
{

}
may make an error "go away" but it quite possible that a swallowed exception is causing the problem you have seen. If you want to ignore an error, log the error to a file in the catch block so if something goes wrong you can look back to see what problems you ignored.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900