以下のコードを実行すると、次のエラーが表示されます。
java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction
エラー全体:
run:
java.lang.ClassCastException: java.lang.String cannot be cast to Panels.AddNewClientSaveAction
Before
at Database.FileUpdate.main(128 | Oli | Much
FileUpdate.java:40)
128 | Oli | Much
128 | Francis Kariuki | Mahia
128 | Francis Kariuki | Mahia
LASTSTUDENTEENTRYLINENNAMES
BUILD SUCCESSFUL (total time: 4 seconds)
この場合、特定のテキスト「LASTSTUDENTEENTRYLINENNAMES」の前にテキストファイルに新しいユーザーを追加する機能を作成しようとしています。
これは、新しいユーザー「Eddys Rockery」を追加する前後の例です。
前:
123 | Oliver | Muchai
456 | Revilo | Chamu
LASTSTUDENTEENTRYLINENNAMES
後:
123 | Oliver | Muchai
456 | Revilo | Chamu
678 | Eddys | Rockery
LASTSTUDENTEENTRYLINENNAMES
これまでのコード。エラーがどこから生成されていると思われるかを示しました。
ヘルプや提案をお寄せいただきありがとうございます。
import Panels.AddNewClientSaveAction;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Stack {
public static final String LAST_STUDENT_LINE = "LASTSTUDENTEENTRYLINENNAMES";
public static StringBuilder line;
public static void main(String[] args) {
StringBuilder sb = new StringBuilder(128);
List<AddNewClientSaveAction> objectInputFieldsList = new ArrayList<>(25);
AddNewClientSaveAction values = new AddNewClientSaveAction();
objectInputFieldsList.addAll(values.rayArrayList());
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/D:/TestFile.DAT/"));
String text = null;
while ((text = br.readLine()) != null) {
if (sb.length() > 0) {
sb.append("\n");
}
sb.append(text);
}
System.out.println("Before");
System.out.println(sb);
// The Error's here: for (AddNewClientSaveAction s : objectInputFieldsList)
for (AddNewClientSaveAction s : objectInputFieldsList) {
int insertIndex = sb.indexOf(LAST_STUDENT_LINE);
line = new StringBuilder(128);
line.append(s.objectGUID).append(" | ").append(s.userGUID).append("\n");
sb.insert(insertIndex, line.toString());
}
System.out.println("\nAfter");
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException exp) {
}
}
try{
java.io.FileWriter fstream = new java.io.FileWriter("/D:/TestFile.DAT/");
BufferedWriter outobj = new BufferedWriter(fstream);
outobj.write(sb.toString() + "\n");
outobj.close();
}catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
別のクラス:
import Panels.AddNewClient;
import java.util.ArrayList;
public class StackAddClientSaveAction {
final public ArrayList objectInputFieldsList = new ArrayList();
public String objectGUID;
public String userGUID;
public String firmGUID;
public String postalCode;
public ArrayList rayArrayList () {
StackAddClientSaveAction addNewClientSaveAction = new StackAddClientSaveAction();
return addNewClientSaveAction.actionPerformed();
}
public ArrayList actionPerformed ()
{
// AddNewClient Class prints out the GUI where postalCode is entered via JTextField
AddNewClient addNewClient = new AddNewClient();
objectGUID = "1452";
userGUID = "90378";
firmGUID = "3663287";
postalCode = addNewClient.postalCodeJTextField.getText();
// Add to list
objectInputFieldsList.add(objectGUID);
objectInputFieldsList.add(userGUID);
objectInputFieldsList.add(firmGUID);
objectInputFieldsList.add(postalCode);
return objectInputFieldsList;
}
}