gpt4 book ai didi

Java boolean 值转换为 Object[][]

转载 作者:行者123 更新时间:2023-12-01 08:10:24 25 4
gpt4 key购买 nike

我得到以下代码,但出现此错误(我试图使代码尽可能短,忽略 getColumnCount 等函数,仅使用构造函数):

以下代码用于通过 SQLite 语句在 Swing 中创建 JTable,我需要复选框的 boolean 值(是的,我知道我必须编辑/添加一个函数,但我想让代码尽可能小) .

代码:

package view;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.table.AbstractTableModel;

import controller.Database;

class Test extends AbstractTableModel {
Database db = new Database();
ResultSet rs;

private String[] columnNames = {"Vorname", "Nachname", "E-Mail", "Anrede", "Jahrgang", "Ablösung", "Scheibe", "Waffe", "Gruppe", "Verpflegung", "Aktiv"};
Object[][] data;

public Test(){
int result = 0;
try {
rs = db.stat.executeQuery("select count(*) as schuetzencount from schuetze;");
result = Integer.parseInt(rs.getString("schuetzencount"));
data = new String[result][11];
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
rs = db.stat.executeQuery("select * from schuetze as s join waffe as w on w.Waffe_ID = s.Waffe_ID join gruppe as g on g.Gruppe_ID = s.Gruppe_ID join anrede as a on a.Anrede_ID = s.Anrede_ID join verpflegung as v on v.Verpflegung_ID = s.Verpflegung_ID;");
int counter = 0;
while(rs.next()){

data[counter][1] = rs.getString("Schuetze_Nachname");
data[counter][0] = rs.getString("Schuetze_Vorname");
data[counter][4] = rs.getString("Schuetze_Jahrgang");
data[counter][2] = rs.getString("Schuetze_Email");
data[counter][5] = rs.getString("Schuetze_Abloesung");
data[counter][6] = rs.getString("Schuetze_Scheibe");
data[counter][7] = rs.getString("Waffe_Name");
data[counter][8] = rs.getString("Gruppe_Name");
data[counter][3] = rs.getString("Anrede_Name");
data[counter][9] = rs.getString("Verpflegung_Name");
data[counter][10] = true;
counter++;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public int getColumnCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getRowCount() {
// TODO Auto-generated method stub
return 0;
}

@Override
public Object getValueAt(int arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}

public static void main(String[] args) {
Test t = new Test();
}
}

错误:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Boolean
at view.Test.<init>(Test.java:43)
at view.Test.main(Test.java:72)

如果我愿意

Object[][] data = {{"Test", "Test","Test","Test","Test","Test","Test","Test","Test","Test",true}}

它有效,但这不是我需要的。然后我尝试执行 Object[] 并填充 boolean 值,然后将 Object[] 添加到 data[][] 中,但是这也不起作用。

希望有人能帮助我,谢谢。

再见。

最佳答案

您有字符串数组,并尝试将 boolean 放入其中

data[counter][10] = true;

这是不允许的。

当你这样做

Object[][] data = {{"Test", "Test","Test","Test","Test","Test","Test","Test","Test","Test",true}}

Java 为您创建对象数组喜欢:

Object[][] o = new Object[1][6];
o[0][2] = true; // it works

关于Java boolean 值转换为 Object[][],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17946195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com