我想添加新的顶点和边缘,并使用JGraph Library创建图形。我总是得到这个 java.lang.NullPointer 异常。我创建了一个类函数createvertex来创建新的单元格/顶点,并绘制边缘连接到端口。但是,即使我没有将端口声明为null,端口也始终显示为null。以下是我的代码。我的代码有什么问题吗?
public class HelloWorld {
public static void main(String[] args) {
// Construct Model and Graph
GraphModel model = new DefaultGraphModel();
GraphLayoutCache view= new GraphLayoutCache(model,new DefaultCellViewFactory());
JGraph graph = new JGraph(model,view);
// Control-drag should clone selection
graph.setCloneable(true);
// Enable edit without final RETURN keystroke
graph.setInvokesStopCellEditing(true);
// When over a cell, jump to its default port (we only have one, anyway)
graph.setJumpToDefaultPort(true);
// Insert all three cells in one call, so we need an array to store them
DefaultGraphCell[] cells = new DefaultGraphCell[5];
DefaultPort[] port = new DefaultPort[4];
// Create Hello Vertex
cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLACK, true);
port[0].setParent(cells[0]);
// Create World Vertex
cells[1] = createVertex("World", 140, 140, 40, 20, Color.ORANGE, true);
cells[1].add(port[1]);
cells[1].add(port[2]);
port[1].setParent(cells[1]);
port[2].setParent(cells[1]);
cells[3]= createVertex("Optical Cards",150,150,20,40,Color.GREEN, true);
cells[3].add(port[3]);
port[3].setParent(cells[3]);
// Create Edge
DefaultEdge[] edge = new DefaultEdge[2];
// Fetch the ports from the new vertices, and connect them with the edge
edge[0].setSource(cells[0].getChildAt(0));
edge[0].setTarget(cells[1].getChildAt(0));
cells[2] = edge[0];
edge[1].setSource(cells[1].getChildAt(1));
edge[1].setTarget(cells[3].getChildAt(0));
cells[4]=edge[1];
// Set Arrow Style for edge
int arrow = GraphConstants.ARROW_CLASSIC;
GraphConstants.setLineEnd(edge[0].getAttributes(), arrow);
GraphConstants.setEndFill(edge[0].getAttributes(), true);
GraphConstants.setLineEnd(edge[1].getAttributes(), arrow);
GraphConstants.setEndFill(edge[1].getAttributes(), true);
// Insert the cells via the cache, so they get selected
graph.getGraphLayoutCache().insert(cells);
// Show in Frame
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static DefaultGraphCell createVertex(String name, double x,double y,double w,double h, Color bg, boolean raised) {
// Create vertex with the given name
DefaultGraphCell cell = new DefaultGraphCell(name);
// Set bounds
GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(
x, y, w, h));
// Set fill color
GraphConstants.setGradientColor(cell.getAttributes(), Color.orange);
GraphConstants.setOpaque(cell.getAttributes(), true);
// Set raised border
if (raised)
GraphConstants.setBorder(cell.getAttributes(), BorderFactory
.createRaisedBevelBorder());
else
// Set black border
GraphConstants.setBorderColor(cell.getAttributes(), Color.black);
// Add a Port
DefaultPort port = new DefaultPort();
cell.add(port);
return cell;
}
}
答案 0 :(得分:0)
DefaultPort[] port = new DefaultPort[4];
// Create Hello Vertex
cells[0] = createVertex("Hello", 20, 20, 40, 20, Color.BLACK, true);
port[0].setParent(cells[0]); // <---------- port[0] is null here, so you
// cannot call setParent()
仅仅因为您初始化了端口数组并不意味着您可以简单地使用数组中的第一个DefaultPort
元素。在尝试取消引用数组中的项之前,需要使用有效的非空DefaultPort
对象填充数组。