Java 图中的NullPointerException和MouseListener

Java 图中的NullPointerException和MouseListener,java,jena,mouselistener,jgrapht,jgraphx,Java,Jena,Mouselistener,Jgrapht,Jgraphx,我试图使用这段代码来更改连接到jgraphx中单击的顶点的某些顶点的不透明度,当您单击顶点之外的某个位置时,它将重新更改不透明度。单击顶点时,将查询其值/字符串,并将连接到该顶点的所有对象添加到新对象列表中。然后我处理这个对象列表来选择哪些要突出显示,哪些不突出显示。然而,我不断得到错误,无法修复它。我没有任何空对象,因为我已经检查过了。如果有人能帮忙,我将不胜感激 graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE

我试图使用这段代码来更改连接到jgraphx中单击的顶点的某些顶点的不透明度,当您单击顶点之外的某个位置时,它将重新更改不透明度。单击顶点时,将查询其值/字符串,并将连接到该顶点的所有对象添加到新对象列表中。然后我处理这个对象列表来选择哪些要突出显示,哪些不突出显示。然而,我不断得到错误,无法修复它。我没有任何空对象,因为我已经检查过了。如果有人能帮忙,我将不胜感激

graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
错误是指以下行:

graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);
代码如下:

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);
                }
            } 


        }
            });

        }
当我使用这段代码时,它只突出显示父顶点的子顶点,工作正常。我只需要展开它,以便根据查询中出现的对象列表高亮显示更多单元格:

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);
                }
            } 


        }
            });

        }
public void CellHighlight(){
graphComponent.getGraphControl().addMouseListener(新的MouseAdapter(){
公共无效MouseEvent(MouseEvent e1){
如果(e1.getButton()==1和&e1.getClickCount()==2){
最终对象selectedCell=graphComponent.getCellAt(e1.getX(),e1.getY());
Object[]allCells=mxGraphModel.getChildren(graph.getModel(),graph.getDefaultParent());
如果(selectedCell!=null){
if(graph.getModel().isVertex(selectedCell)){
对于(对象myCell:allCells){
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u OPACITY,OPACITY\u PALE);
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u TEXT\u OPACITY,OPACITY\u PALE);
}
List cellList=新的ArrayList();
cellList.add(selectedCell);
Object[]outgoingEdges=mxGraphModel.getOutgoingEdges(graph.getModel(),selectedCell);
用于(对象边:延伸边){
add(graph.getModel().getTerminal(edge,false));
}
addAll(Arrays.asList(outgoingEdges));
对于(对象myCell:cellList){
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u OPACITY,OPACITY\u HL);
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u TEXT\u OPACITY,OPACITY\u HL);
}
}否则{
对于(对象myCell:allCells){
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u OPACITY,OPACITY\u HL);
graph.getView().getState(myCell.getStyle().put(mxConstants.STYLE\u TEXT\u OPACITY,OPACITY\u HL);
}
}
mxRectangle bounds=graph.getBoundsForCells(所有单元格,true,true,true);
图.重新绘制(边界);
}
} 
}
});
}

我找到了解决方案。正在添加对象,并且对象列表不是空的,但是jgraphx无法识别相同的对象。应该立即添加顶点/单元格作为对象。

粗略猜测,我认为
getState(myCell)
函数可能返回
null
。通常,尝试将NPE线路拆分为多个调用,这样您就可以看到返回的
null
。是的,这就是问题所在,但我不明白为什么。objectlist不是空的@弗拉维奥
graph.getView().getState(myCell).getStyle().put(mxConstants.STYLE_OPACITY, OPACITY_HL);