Read Jlist files and display content in a Jtextarea

Asked

Viewed 100 times

2

JTextArea caixaTexto = new JTextArea();

 try {
    String[] arrayLinhas = null;        
    int i = 0;                          

    BufferedReader br = new BufferedReader(new FileReader(diretoriaExecucao + "/" + valorSelecionado));

    while(br.ready()){
        String linha = br.readLine();
        arrayLinhas[i] = linha;
        i++;
    }
    caixaTexto.setText(arrayLinhas.toString());  //imprimo caixa texto
    br.close();                         

} catch (IOException e2) {
    e2.printStackTrace();
}

I have a list from which I select a file, read that file and print the output in the Jtextarea text box.

Gives me an error of null Exception on line:

arrayLines[i] = line;

2 answers

1


I adapted your code for it to work. You don’t need an array if the only goal is to show the content in a text area.

try {
    caixaTexto.setText(""); // Limpar o TextArea
    BufferedReader br = new BufferedReader(new FileReader(diretoriaExecucao + "/" + valorSelecionado));

    while(br.ready()){
        String texto = br.readLine() + "\n"; 
        caixaTexto.append(texto); 
    }
    
    br.close();                         
} catch (IOException e2) {
    e2.printStackTrace();
}

Old version

You did not prompt the array, see this line

String[] arrayLinhas = null;

Should be

String[] arrayLinhas = new String[tamanhoDoArray];

If you don’t know the number of lines beforehand, you can replace the array with a ArrayList

ArrayList<String> listaLinhas = new ArrayList<String>();

//...
while(br.ready()){
    String linha = br.readLine();
    listaLinhas.add(linha);
}
  • Yes but now I have the problem when I write in the text box with the first solution does not show me the text but encoded.

  • That’s another problem, young man. You can open another question with this.

  • may be List instead of List array?

  • 1

    On the left side (declaration) yes, on the right side (instantiation).

  • In the case of append no, this is because every time you click on other files, the text that is already there always appears.

  • Clean it up first, pray. I edited the answer

  • and how do I change line? /n does not work

  • and vote on my post pff to mark you correct answer

  • It’s all right, man.

Show 4 more comments

1

Nullpointerexception occurs because arrayLinhas is not initialized.

My dirty is replacing it with a List, like the ArrayList and use the add method for the lines. Then you can take an array of lines if you need one list.toArray()

Another solution for your case is to use the Jtextarea append directly:

JTextArea caixaTexto = new JTextArea();

try {
     BufferedReader br = new BufferedReader(new FileReader(diretoriaExecucao, valorSelecionado));

     while(br.ready()){
         String linha = br.readLine();
         caixaTexto.append(linha);//imprime continuamente na caixa texto
     }
 br.close();                         

 } catch (IOException e2) {
     e2.printStackTrace();
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.