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++) { //jframe.getMapContent().layers().get(i).dispose(); map.removeLayer(layerList.get(i)); } shapeFile = new File(pathname); loadFile(shape); //worldAf = jframe.getMapPane().getScreenToWorldTransform(); //scaleRatio = new BigDecimal(worldAf.getScaleX()); } 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 ListenerLoad 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 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); } 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
SEVERE: nulljava.lang.NullPointerException at com.mycompany.shapefileexporter.mvn.ShapefileExporter$1.action(ShapefileExporter.java:183) 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).
أكثر...
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++) { //jframe.getMapContent().layers().get(i).dispose(); map.removeLayer(layerList.get(i)); } shapeFile = new File(pathname); loadFile(shape); //worldAf = jframe.getMapPane().getScreenToWorldTransform(); //scaleRatio = new BigDecimal(worldAf.getScaleX()); } 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 ListenerLoad 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 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); } 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
SEVERE: nulljava.lang.NullPointerException at com.mycompany.shapefileexporter.mvn.ShapefileExporter$1.action(ShapefileExporter.java:183) 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).
أكثر...