Java Spring数据存储库-NullPointerException
我试图从cassandra键空间加载实体。我无法找出代码中的错误所在。我在DAO类中得到一个空指针异常 下面是我的代码: 卡桑德拉酒店 服装套装 梳妆台 包com.suo.daoJava Spring数据存储库-NullPointerException,java,spring,cassandra,spring-data,spring-data-cassandra,Java,Spring,Cassandra,Spring Data,Spring Data Cassandra,我试图从cassandra键空间加载实体。我无法找出代码中的错误所在。我在DAO类中得到一个空指针异常 下面是我的代码: 卡桑德拉酒店 服装套装 梳妆台 包com.suo.dao import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.suo.pojo.Dre
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;
}
}
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);
}
错误
假设您应该修改DressSetDO POJO中的代码 在DressSetDO中,id应更改为
Long
@PrimaryKey
private Long id;
假设您应该修改DressSetDO POJO中的代码 在DressSetDO中,id应更改为
Long
@PrimaryKey
private Long id;
您可以共享
DressSetRepository
类代码吗。添加DressSetRepository代码可能有帮助您不需要在Repository界面上使用@Repository
,事实上,它可能会干扰其他组件。DressSetRepository似乎没有问题。DressSetRepository未在DressSetDAO类中自动连接。在调用dressRepo.findById(id)之前,请先对dressRepo进行null检查,并查看其是否为null。您是否可以共享DressSetRepository
类代码。添加DressSetRepository代码可能有帮助您不需要在Repository界面上使用@Repository
,事实上,它可能会干扰其他组件。DressSetRepository似乎没有问题。DressSetRepository未在DressSetDAO类中自动连接。在调用dressRepo.findById(id)之前,请先对dressRepo进行null检查,看看它是否为null。primaryKey是一个UUID,长度不长。所以我用它作为字符串,只是试着用Long。我认为支持Spring数据存储库需要很长时间,primaryKey是一个UUID,而不是很长。所以我用它作为字符串,只是试着用Long。我认为支持Spring数据存储库应该需要很长时间
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);
}
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 !");
}
}
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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.springframework.context.annotation.AnnotationConfigApplicationContext@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)
@PrimaryKey
private Long id;