java void 不能被取消引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10763517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
void cannot be dereferenced
提问by Nathan Kreider
I have a method of editing members, and I want to print out errors into a file, but I keep getting the void cannot be dereferenced error if I try to print out the stack trace into a Error_Report.txt file. Is there anyway I can print it out? This is my code.
我有一种编辑成员的方法,我想将错误打印到文件中,但是如果我尝试将堆栈跟踪打印到 Error_Report.txt 文件中,我会不断收到 void 无法取消引用错误。反正我可以打印出来吗?这是我的代码。
public void edit() {
FileWriter fw = new FileWriter(new File("Error_Report.txt"));
Connection con;
Statement stmt;
ResultSet rs;
int id = (int)_id.getSelectedItem();
String name = _name.getText();
String user = _username.getText();
String pass = _password.getText();
String pos = _position.getSelectedItem().toString();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:collegesys",
"root", "0blivi0n");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
PreparedStatement prep = con.prepareStatement("UPDATE `main` WHERE ID = ?");
prep.setInt(1, id);
prep.setString(2, name);
prep.setString(3, user);
prep.setString(4, pass);
prep.setString(5, pos);
prep.execute();
} catch(SQLException sqle) {
String sql = sqle.printStackTrace().toString();
fw.write("" + sql);
} catch(ClassNotFoundException cnfe) {
fw.write("" + cnfe);
}
}
回答by Dawood ibn Kareem
Your problem is that printStackTrace
doesn't return anything, so there's nothing to convert to a string. Write it like this.
您的问题是printStackTrace
不返回任何内容,因此没有任何内容可以转换为字符串。像这样写。
PrintWriter writer = new PrintWriter(fw);
sqle.printStackTrace(writer);
writer.close();
回答by Luiggi Mendoza
sqle.printStackTrace()
returns a void and can't be used as parameter. Change your code to something like this:
sqle.printStackTrace()
返回一个 void 并且不能用作参数。将您的代码更改为如下所示:
catch(SQLException sqle) {
StringBuilder sb = new StringBuilder();
StackTraceElement[] st = sqle.getStackTrace();
for(StackTraceElement s : st) {
sb.append(s);
sb.append('\n');
}
fw.write(sb.toString());
}