0

Is there any way to clear a JMapFrame and all of its layers before opening a new file? I currently am running into an error (exception no stack trace) while opening a file with one already open that has multiple layers added to it (with the layers pane).

    fileMenu.add(new SafeAction("Open File...") {
            @Override
            public void action(ActionEvent e) throws Throwable {


                setJFCFilter("Shapefiles", "shp");
                int returnVal = jfc.showOpenDialog(jframe);

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    String pathname = jfc.getSelectedFile().getAbsolutePath();
                    URL shape = acquireURL(pathname);

                    if (shape == null) {
                        JOptionPane.showMessageDialog(jframe, "could not find file \"" + pathname + "\"", "Could not find file",
                                JOptionPane.ERROR_MESSAGE);
                    }

                    try {
                        if (layers_size > 1) {
                            for (int i = 0; i < layerList.size(); i++) {
                                map.removeLayer(layerList.get(i));
                            }

                            jframe.revalidate();
                            jframe.repaint();

                            shapeFile = new File(pathname);
                            loadFile(shape);

                            worldAf = jframe.getMapPane().getScreenToWorldTransform();
                            scaleRatio = new BigDecimal(worldAf.getScaleX()); // NullPointer - Exception 2
                        } else {
                            shapeFile = new File(pathname);
                            loadFile(shape);

                            worldAf = jframe.getMapPane().getScreenToWorldTransform();
                            scaleRatio = new BigDecimal(worldAf.getScaleX());
                        }

                    } catch (Exception ex) {
                        Logger.getLogger(ShapefileExporter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } // End Action
        }); // End Listener

Load File

// Load shapefile into JMapFrame
            protected void loadFile(URL shape) throws IOException {
                try {

                    FileDataStore store = FileDataStoreFinder.getDataStore(shape);
                    featureSource = store.getFeatureSource();

                    ShapefileDataStore ds = new ShapefileDataStore(shape);
                    FeatureSource<SimpleFeatureType, SimpleFeature> fs = ds.getFeatureSource();

                    collection = fs.getFeatures();
                    FeatureArray = features();

                    map = new MapContent();
                    Style style = SLD.createSimpleStyle(featureSource.getSchema());

                    Layer layer = new FeatureLayer(featureSource, style);
                    map.addLayer(layer);

                    layerList = new ArrayList();
                    layerList.add(layer);

                    jframe.setMapContent(map); // NullPointer - Exception 1
                } catch (IOException ex) {
                    Logger.getLogger(ShapefileExporter.class.getName()).log(Level.SEVERE, null, ex);
                }
            }

After running this bit of code, I run some more to add a layer. Once this second layer has been added if I run this code again (opening a different file) the exception happens.

Also I don't think I can post the full code as its over 1000 lines of code.

Exception 1

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at org.geotools.swing.MapLayerTableCellRenderer.getListCellRendererComponent(MapLayerTableCellRenderer.java:259)
    at javax.swing.plaf.basic.BasicListUI.updateLayoutState(BasicListUI.java:1361)
    at javax.swing.plaf.basic.BasicListUI.maybeUpdateLayoutState(BasicListUI.java:1311)
    at javax.swing.plaf.basic.BasicListUI.getPreferredSize(BasicListUI.java:578)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1661)
    at javax.swing.ScrollPaneLayout.layoutContainer(ScrollPaneLayout.java:790)
    at java.awt.Container.layout(Container.java:1503)
    at java.awt.Container.doLayout(Container.java:1492)
    at java.awt.Container.validateTree(Container.java:1688)
    at java.awt.Container.validate(Container.java:1623)
    at javax.swing.RepaintManager$2.run(RepaintManager.java:691)
    at javax.swing.RepaintManager$2.run(RepaintManager.java:689)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.validateInvalidComponents(RepaintManager.java:688)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1676)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Exception 2

Dec 31, 2015 3:01:09 PM com.mycompany.shapefileexporter.mvn.ShapefileExporter$1 action
SEVERE: null
java.lang.NullPointerException
    at com.mycompany.shapefileexporter.mvn.ShapefileExporter$1.action(ShapefileExporter.java:249)
    at org.geotools.swing.action.SafeAction.actionPerformed(SafeAction.java:50)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:157)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.Component.processMouseEvent(Component.java:6516)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6281)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4872)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4698)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
    at java.awt.EventQueue.access$300(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:706)
    at java.awt.EventQueue$3.run(EventQueue.java:704)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.awt.EventQueue$4.run(EventQueue.java:718)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

Update: Followed @iant advice and still running into null pointer exceptions, and the layers, after removing are still there (I managed to delete one but received multiple null pointer exceptions).

Update: 12/30/15
In open file menu item (above), attempting to reset the JMapFrame, nothing shows the frame just blinks and stays the same.

jframe.setVisible(false);
jframe.repaint();
initialize();
jframe.repaint();

shapeFile = new File(pathname);
loadFile(shape);
11
  • You go through the layers list and remove each layer in turn.
    – Ian Turton
    Dec 19, 2015 at 18:59
  • 1
    At least tell us what the exception is, maybe run in a debugger to find the issue.
    – Ian Turton
    Dec 19, 2015 at 19:00
  • how do i get the layer list? Dec 19, 2015 at 22:58
  • I just used List<Layer> layers = jframe.getMapContent().layers(); System.out.println("Layers: " + layers.size()); Which either gives me 1 layer or a null pointer Dec 19, 2015 at 23:14
  • Using this: (does not work) for (int i = 0; i < map.layers().size(); i++) { map.layers().get(i).dispose(); } Dec 22, 2015 at 23:25

1 Answer 1

1

This should work (it works great in my program)

map.layers().clear();
2
  • Pretty sure I've tried that and it doesn't work, normally it would. But there is some error in the library that is causing an exception, nothing can be done to fix it unless the error is fixed. Jun 2, 2016 at 15:04
  • have you tried this /* if (mapPane != null && mapPane.getMapContent() != null) { for (Layer layer : mapPane.getMapContent().layers()) { mapPane.getMapContent().removeLayer(layer); } }*/ Jun 3, 2016 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.