java - Trying to read from a file. Throws an exception -


im trying read file containing single vertical file of numbers, , filling matrix (that composed jtextfields) them, when try set method, program throws exception after changing second row

for(int a=0; < i; a++) {      for(int b=0; b < i; b++){         // x = raf.readline();         matrix[a][b].settext(raf.readline());     } } 

you should not read file gui class. try separating concerns creating dedicated class reading text file. perhaps let file reader class return iterator<string> of lines. test file reader class in unit test , make sure displays lines correctly.

then this:

iterator<string> lines = yourhelperclass.getlines() for(int a=0; < i; a++) {      for(int b=0; b < i; b++){         if(!lines.hasnext()){             // not enough lines, throw exception here         }         matrix[a][b].settext(lines.next());     } } 

that way lot easier find out going wrong.

recommended read: coupling , cohesion: 2 cornerstones of oo programming


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -