我在节目中的错误是
java.lang.ArrayIndexOutOfBoundsException
at knapsacproject.Interface.jButton4ActionPerformed(Interface.java:221)
此代码用于按钮,它将结果打印在 jTextField4 中,但没有任何内容写入
我在java中的代码:
DefaultTableModel model = (DefaultTableModel)jTable1.getModel();
int nRow = model.getRowCount();
try{
algorithm algo = new algorithm(getTableData(model, 1),getTableData(model, 2),
Integer.parseInt(jTextField1.getText)),
Integer.parseInt(jTextField2.getText)),Integer.parseInt(jTextField3.getText()));
int[] result = algo.getResult();
for(int i = 0; i <nRow;i++) {
if(result[i]==1)
model.setValueAt("take", i , 3);
else
model.setValueAt("leave", i, 3);
}
jTable1.getColumnModel().getColumn(3).setCellRenderer(new StatusColumnCellRenderer());
jTextField4.setText(Integer.toString(result[nRow]));
和eror一致:
jTextField4.setText(Integer.toString(result[nRow]));
有什么问题?
答案 0 :(得分:2)
def facebook
user = User.from_omniauth(env["omniauth.auth"])
# preexisting_user = User.find(params[:id]) # What should go in this line?
# user.persisted? is what you need to add if you want to add here
# if user pre existed, it will execute if block otherwise else block.
# checks if pre-existing user
if user.persisted?
cookies.permanent.signed[:user_id] = user.id
redirect_to root_url
flash.now[:info] = 'Welcome Back to Live to Challenge!'
else
action = session.delete(:challenge_action)
user.challenges.create(action: action)
user.send_welcome_email
user.remember
cookies.permanent.signed[:user_id] = user.id
redirect_to tutorial_url
flash.now[:info] = 'Welcome to Live to Challenge!'
end
end
数组大小为result
,但您尝试访问索引为nRow
的元素,而您不能这样做,因为java中的数组起始索引为nRow
。我猜您需要将问题行更改为:
0
答案 1 :(得分:1)
尝试使用result[nRow-1]
代替result[nRow]
,如下所示:
jTextField4.setText(Integer.toString(result[nRow-1]));
因为数组的索引从0
开始到nRow-1
而不是nRow
。