(mainメソッドを呼び出すクラスが)例外7
import java.io.*; public class bob { public static void main(String[] args){ new bert(); new larry();
public class larry{ bert bertObj = new bert(); public larry(){ try{ File file = new File("text.txt"); if(!file.exists()) file.createNewFile(); PrintWriter pw = new PrintWriter(file); for(String x: bertObj.string){ pw.println(x); } pw.close(); }catch(IOException e){ e.printStackTrace(); } } }
は(読み込み(それへのファイルとcopys文字列を作成します)ファイルをコピーして文字列配列にコピーします) (文字列を初期化していないので、エラーではないかと思いますが、その中にいくつの値が格納されているかはわかりません)String string [] = new例えば、String [100] ... else else私が行う)
クラスBERTでpublic class bert { String string[]; public bert(){ BufferedReader br = null; try{ br = new BufferedReader(new FileReader("C:\\Program Files\\Java\\jdk1.8.0_101\\THIRDPARTYLICENSEREADME.txt")); int counter = 0; while((string[counter] = br.readLine()) != null) { counter++; } for(int x = counter; x < 4000; x++) string[x] = ""; }catch(IOException e){ e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Q
例外7
-1
A
答えて
-1
、あなたの文字列を[]に初期化されることはありませんあなたはこの行でそれにアクセスしようとする前に:?
while((string[counter] = br.readLine()) != null)
ちょうどあなたのようなラリークラスで初期化BERT bertクラスのString []を初期化する必要があります。
電源を入れ、この行:
String [] string;
の中へは:
String[] string = new String[5]; // 5 is an arbitrary number I chose, you need to choose how many strings you will be holding in that array
申し訳ありませんが、完全にあなたのコメントを読んでいません。静的な配列の代わりに、arraylistを使ってみてください。これは配列と同じようには動作しません。ここでArrayListの上
ArrayList<String> string = new ArrayList();
string.add("hello"); // string now has a string "hello"
string.add("world"); // string now has a string "hello" followed by a string "world"
for(int i = 0; i < string.size(); i++) {
System.out.println(string.get(i)); // gets position i from the array and prints it. Will print hello then a new line then world.
}
より:
あなたに簡単な例を挙げれば
+0
ありがとう!非常に役に立ちます:) –
+0
問題はありません、私は人々が重複としてマークするとき、私は嫌い、ちょうど質問に答える、これらは時々非常に具体的です! – Cmoraski
関連する問題
- 1. WebSphere 7セッションのレプリケーション例外
- 2. Windows 7でのMEF例外
- 3. Log4j2とJboss EAP 7:例外ログ記録
- 4. Drupalの7、取扱いヌル例外
- 5. デルファイ7のWindows Vista/7のファイアウォール例外のネットワークロケーション
- 6. TransactionScope例外例外
- 7. インタフェース例外例外
- 8. Facebookアカウントキット - SDK 7日以上経過した場合の例外
- 9. Intel Parallel Debugger Extension(libiomp5md)/ IPP 7とVS2005のファーストチャンス例外
- 10. WPF Dispatcher.RunがWin 7でnull例外をスローする64
- 11. メモ帳++ HTML Tidyは不明な例外のWindows 7のx64
- 12. のSymfony \コンポーネント\プロセス\例外の\のRuntimeException信号 "7"
- 13. WAS 7:Writerは既に例外を取得しています
- 14. JWPlayer 7 SDK /コード例
- 15. Java例外とC++例外
- 16. Cocos2d-Xメニューコールバック例外例外
- 17. PHP PDO例外と例外strangeness
- 18. F#の例外と内部例外プロパティ
- 19. Mockito :: MockMVCのMockMVC例外のヌルポインタ例外
- 20. 春休み例外の例外
- 21. Pythonの:KeyError例外/例外IOError urllib.urlopen
- 22. =例外をスローする例外
- 23. 例外のあるPython例外
- 24. Javaの例外処理 - カスタム例外
- 25. フラグメントクラス内のインフレータ例外の例外
- 26. アプリケーション例外とシステム例外の違い
- 27. 引数例外の例外ASP.Net
- 28. Javaクラスの例外例外のジェネリック
- 29. 未処理の例外 'System.Data.SqlClient.SqlExceptionの例外
- 30. 例外と例外のスロー。<T>?
をこれに対する最も簡単な解決策は、配列を取り除くと、ArrayListのか、文字列を使用して取得することです。私はあなたの考えを知りません。(int x = counter; x <4000; x ++) string [x] = ""; 'doは実行します –