JTabel: setting a new value; setValueAt; NullPointerException
Hi,
I had already a similar problem, this time I have to ask this forum again:
I have a GUI with some JPanels laid out using BorderLayout. One of these JPanels contains my JTable; this JPanel is in a separate class.
In another class some methods calculate speed, angle, distance etc... values I need for my program.
I just want to enter these calculated values in my JTable so that the user can see them. So, the JTable itself should just show the values. I don't want that the user can change the values or that the table respond to events.
Here is my class "InfoPanel" which contains the JTable:
publicclass InfoPanelextends JComponent{
privatestaticfinallong serialVersionUID = 1;
public JTable table;
String[] columnNames ={"Type","Value"};
Object[][] rowData ={
"Angle","0"},
};
public InfoPanel(){
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
Dimension dim_wPanel =new Dimension(300, 150);
JPanel wPanel =new JPanel();
wPanel.setPreferredSize(dim_wPanel);
table =new JTable(rowData, columnNames);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel tablePanel =new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(table.getTableHeader(), BorderLayout.PAGE_START);
tablePanel.add(table, BorderLayout.CENTER);
add(windDirectionPanel);
add(tablePanel);
}
}
...and here is my class calculating some values and using the setValueAt method (just including the necessary code):
publicclass PlayerMgmtextends JComponent{
privatestaticfinallong serialVersionUID = 1;
InfoPanel ip;
private List <Ellipse2D>playerList =new ArrayList<Ellipse2D>();
public PlayerMgmt(){
(...)
}
protectedvoid paintComponent(Graphics g){
(...)
if (pList.size() > 1){
ip.table.getModel().setValueAt(calcAngle(player, playerLast), 0, 1);
}
}
(...)
private String calcAngle(Ellipse2D p, Ellipse2D lp){
double radians = (Math.atan2(lp.getY() - p.getY(), lp.getX() - p.getX()) * 180.0) / Math.PI;
return formatter.format(radians);
}
}
The setValueAt method results in a NullPointerException:
Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException
What am I doing wrong?
Thanks for your help!
[4116 byte] By [
SFLa] at [2007-11-26 16:48:51]

# 3
Oh, here is the full code of the class PlayerMgmt:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
public class PlayerMgmt extends JComponent {
private static final long serialVersionUID = 1;
private BShapes cb = new BShapes();
private InfoPanel ip;
private List <Ellipse2D>playerList = new ArrayList<Ellipse2D>();
private Ellipse2D player, playerLast;
private Line2D connectingLine;
private NumberFormat formatter = new DecimalFormat("#,###,###");
public PlayerMgmt () {
addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evt) {
if (SwingUtilities.isLeftMouseButton(evt)) {
playerList.add(new Ellipse2D.Double(evt.getX(), evt.getY(), 20, 20));
}
if (SwingUtilities.isRightMouseButton(evt)) {
if (playerList.size() > 0) playerList.remove(playerList.size() - 1);
}
repaint();
}
});
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Rectangle drawHere = g.getClipBounds();
g2.setColor(Color.WHITE);
g2.fillRect(drawHere.x, drawHere.y, drawHere.width, drawHere.height);
g2.setColor(Color.BLACK);
g2.draw(cb.drawIsland());
for(int i=1 ; i < playerList.size(); i++) {
player = playerList.get(i);
playerLast = playerList.get(i-1);
connectingLine = new Line2D.Double(playerLast.getX(), playerLast.getY(), player.getX(), player.getY());
if (isCollision(player, connectingLine)) g2.setColor(Color.RED);
else g2.setColor(Color.BLACK);
g2.draw(player);
g2.draw(connectingLine);
}
// setValueAt
if (playerList.size() > 1) {
ip.table.getModel().setValueAt(calcAngle(player, playerLast), 0, 1);
}
}
private boolean isCollision(Ellipse2D p, Line2D l) {
if (cb.drawRoundedIsland().getBounds().contains(p.getBounds()) || (cb.drawRoundedIsland().getBounds().intersects(l.getBounds()))) return true;
else return false;
}
private String calcAngle(Ellipse2D p, Ellipse2D lp) {
double radians = (Math.atan2(lp.getY() - p.getY(), lp.getX() - p.getX()) * 180.0) / Math.PI;
return formatter.format(radians);
}
}
...and the exception:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PlayerMgmt.paintComponent(PlayerMgmt.java:72)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Thanks for your help!
SFLa at 2007-7-8 23:16:24 >

# 7
I corrected my code but I get a NullPointerException again (caused by setValueAt):
In my main-class I call PlayerMgmt: pmPanel = new PlayerMgmt(ip);
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PlayerMgmt.paintComponent(PlayerMgmt.java:73)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
SFLa at 2007-7-8 23:16:24 >
