Asma.O
Asma.O

Reputation: 465

Drawing a shape with draw graphic method?

am trying to draw a graphic based on a shape that gets geometry of a point and change it's color once the row of this geometry "bornes" is selected the problem is i get this error every time i select a row here's the log file: thanks in advance

     Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.vividsolutions.jump.workbench.ui.renderer.java2D.Java2DConverter.toShape(Java2DConverter.java:313)
at com.vividsolutions.jump.workbench.ui.plugin.specific.SearchPropertiesPlugin$2.valueChanged(SearchPropertiesPlugin.java:144)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.setSelectionInterval(DefaultListSelectionModel.java:442)
at javax.swing.JTable.changeSelectionModel(JTable.java:2352)
at javax.swing.JTable.changeSelection(JTable.java:2421)
at javax.swing.plaf.basic.BasicTableUI$Handler.adjustSelection(BasicTableUI.java:1085)
at javax.swing.plaf.basic.BasicTableUI$Handler.mousePressed(BasicTableUI.java:1008)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
at java.awt.Component.processMouseEvent(Component.java:6294)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
at java.awt.Component.processEvent(Component.java:6062)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4660)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4233)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2489)
at java.awt.Component.dispatchEvent(Component.java:4488)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:668)
at java.awt.EventQueue.access$400(EventQueue.java:81)
at java.awt.EventQueue$2.run(EventQueue.java:627)
at java.awt.EventQueue$2.run(EventQueue.java:625)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$3.run(EventQueue.java:641)
at java.awt.EventQueue$3.run(EventQueue.java:639)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:638)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

here's the lines of the code concerned by this error, it happens in the listener of the bornesTable that contains the bornes (Name, X, Y)

     searchProperties.getBornesTable().getSelectionModel().addListSelectionListener(new ListSelectionListener(){

    @Override
    public void valueChanged(ListSelectionEvent e) {

        try {

            if (search()!= null){
        // here's the line 144 in toShape() method      
            final Shape shape = context.getLayerViewPanel().getViewport().getJava2DConverter().toShape(selectedBornes());
            Color color = Color.yellow;
            Stroke stroke =new BasicStroke(5, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
            final Graphics2D graphics = (Graphics2D) context.getLayerViewPanel().getGraphics();
            graphics.setColor(color);
            //graphics.setXORMode(Color.white);
            graphics.setStroke(stroke);
            graphics.fill(shape);

            }
            else
                  JOptionPane.showMessageDialog(null, "vous n'avez pas selectionner une bornes " );

        } catch (NoninvertibleTransformException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }




    }

   });

here's the method: selectedBornes()

   public Geometry selectedBornes() {

    if (getSelectedRowIndex()> 0)
    {
     Geometry geometry;
     geometry = searchBorne().get(getSelectedRowIndex()).getGeometry();
     return geometry;
    }
    return null;

}   

and the method serchBornes() there's no problem with this but i'll post it

   private List<BasicFeature> searchBorne() {

    final List<BasicFeature> basicFeatureBornes = new ArrayList<BasicFeature>();

     for(BasicFeature basicFeature : BornesList) {

                if ( searchProperties.getNumTextField().getText().equals(basicFeature.getString("num")) &&
                        searchProperties.getComplementComboBox().getSelectedItem().equals(basicFeature.getString("complement")))
                    basicFeatureBornes.add(basicFeature);


                    }
     return basicFeatureBornes;

}

and the method getSelectedRowIndex() there's no problem with this too, but i'll post it

    public int getSelectedRowIndex() {

    int row = 0;
     for(int i = 0; i < searchBorne().size() ; i++ ) {

          row = searchProperties.getBornesTable().getSelectedRow();

     }

    return row;

}

Upvotes: 1

Views: 153

Answers (2)

Lakshmi
Lakshmi

Reputation: 2294

final Shape shape = context.getLayerViewPanel().getViewport().getJava2DConverter().toShape(selectedBornes());

Here you are directly passing the selectedBornes() value to the toShape method but you did not check whether the selectedBornes() value is null or not as a result you get NullPointerException if no RowIndex is selected so change your code as

Goemetry newGometry=selectedBornes();
if (newGometry !=null){
final Shape shape = context.getLayerViewPanel().getViewport().getJava2DConverter().toShape(newGometry);
}

Upvotes: 3

StanislavL
StanislavL

Reputation: 57381

public Geometry selectedBornes() {
    if (getSelectedRowIndex()> 0)
    {
     Geometry geometry;
     geometry = searchBorne().get(getSelectedRowIndex()).getGeometry();
     return geometry;
    }

What happens if selected index is 0 (for the first element)? It returns null and the method crashes. Add a check to process the cases if nothing is selected.

Upvotes: 2

Related Questions