2017-04-04 223 views
0

我想從cassandra keyspace中加載一個實體。我無法找到我的代碼中的錯誤。我在DAO類中得到一個空指針異常。Spring Data Repository - NullPointerException

下面是我的代碼:

cassandra.properties

cassandra.contactpoints=localhost 
cassandra.port=9042 
cassandra.keyspacename=kssuo 

DressSetDO

package com.suo.pojo; 

import org.springframework.data.cassandra.mapping.Column; 
import org.springframework.data.cassandra.mapping.PrimaryKey; 
import org.springframework.data.cassandra.mapping.Table; 

@Table(value = "dress_set_links") 
public class DressSetDO { 
    @PrimaryKey 
    private String id; 

    @Column(value = "set_name") 
    private String setName; 
    private String shirt; 
    private String tshirt; 
    private String trouser; 
    @Column(value = "sun_glasses") 
    private String sunGlasses; 
    private String belt; 
    private String shoes; 
    private String watch; 
    private String sweater; 
    private String jacket; 
    private String suits; 
    private String bags; 
    private String tie; 
    @Column(value = "pocket_square") 
    private String pocketSquare; 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getSetName() { 
     return setName; 
    } 

    public void setSetName(String setName) { 
     this.setName = setName; 
    } 

    public String getShirt() { 
     return shirt; 
    } 

    public void setShirt(String shirt) { 
     this.shirt = shirt; 
    } 

    public String getTshirt() { 
     return tshirt; 
    } 

    public void setTshirt(String tshirt) { 
     this.tshirt = tshirt; 
    } 

    public String getTrouser() { 
     return trouser; 
    } 

    public void setTrouser(String trouser) { 
     this.trouser = trouser; 
    } 

    public String getSunGlasses() { 
     return sunGlasses; 
    } 

    public void setSunGlasses(String sunGlasses) { 
     this.sunGlasses = sunGlasses; 
    } 

    public String getBelt() { 
     return belt; 
    } 

    public void setBelt(String belt) { 
     this.belt = belt; 
    } 

    public String getShoes() { 
     return shoes; 
    } 

    public void setShoes(String shoes) { 
     this.shoes = shoes; 
    } 

    public String getWatch() { 
     return watch; 
    } 

    public void setWatch(String watch) { 
     this.watch = watch; 
    } 

    public String getSweater() { 
     return sweater; 
    } 

    public void setSweater(String sweater) { 
     this.sweater = sweater; 
    } 

    public String getJacket() { 
     return jacket; 
    } 

    public void setJacket(String jacket) { 
     this.jacket = jacket; 
    } 

    public String getSuits() { 
     return suits; 
    } 

    public void setSuits(String suits) { 
     this.suits = suits; 
    } 

    public String getBags() { 
     return bags; 
    } 

    public void setBags(String bags) { 
     this.bags = bags; 
    } 

    public String getTie() { 
     return tie; 
    } 

    public void setTie(String tie) { 
     this.tie = tie; 
    } 

    public String getPocketSquare() { 
     return pocketSquare; 
    } 

    public void setPocketSquare(String pocketSquare) { 
     this.pocketSquare = pocketSquare; 
    } 

    public String toString() { 
     return "DressSetLinks {" 
       + "id: " + id 
       + "setName: " + setName 
       + "shirt: " + shirt 
       + "tshirt: " + tshirt 
       + "trouser: " + trouser 
       + "sunGlasses: " + sunGlasses 
       + "belt: " + belt 
       + "shoes: " + shoes 
       + "watch: " + watch 
       + "sweater: " + sweater 
       + "jacket: " + jacket 
       + "suits: " + suits 
       + "bags: " + bags 
       + "tie: " + tie 
       + "pocketSquare: " + pocketSquare 
       + "}"; 
    } 
} 

DressSetDAO

package com.suo.dao;

import java.util.List; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

import com.suo.pojo.DressSetDO; 
import com.suo.repo.DressSetRepository; 

@Component 
public class DressSetDAO { 

    @Autowired 
    private DressSetRepository dressRepo; 

    public List<DressSetDO> search(String id) { 
     List<DressSetDO> list = dressRepo.findById(id); 

     System.out.println(">>> DressSet List: " + list); 
     return list; 
    } 
} 

CassandraConfig

package com.suo.config; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 
import org.springframework.core.env.Environment; 
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean; 
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean; 
import org.springframework.data.cassandra.config.SchemaAction; 
import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration; 
import org.springframework.data.cassandra.convert.CassandraConverter; 
import org.springframework.data.cassandra.convert.MappingCassandraConverter; 
import org.springframework.data.cassandra.core.CassandraOperations; 
import org.springframework.data.cassandra.core.CassandraTemplate; 
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext; 
import org.springframework.data.cassandra.mapping.CassandraMappingContext; 
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; 

@Configuration 
@PropertySource(value = { "classpath:cassandra.properties" }) 
@ComponentScan(basePackages = {"com.suo.dao", "com.suo.repo", "com.suo.pojo", "com.suo.config"}) 
@EnableCassandraRepositories(basePackages = { "com.suo.repo" }) 
public class CassandraConfig extends AbstractCassandraConfiguration { 

    private final String KEY_SPACE = "kssuo"; 

    @Autowired 
    private Environment env; 

    @Override 
    protected String getKeyspaceName() { 
     return env.getProperty("cassandra.keyspacename"); 
    } 

    @Bean 
    public CassandraClusterFactoryBean cluster() { 
     CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); 
     cluster.setContactPoints(env.getProperty("cassandra.contactpoints")); 
     cluster.setPort(Integer.parseInt(env.getProperty("cassandra.port"))); 

     return cluster; 
    } 

    @Bean 
    public CassandraMappingContext mappingContext() { 
     return new BasicCassandraMappingContext(); 
    } 

    @Bean 
    public CassandraConverter converter() { 
     return new MappingCassandraConverter(mappingContext()); 
    } 

    @Bean 
    public CassandraSessionFactoryBean session() { 
     CassandraSessionFactoryBean session = new CassandraSessionFactoryBean(); 
     session.setCluster(cluster().getObject()); 
     session.setKeyspaceName(getKeyspaceName()); 
     session.setConverter(converter()); 
     session.setSchemaAction(SchemaAction.NONE); 

     return session; 

    } 

    @Bean 
    public CassandraOperations cassandraTemp() throws Exception { 
     return new CassandraTemplate(session().getObject()); 
    } 

} 

DressSetRepository

包com.suo.repo;

import java.io.Serializable; 
import java.util.List; 

import org.springframework.data.repository.CrudRepository; 
import org.springframework.stereotype.Repository; 

import com.suo.pojo.DressSetDO; 

@Repository 
public interface DressSetRepository extends CrudRepository<DressSetDO, Serializable>{ 
    List<DressSetDO> findById(String id); 
} 

Applicaiton.java

包com.suo.main;

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

import com.suo.dao.DressSetDAO; 

@SpringBootApplication 
public class Application { 

    @Autowired 
    private static DressSetDAO dressDao; 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
     dressDao = new DressSetDAO(); 
     if(dressDao != null) { 
      dressDao.search("1"); 
     } else { 
      System.out.println("DressSetDAO bean is not autowired !"); 
     } 
    } 
} 

ERROR

. ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: Spring Boot ::  (v1.5.2.RELEASE) 

2017-04-04 16:29:03.129 INFO 17034 --- [   main] com.example.Application     : Starting Application on Rams-MacBook-Pro.local with PID 17034 (/Users/Chandra/Documents/workspace-sts-3.8.3.RELEASE/suitup.online/target/classes started by Chandra in /Users/Chandra/Documents/workspace-sts-3.8.3.RELEASE/suitup.online) 
2017-04-04 16:29:03.132 INFO 17034 --- [   main] com.example.Application     : No active profile set, falling back to default profiles: default 
2017-04-04 16:29:03.176 INFO 17034 --- [   main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.spring[email protected]1d7acb34: startup date [Tue Apr 04 16:29:03 PDT 2017]; root of context hierarchy 
2017-04-04 16:29:03.546 WARN 17034 --- [   main] f.a.AutowiredAnnotationBeanPostProcessor : Autowired annotation is not supported on static fields: private static com.suo.dao.DressSetDAO com.example.Application.dressDao 
2017-04-04 16:29:03.833 INFO 17034 --- [   main] com.datastax.driver.core.ClockFactory : Using native clock to generate timestamps. 
2017-04-04 16:29:03.978 INFO 17034 --- [   main] com.datastax.driver.core.NettyUtil  : Did not find Netty's native epoll transport in the classpath, defaulting to NIO. 
2017-04-04 16:29:04.216 WARN 17034 --- [   main] com.datastax.driver.core.Cluster   : You listed localhost/0:0:0:0:0:0:0:1:9042 in your contact points, but it wasn't found in the control host's system.peers at startup 
2017-04-04 16:29:04.305 INFO 17034 --- [   main] c.d.d.c.p.DCAwareRoundRobinPolicy  : Using data-center name 'datacenter1' for DCAwareRoundRobinPolicy (if this is incorrect, please provide the correct datacenter name with DCAwareRoundRobinPolicy constructor) 
2017-04-04 16:29:04.306 INFO 17034 --- [   main] com.datastax.driver.core.Cluster   : New Cassandra host localhost/127.0.0.1:9042 added 
2017-04-04 16:29:04.396 INFO 17034 --- [   main] o.s.j.e.a.AnnotationMBeanExporter  : Registering beans for JMX exposure on startup 
2017-04-04 16:29:04.407 INFO 17034 --- [   main] com.example.Application     : Started Application in 11.497 seconds (JVM running for 11.768) 
Exception in thread "main" java.lang.NullPointerException 
    at com.suo.dao.DressSetDAO.search(DressSetDAO.java:18) 
    at com.example.Application.main(Application.java:19) 
+0

你可以分享'DressSetRepository'類代碼嗎?這可能會有所幫助 –

+0

添加了DressSetRepository代碼 – chandra

+0

您在存儲庫接口上不需要'@ Repository',實際上它可能會干擾其他組件。 – mp911de

回答

0

假設你應該修改DressSetDO POJO的代碼。

DressSetDO中的id應改爲Long

@PrimaryKey 
private Long id; 
+0

primaryKey是一個UUID並且不長。所以我用它作爲字符串 – chandra

+0

只需使用Long即可。我想應該是很長時間來支持Spring數據存儲庫[link](https://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/) –

相關問題