J表。打印选中行的值,如果选中行为空则提示错误
JTable. Printing values of selected row and prompt an error if the selected row is null
我创建了一个包含 3 列的 JTable,并在前两行中插入了一个值。当我 select 第一行或第二行并单击按钮 "GetData" 时, selected 行中的数据将打印在控制台上,但是!如果我 select 行中没有数据,则在这行代码 dataString = dataObject.toString();
上会出现 NullPointerException。
我插入了 if else 语句来检查 dataString 或 dataObject 是否持有 空值或“”值
import java.awt.FlowLayout;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JayTable extends JFrame
{
DefaultTableModel model;
JTable table;
String col[] = {"Name","Address","Gender"};
public static void main(String args[])
{
new JayTable().start();
}
public void start()
{
model = new DefaultTableModel(col,50);
table=new JTable(model)
{
@Override public boolean isCellEditable(int arg0, int arg1)
{
return false;
}
};
table.getColumnSelectionAllowed();
JScrollPane pane = new JScrollPane(table);
JButton button = new JButton("GetData");
table.setValueAt("Ritchie Rich",0,0);
table.setValueAt("The Mansion",0,1);
table.setValueAt("Male",0,2);
table.setValueAt("Flintstone", 1, 0);
table.setValueAt("Rocky Road", 1, 1);
table.setValueAt("Unknown", 1, 2);
add(pane);
add(button);
button.setEnabled(false);
ListSelectionModel listSelectionModel = table.getSelectionModel();
listSelectionModel.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
button.setEnabled(!lsm.isSelectionEmpty());
}
});
button.addActionListener(e ->
{
int row = table.getSelectedRow();
int column;
Object dataObject = null;
String dataString = "";
for(column = 0 ; column < table.getColumnCount(); column++)
{
dataObject = table.getValueAt(row, column);
dataString = dataObject.toString();
if(dataObject == null || dataObject.equals("") || dataString ==null || dataString.equals("") )
System.out.println("No value");
else
System.out.print(dataString+", ");
}
System.out.println();
});
setSize(500,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
}
您的空检查为时已晚。改成
dataObject = table.getValueAt(row, column);
if(dataObject == null || "".equals(dataObject)) {
System.out.println("No value");
} else {
dataString = dataObject.toString();
if(dataString ==null || "".equals(dataString) )
System.out.println("No value");
else
System.out.print(dataString+", ");
}
我创建了一个包含 3 列的 JTable,并在前两行中插入了一个值。当我 select 第一行或第二行并单击按钮 "GetData" 时, selected 行中的数据将打印在控制台上,但是!如果我 select 行中没有数据,则在这行代码 dataString = dataObject.toString();
上会出现 NullPointerException。
我插入了 if else 语句来检查 dataString 或 dataObject 是否持有 空值或“”值
import java.awt.FlowLayout;
import java.util.Scanner;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class JayTable extends JFrame
{
DefaultTableModel model;
JTable table;
String col[] = {"Name","Address","Gender"};
public static void main(String args[])
{
new JayTable().start();
}
public void start()
{
model = new DefaultTableModel(col,50);
table=new JTable(model)
{
@Override public boolean isCellEditable(int arg0, int arg1)
{
return false;
}
};
table.getColumnSelectionAllowed();
JScrollPane pane = new JScrollPane(table);
JButton button = new JButton("GetData");
table.setValueAt("Ritchie Rich",0,0);
table.setValueAt("The Mansion",0,1);
table.setValueAt("Male",0,2);
table.setValueAt("Flintstone", 1, 0);
table.setValueAt("Rocky Road", 1, 1);
table.setValueAt("Unknown", 1, 2);
add(pane);
add(button);
button.setEnabled(false);
ListSelectionModel listSelectionModel = table.getSelectionModel();
listSelectionModel.addListSelectionListener(new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent e)
{
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
button.setEnabled(!lsm.isSelectionEmpty());
}
});
button.addActionListener(e ->
{
int row = table.getSelectedRow();
int column;
Object dataObject = null;
String dataString = "";
for(column = 0 ; column < table.getColumnCount(); column++)
{
dataObject = table.getValueAt(row, column);
dataString = dataObject.toString();
if(dataObject == null || dataObject.equals("") || dataString ==null || dataString.equals("") )
System.out.println("No value");
else
System.out.print(dataString+", ");
}
System.out.println();
});
setSize(500,400);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
pack();
setVisible(true);
}
}
您的空检查为时已晚。改成
dataObject = table.getValueAt(row, column);
if(dataObject == null || "".equals(dataObject)) {
System.out.println("No value");
} else {
dataString = dataObject.toString();
if(dataString ==null || "".equals(dataString) )
System.out.println("No value");
else
System.out.print(dataString+", ");
}