¿Cómo el valor de una matriz se vuelve nulo en Java si se usa dentro del alcance? Le estoy proporcionando el código Java…

programación


El resto del código que no se menciona aquí es correcto…

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

Lo que he probado:

No puedo entender por qué Unidad[n][i] se vuelve nulo justo después de mostrar el primer valor, cuando los registros están presentes en el archivo

Solución 1

Como ha dicho Richard, dependerá de los datos procesados ​​y no tenemos acceso a su sistema de archivos; el depurador es (como siempre) su mejor amigo aquí, ya que puede mostrarle exactamente qué sucede mientras se ejecuta su programa.
Entonces, dependerá de ti.

Afortunadamente, tienes una herramienta a tu disposición que te ayudará a descubrir qué está pasando: el depurador. La forma en que lo use depende de su sistema compilador, pero una búsqueda rápida en Google para buscar el nombre de su IDE y “depurador” debería brindarle la información que necesita.

Coloque un punto de interrupción en la primera línea de la función y ejecute su código a través del depurador. Luego mire su código y sus datos y determine qué debería suceder manualmente. Luego, realice un solo paso en cada línea para verificar que lo que esperaba que sucediera sea exactamente lo que sucedió. Cuando no es así, es cuando tienes un problema y puedes retroceder (o ejecutarlo nuevamente y observar más de cerca) para descubrir por qué.

Lo sentimos, pero no podemos hacer eso por usted. Es hora de que aprenda una habilidad nueva (y muy, muy útil): ¡depurar!

Pero hazte dos grandes favores:
1) Deje de usar nombres de variables realmente cortos: use nombres significativos en lugar de “a”, “p”, “jj”, “y”, “c”, etc. y su código se volverá mucho más legible, pero también más confiable. ya que es más obvio cuando usas la variable incorrecta por error…

2) Deja de tragar excepciones.

Java
catch(IOException e)
{

}

Es posible que un error “desaparezca”, pero es muy posible que una excepción ingerida esté causando el problema que ha visto. Si desea ignorar un error, registre el error en un archivo en el catch bloquee para que, si algo sale mal, pueda mirar hacia atrás para ver qué problemas ignoró.

コメント

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