java - Sorting algorithm with run error -


hey wanted excercise in java, reading .txt files filled numbers, , creating 1 numbres max min.

in order made classes. code:

import java.io.*; import java.util.linkedlist;  public class texto {      string filename;      public texto(string nombrearchivo) {         this.filename = nombrearchivo;                       }      public linkedlist storearray() {         linkedlist list = new linkedlist();         int a=0;         try {             filereader file = new filereader(filename);             bufferedreader buffer = new bufferedreader(file);             string temp="";             while (temp!=null) {                 list.add(buffer.readline());                 temp=list.get(a).tostring(); // line 25                 a++;                             }             list.remove(a);         } catch (ioexception e) {             e.getmessage();         }         return list;     }      public int getlength() {         return storearray().size(); // line 41     }      public void orderfile() {         try {             filewriter file = new filewriter("archivoordenado.txt");             bufferedwriter buffer = new bufferedwriter(file);             printwriter print = new printwriter(buffer);             int array[] = new int[getlength()]; // line 52              (int i=0;i<getlength();i++) {                 array[i]=(integer)storearray().get(i);             }              int temp;              (int i=0;i<getlength();i++) {                 (int j=1;i<getlength();j++) {                     if (array[i]<array[j]) {                         temp=array[i];                         array[i]=array[j];                         array[j]=temp;                     }                 }             }              (int i=0;i<getlength();i++) {                 print.println(array[i]);             }             print.close();             buffer.close();          } catch (ioexception e) {             e.getmessage();         }      } } 

and in class y call methods this:

public class main {     public static void main(string[] args) {         texto t = new texto("numeros.txt");         t.orderfile();     } } 

the run error says:

exception in thread "main" java.lang.nullpointerexception     @ texto.storearray(texto.java:25)     @ texto.getlength(texto.java:41)     @ texto.orderfile(texto.java:52)     @ main.main(main.java:6) 

this line 25 : temp=list.get(a).tostring();
41: return storearray().size();
52: int array[] = new int[getlength()];

follow comments:

    string temp="";     while (temp!=null)     {         list.add(buffer.readline()); // buffer.readline() return null @ eof                                      // isn't tested here         temp=list.get(a).tostring(); // @ eof .tostring() null - npe.         a++;                     } 

rewrite this

    string temp="";     while (temp!=null) {         temp=buffer.readline();         if (temp != null) {             list.add(temp);         }     } 

then list.remove(a); after end of loop can dropped, it's there correct fact ended null.

edit: version 1 comparison, bit more compact maybe bit less readable

    string temp="";     while ((temp = buffer.readline()) != null) {          list.add(temp);     }  

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -