图中的NullPointerException和MouseListener

时间:2013-10-07 13:20:24

标签: java jena mouselistener jgrapht jgraphx

我正在尝试使用此代码来更改连接到在jgraphx中单击的顶点的某些顶点的不透明度,当您单击顶点之外的某个位置时,它将重新更改不透明度。单击顶点时,将查询其值/字符串,并将所有连接到它的对象添加到新对象列表中。然后我处理这个对象列表来选择要突出显示的内容,而不是什么。但是我一直在收到错误,无法解决问题。据我检查,我没有任何null对象。如果有人可以提供帮助,我们将非常感激。

错误指的是:

graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);

以下是代码:

    public void MassCellHighlight() {

            int OPACITY_PALE = 15;
            int OPACITY_HL = 100;

            graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {

                public void mouseReleased (MouseEvent e1) {
                    if (e1.getButton() == 1 && e1.getClickCount() == 1) {

                    long x = e1.getX();
                    long y = e1.getY();
                    Object cell = graphComponent.getCellAt((int) x, (int)y); 

                    String usecell = graph.convertValueToString(cell);

                    PropertyConfigurator.configure("\\jena-log4j.properties");
                    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
                    FileManager.get().readModel( model, "file" );


                    String queryString =
                            "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +        
                            "PREFIX owl: <http://www.w3.org/2002/07/owl#> " +
                            "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
                            "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
                            "PREFIX bi: <#> " +

                             " SELECT ?Path " +
                             " WHERE { bi:"+usecell+" bi:can_lead_to ?Path } " ;


                             Query query = QueryFactory.create(queryString);
                             QueryExecution qe= QueryExecutionFactory.create(query, model);
                            ResultSet resultset = qe.execSelect();
                            ResultSet results = ResultSetFactory.copyResults(resultset); 


                            List<Object> values = new ArrayList<Object>();
                            while ( results.hasNext() ) {
                                values.add( results.next().get( "Path" ));
                            }



                            String s = values.toString();



                           Pattern p = Pattern.compile("(?<=#)([^,]*)(?=(,|\\]))");
                              Matcher m = p.matcher(s); 



                               List<String> listhl = new ArrayList<String>() ;

                               while (m.find()) {
                                   listhl.add(m.group(1));
                                }

                               listhl.add(usecell);

                               List<Object> objectlist = new ArrayList<Object>(listhl);

                               System.out.println(objectlist);

                        Object[] allCells = mxGraphModel.getChildren(graph.getModel(), graph.getDefaultParent());
                        if (cell != null) {
                        if (graph.getModel().isVertex(cell)) {

                            for( Object myCell : objectlist) {

                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
                            }


                            for ( Object myCell: allCells) {
                                graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_PALE);
                                graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_PALE);
                            }
                    }else {
                        for( Object myCell: allCells) {
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
                        }
                    }


                        mxRectangle bounds = graph.getBoundsForCells(allCells, true, true, true);
                        graph.repaint( bounds);
                    }   
                }
                }
            });
        }

错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at GUIquery$14.mouseReleased(GUIquery.java:1087)
    at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(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)

当我使用此代码时,它只突出显示父顶点的子代,它可以正常工作。我只需要扩展它,以便突出显示更多单元格,具体取决于查询中出现的对象列表:

public void CellHighlight() {



        graphComponent.getGraphControl().addMouseListener(new MouseAdapter() {
            public void mouseReleased (MouseEvent e1) {
                if (e1.getButton() == 1 && e1.getClickCount() == 2) {
                final Object selectedCell = graphComponent.getCellAt(e1.getX(), e1.getY());
                Object[] allCells = mxGraphModel.getChildren(graph.getModel(), graph.getDefaultParent());
                if (selectedCell != null) {
                    if (graph.getModel().isVertex( selectedCell)) {
                        for( Object myCell: allCells) {
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_PALE);
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_PALE);

                        }
                        List<Object> cellList = new ArrayList<Object>(); 
                        cellList.add(selectedCell);
                        Object[] outgoingEdges = mxGraphModel.getOutgoingEdges( graph.getModel(), selectedCell);
                        for( Object edge: outgoingEdges) {
                            cellList.add( graph.getModel().getTerminal( edge, false)); 
                        }
                        cellList.addAll( Arrays.asList(outgoingEdges));
                        for( Object myCell: cellList) {
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
                        }
                    } else {
                        for( Object myCell: allCells) {
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
                            graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_TEXT_OPACITY, OPACITY_HL);
                        }
                    }
                    mxRectangle bounds = graph.getBoundsForCells(allCells, true, true, true);
                    graph.repaint( bounds);
                }
            } 


        }
            });

        }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。添加对象和对象列表不是null,但jgraphx不识别相同的对象。应该立即将顶点/单元格添加为对象。