I see; so it really just did not connect to your shapefile. Can I do the
obvious thing and confirm that you have bc_hosptials.shp,
bc_hospitals.dbf etc...
A couple of options:
- Start up a debugger and place a break point inside
ShapefileDataStoreFactory .canProcess; and watch it try and connect to
your file.
- You could also use new ShapefileDataStore( ... ) directly; that should
at least produce a more interesting interesting error message.
Cheers,
Jody
> dear Jody:
> the result is the same. the code is shown below.
> it shows: Hello Geotools
> file:/D:/liu/gt/work/autoclust/bc_hospitals.shp
> java.lang.NullPointerException at readDATA.main(readDATA.java:44) (the
> line i marked red)
> //package org.geotools.demo.example;
> import java.io.File;
> import java.util.Map;
> import java.util.HashMap;
> import org.geotools.data.DataStore;
> import org.geotools.data.DataStoreFinder;
> import org.geotools.data.FeatureSource;
> import org.geotools.feature.FeatureCollection;
> import org.geotools.feature.FeatureIterator;
> import org.geotools.feature.Feature;
> import com.vividsolutions.jts.geom.Geometry;
> import org.geotools.data.shapefile.*;
> import org.geotools.data.shapefile.shp.*;
> import org.geotools.data.shapefile.dbf.*;
> import org.geotools.data.shapefile.indexed.*;
> import org.geotools.data.shapefile.prj.*;
> import org.geotools.referencing.crs.*;
> //import org.geotools.factory.GeoTools;
> public class readDATA
> {
> public static void main( String[] args )
> {
> try{
>
> System.out.println( "Hello GeoTools " );
> try {
> //if( args.length != 1 ) System.exit(1);
> File file = new File( "bc_hospitals.shp" );
> if( !file.exists() ) System.exit(1);
>
> Map connect = new HashMap();
> connect.put( "url", file.toURL() );
> System.out.println(file.toURL().toString());
> DataStore dataStore = DataStoreFinder.getDataStore( connect );
> String[] typeNames = dataStore.getTypeNames();
> String typeName = typeNames[0];
> System.out.println( "Reading content "+ typeName );
> FeatureSource featureSource = dataStore.getFeatureSource( typeName );
> FeatureCollection collection = featureSource.getFeatures();
> FeatureIterator iterator = collection.features();
> int length = 0;
> try {
> while( iterator.hasNext() ){
> Feature feature = iterator.next();
> Geometry geometry = feature.getDefaultGeometry();
> length += geometry.getLength();
> }
> }
> finally {
> iterator.close();
> }
> System.out.println( "Total length "+length );
>
> } catch (Exception e) {
> e.printStackTrace();
> }
>
> }
> catch( NullPointerException problem){
> problem.printStackTrace();
> System.exit(1);
> }
>
> }
> }
>
> ------------------------------------------------------------------------
> *From:* Jody Garnett [mailto:[EMAIL PROTECTED]
> *Sent:* Thu 10/25/2007 1:56 PM
> *To:* #LIU DONGQUAN#; gt2-users
> *Subject:* Re: [Geotools-gt2-users] Problem of reading shapefile
>
> #LIU DONGQUAN# wrote:
> > I am new to Java. How can I check the stack trace? I am not using
> any IDE now. And I did not install maven either. I only installed JDK
> and used command line to run my program. Should I install eclipse?
> >
> Put the following around the code that is producing the error:
>
> try {
>
> }
> catch( NullPointerException problem){
> problem.printStackTrace();
> System.exit(1);
> }
>
> Cheers,
> jody
> > dq
> >
> > -----Original Message-----
> > From: Jody Garnett [mailto:[EMAIL PROTECTED]
> > Sent: 2007年10月25日 13:44
> > To: #LIU DONGQUAN#; gt2-users
> > Subject: Re: [Geotools-gt2-users] Problem of reading shapefile
> >
> > No worries; 2.3.3 should be fine; now about that stack trace?
> > Jody
> > #LIU DONGQUAN# wrote:
> >
> >> Dear Jody:
> >> Thank you for your reply. I am using gt2-2.3.3.a. should I try
> other version?
> >>
> >> Regards
> >> liudq
> >>
> >> -----Original Message-----
> >> From: Jody Garnett [mailto:[EMAIL PROTECTED]
> >> Sent: 2007年10月25日 5:52
> >> To: #LIU DONGQUAN#
> >> Cc: [email protected]
> >> Subject: Re: [Geotools-gt2-users] Problem of reading shapefile
> >>
> >> mayol wrote:
> >>
> >>
> >>> hi, all
> >>> i am new to geotools. i am trying the shapefile reading programe
> on the
> >>> website. but it got problem when running it.
> >>> problem is: java.lang.NullPointerException!!!
> >>>
> >>>
> >>>
> >> Can you give me the stack trace?
> >>
> >>
> >>
> >>> the code is shown below. i think problem is in this line: "DataStore
> >>> dataStore = DataStoreFinder.getDataStore( connect );". please help
> me about
> >>> it. thanks a lot~~~~
> >>>
> >>>
> >>>
> >>
> >>
> >>> File file = new File( "bc_hospitals.shp" );
> >>> if( !file.exists() ) System.exit(1);
> >>>
> >>> Map connect = new HashMap();
> >>> connect.put( "url", file.toURL() );
> >>> System.out.println(file.toURL().toString());
> >>>
> >>> DataStore dataStore = DataStoreFinder.getDataStore( connect );
> >>>
> >>>
> >>>
> >> That looks okay; and I see you checked to make sure the file exists
> >> already. What version of GeoTools are you running with?
> >> Jody
> >>
> >>
> >
> >
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users