如果在作用域内使用数组,那么在 java 中数组的值如何变为 null? 我正在提供它的java代码……

编程


这里没有提到的其余代码是正确的……

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)
                                {
                                    
                                }
                            }
                        }
}

我尝试过的:

我不明白为什么单位[n][i] 当文件中存在记录时,显示第一个值后立即变为 null

解决方案1

正如理查德所说,这将取决于处理的数据,并且我们无法访问您的文件系统 – 调试器(一如既往)是您最好的朋友,因为它可以准确地向您显示程序运行时发生的情况。
所以,这将取决于你。

幸运的是,您有一个可用的工具可以帮助您了解正在发生的情况:调试器。 如何使用它取决于您的编译器系统,但是快速 Google 一下您的 IDE 名称和“调试器”应该会为您提供所需的信息。

在函数的第一行放置一个断点,然后通过调试器运行代码。 然后查看您的代码和数据并找出应该手动发生的情况。 然后单步执行每一行,检查您期望发生的情况是否确实发生。 如果不是,那就是你遇到了问题,你可以回溯(或再次运行它并更仔细地查看)以找出原因。

抱歉,我们无法为您做到这一点 – 您是时候学习一项新的(并且非常非常有用)技能了:调试!

但请帮自己两个大忙:
1)停止使用非常短的变量名称:使用有意义的名称而不是“a”、“p”、“jj”、“y”、“c”等,这样您的代码就会变得更具可读性,而且也更可靠因为当您错误地使用错误的变量时,情况会更加明显……

2)停止吞噬异常。

爪哇
catch(IOException e)
{

}

可能会导致错误“消失”,但很可能是吞没的异常导致了您所看到的问题。 如果您想忽略错误,请将错误记录到以下文件中: catch 阻止,这样如果出现问题,您可以回头看看您忽略了哪些问题。

コメント

タイトルとURLをコピーしました