我正在尝试使用以下方法创建一个思维导图样式的应用程序:
Netbeans 7.2 jdk 6.35 Primefaces 3.4
这个想法是通过访问数据库并从该数据库生成节点来生成思维导图。我已确保该程序从与我尝试访问的对象相同类型的对象中生成合适的图形。但是,当我尝试访问数据库信息时,我得到了 NullPointerException。我在生成通用页面时使用的步骤是:
1)创建数据库并填充它们。2)在netbeans中使用了“来自数据库的新实体类”功能。3) 使用了“来自实体类的 JSF 页面”功能。4) 创建了一个 JSF Managed Bean 来生成思维导图。
下面是思维导图 Bean 的代码:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package jsf;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import jpa.entities.Enginetable;
import jpa.session.EnginetableFacade;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.mindmap.DefaultMindmapNode;
import org.primefaces.model.mindmap.MindmapNode;
/**
*
* @author AOL
*/
@Named(value = "mmBean")
@RequestScoped
public class mmBean {
private MindmapNode root;
private MindmapNode selectedNode;
private List<Enginetable> elements;
private List<Enginetable> filteredElements;
private List<Enginetable> userlist;
private Enginetable selectedElement;
private Enginetable[] selectedElements;
private List<Enginetable> elementlist = new ArrayList<Enginetable>();
private Enginetable nodeentity = new Enginetable();
private Enginetable rootentity = new Enginetable();
private Enginetable userentity = new Enginetable();
private jpa.session.EnginetableFacade ejbFacade;
private static final String[] username;
static{
username = new String[10];
username[0] = "Mercedes";
username[1] = "BMW";
username[2] = "Volvo";
username[3] = "Audi";
username[4] = "Renault";
username[5] = "Opel";
username[6] = "Volkswagen";
username[7] = "Chrysler";
username[8] = "Ferrari";
username[9] = "Ford";
}
/**
* Creates a new instance of MmBean
*/
public mmBean() {
///if(elements==null){
elements = new ArrayList<Enginetable>();
//}
//remoteuser=null;
// this.populateRandomElements(elements, 10);
this.populateRandomElements(elements,6);
// filteredElements = new ArrayList<Graphelement>();
root = new DefaultMindmapNode("Mercedes", "Google", "FFCC00", false);
/*MindmapNode bmw = new DefaultMindmapNode("BMW", "IP Nos", "6e9ebf", true);
MindmapNode volvo = new DefaultMindmapNode("Volvo", "Names", "6e9ebf", true);
MindmapNode audi = new DefaultMindmapNode("Audi", "Malicious ", "6e9ebf", true);
MindmapNode renault = new DefaultMindmapNode("Renault", "IP Nos", "6e9ebf", true);
MindmapNode opel = new DefaultMindmapNode("Opel", "Names", "6e9ebf", true);
MindmapNode volkwagen = new DefaultMindmapNode("Volkswagen", "Malicious ", "6e9ebf", true);
MindmapNode chrysler = new DefaultMindmapNode("Chrysler", "IP Nos", "6e9ebf", true);
MindmapNode ferrari = new DefaultMindmapNode("Ferrari", "Names", "6e9ebf", true);
MindmapNode ford = new DefaultMindmapNode("Ford", "Malicious ", "6e9ebf", true);*/
Object label = root.getLabel();
Enginetable entity;
for( int i = 0 ; i < this.elements.size() ; i++){
entity = this.elements.get(i);
if ( !label.equals(entity.getStartnode() ) && label.equals(entity.getEndnode() ) ) {
root.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "00FF99", true));
}
else if ( !label.equals(entity.getStartnode() ) && !label.equals(entity.getEndnode() ) ){
root.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "CC0000", true));
}
else if ( label.equals(entity.getStartnode() ) ) {
this.rootentity.setStartnode( entity.getStartnode() );
this.rootentity.setEndnode( entity.getEndnode() );
this.rootentity.setConnections( entity.getConnections() );
this.rootentity.setGroupid( entity.getGroupid() );
this.rootentity.setMoveid( entity.getMoveid() );
this.rootentity.setNewconnectioncost( entity.getNewconnectioncost() );
this.rootentity.setNewinfovalue( entity.getNewinfovalue() );
this.rootentity.setUsername( entity.getUsername() );
this.rootentity.setUserid( entity.getUserid() );
}
}
}
public void setElements(List<Enginetable> elements) {
this.elements = elements;
}
public List<Enginetable> getElements() {
return elements;
}
public void setFilteredElements(List<Enginetable> filteredElements) {
//this.filteredElements = filteredElements;
this.elements=filteredElements;
}
public List<Enginetable> getFilteredElements() {
return filteredElements;
}
public void setSelectedElements(Enginetable[] selectedElements) {
this.selectedElements = selectedElements;
}
public Enginetable[] getSelectedElements() {
return selectedElements;
}
public void setSelectedElement(Enginetable selectedElement) {
this.selectedElement = selectedElement;
}
public Enginetable getSelectedElement() {
return selectedElement;
}
public MindmapNode getRoot() {
return root;
}
public MindmapNode getSelectedNode() {
return selectedNode;
}
public void setSelectedNode(MindmapNode selectedNode) {
this.selectedNode = selectedNode;
}
public void onNodeSelect(SelectEvent event) {
MindmapNode node = (MindmapNode) event.getObject();
for( int i = 0 ; i < this.elements.size() ; i++){
Enginetable entity;
Object label = node.getLabel();
Object rootlabel = this.root.getLabel();
entity = elements.get(i);
if ( entity.getStartnode() == rootlabel ){
this.rootentity = entity;
}
if ( entity.getStartnode() == label ){
this.nodeentity = entity;
}
}
//load children of select node and add via node.addNode(childNode);
//populate if not already loaded
if(node.getChildren().isEmpty()) {
for( int i = 0 ; i < this.elements.size() ; i++){
Enginetable entity;
entity = this.elements.get(i);
if ( (this.nodeentity.getStartnode() == null ? entity.getStartnode() != null : !this.nodeentity.getStartnode().equals(entity.getStartnode())) && (this.nodeentity.getEndnode() == null ? entity.getStartnode() != null : !this.nodeentity.getEndnode().equals(entity.getStartnode())) && (this.rootentity.getEndnode() == null ? entity.getStartnode() == null : this.rootentity.getEndnode().equals(entity.getStartnode())) ) {
node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "33FF00", true));
}
else if ( (this.nodeentity.getStartnode() == null ? entity.getStartnode() != null : !this.nodeentity.getStartnode().equals(entity.getStartnode())) && (this.nodeentity.getEndnode() == null ? entity.getStartnode() == null : this.nodeentity.getEndnode().equals(entity.getStartnode())) && (this.rootentity.getEndnode() == null ? entity.getStartnode() == null : this.rootentity.getEndnode().equals(entity.getStartnode())) ) {
node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "3300FF", true));
}
else if ( (this.nodeentity.getStartnode() == null ? entity.getStartnode() != null : !this.nodeentity.getStartnode().equals(entity.getStartnode())) && (this.nodeentity.getEndnode() == null ? entity.getStartnode() == null : this.nodeentity.getEndnode().equals(entity.getStartnode())) && (this.rootentity.getEndnode() == null ? entity.getStartnode() != null : !this.rootentity.getEndnode().equals(entity.getStartnode())) ) {
node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "FF9900", true));
}
else if ( (this.nodeentity.getStartnode() == null ? entity.getStartnode() != null : !this.nodeentity.getStartnode().equals(entity.getStartnode())) && (this.nodeentity.getEndnode() == null ? entity.getStartnode() != null : !this.nodeentity.getEndnode().equals(entity.getStartnode())) && (this.rootentity.getEndnode() == null ? entity.getStartnode() != null : !this.rootentity.getEndnode().equals(entity.getStartnode())) ) {
node.addNode(new DefaultMindmapNode(entity.getStartnode() , "Malicious Software: ", "CC0000", true));
}
}
}
}
public void onNodeDblselect(SelectEvent event) {
this.selectedNode = (MindmapNode) event.getObject();
}
private EnginetableFacade getFacade() {
return ejbFacade;
}
private void populateRandomElements(List<Enginetable> list, int size) {
userlist = this.getListByUserID( 1 );
for(int i = 0 ; i < size ; i++) {
if ("Chrysler".equals(username[i])){
Random rand = new Random();
Enginetable gel =new Enginetable();
gel.setMoveid(i);
gel.setUserid(i);
gel.setStartnode(username[i]);
gel.setEndnode(username[9]);
gel.setConnections(username[9]);
// gel.setPurpose("dubious");
// gel.setRelationship("naughty");
// gel.setExperimentid(1);
gel.setNewinfovalue(1);
gel.setNewconnectioncost(1);
list.add(gel);
}
else {
Random rand = new Random();
Enginetable gel =new Enginetable();
gel.setMoveid(i);
gel.setUserid(i);
gel.setStartnode(username[i]);
gel.setEndnode(username[9-i]);
gel.setConnections(username[9-i]);
// gel.setPurpose("dubious");
// gel.setRelationship("naughty");
// gel.setExperimentid(1);
gel.setNewinfovalue(1);
gel.setNewconnectioncost(1);
list.add(gel);
}
}
}
//
public Integer getMoveId(Enginetable ent) {
return ent.getMoveid();
}
public void setMoveId(Enginetable ent, Integer id) {
ent.setMoveid(id);
}
public Integer getUserId(Enginetable ent) {
return ent.getMoveid();
}
public void setUserId(Enginetable ent, Integer id) {
ent.setMoveid(id);
}
public String getStartNode(Enginetable ent) {
return ent.getStartnode();
}
public void setStartNode(Enginetable ent, String startnode) {
ent.setStartnode(startnode);
}
public String getEndNode(Enginetable ent) {
return ent.getEndnode();
}
public void setEndNode(Enginetable ent, String endnode) {
ent.setEndnode(endnode);
}
/* public int getExperimentid(Enginetable ent) {
return ent.getExperimentid();
}
public void setExperimentid(Enginetable ent, int experimentid) {
ent.setExperimentid(experimentid);
}*/
public int getNewInfoValue(Enginetable ent) {
return ent.getNewinfovalue();
}
public void setNewInfoValue(Enginetable ent, int totalinfovalue) {
ent.setNewinfovalue(totalinfovalue);
}
public String getConnections(Enginetable ent) {
return ent.getConnections();
}
public void setConnections(Enginetable ent, String connections) {
ent.setConnections(connections);
}
public String getNewConnections(Enginetable ent) {
return ent.getConnections();
}
public void setNewConnections(Enginetable ent, String newconnections) {
ent.setConnections( newconnections );
}
/* public String getUserName(Enginetable ent) {
return ent.getUsername();
}
public void setUsername(Enginetable ent, String name) {
ent.setUsername(name);
}
*/
public List<Enginetable> getListByUsername(String user){
return this.ejbFacade.getEnginetableByUsername(user);
}
public List<Enginetable> getListByUserID( int id ){
if ( this.ejbFacade.getEnginetableByUserid(id) == null){
throw new NullPointerException("admin is null!");
}
else if ( this.ejbFacade.getEnginetableByUserid(id) != null){
List<Enginetable> list = new ArrayList<Enginetable>();
list = this.ejbFacade.getEnginetableByUserid(id);
return list;
}
else{
List<Enginetable> list = new ArrayList<Enginetable>();
Enginetable gel =new Enginetable();
gel.setMoveid(1);
gel.setUserid(1);
gel.setStartnode(username[1]);
gel.setEndnode(username[9]);
gel.setConnections(username[9]);
// gel.setPurpose("dubious");
// gel.setRelationship("naughty");
// gel.setExperimentid(1);
gel.setNewinfovalue(1);
gel.setNewconnectioncost(1);
list.add(gel);
return list;
}
}
}
为了完整起见,我已经包含了它导入的类的代码。
编辑:我删除了 Enginetable 类,因为我有太多字符无法通过堆栈跟踪提交。它只是获取和设置方法。
下面是 EngineTableFacade 类的代码: package jpa.session;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import jpa.entities.Enginetable;
/**
*
* @author AOL
*/
@Stateless
public class EnginetableFacade extends AbstractFacade<Enginetable> {
@PersistenceContext(unitName = "Networks0.1PU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public EnginetableFacade() {
super(Enginetable.class);
}
public List<Enginetable> getEnginetableByUsername( String username ) {
try {
List<Enginetable> userlist = (List<Enginetable>)
em.createNamedQuery("Enginetable.findByUsername").
setParameter("Username", username).getResultList();
return userlist;
} catch (NoResultException nre) {
return null;
}
}
public List<Enginetable> getEnginetableByUserid( int id ) {
try {
List<Enginetable> userlist = (List<Enginetable>)
em.createNamedQuery("Enginetable.findByUserid").
setParameter("Userid", id).getResultList();
return userlist;
} catch (NoResultException nre) {
return null;
}
}
}
这会在调用 getListByUsername 或 getListByUserID 方法时引发 NullPointerException,但如果不是,则可以正常工作。检查 StackTrace 后,这证实了我的想法,即抛出异常的是这两种方法。
我怀疑,虽然我不确定,这意味着实体管理器在 mmBean 中为空。我不确定如何,甚至是否可以纠正这种情况。拜托,任何建议都将不胜感激,我对这座需要跨越但不能跨越的小桥感到非常困惑和沮丧。谢谢你。
编辑以包括堆栈跟踪:
com.google.common.collect.ComputationException: java.lang.RuntimeException: java.lang.NullPointerException
at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:397)
at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:115)
at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:96)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:188)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at org.primefaces.component.mindmap.Mindmap.getValue(Mindmap.java:98)
at org.primefaces.component.mindmap.MindmapRenderer.encodeScript(MindmapRenderer.java:51)
at org.primefaces.component.mindmap.MindmapRenderer.encodeEnd(MindmapRenderer.java:44)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1764)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:186)
at org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:341)
at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:242)
at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:87)
at org.jboss.weld.bean.proxy.ClientProxyProvider.access$000(ClientProxyProvider.java:43)
at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:53)
at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:46)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:355)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:393)
... 52 more
Caused by: java.lang.NullPointerException
at jsf.mmBean.getListByUserID(mmBean.java:360)
at jsf.mmBean$Proxy$_$$_WeldClientProxy.getListByUserID(mmBean$Proxy$_$$_WeldClientProxy.java)
at jsf.mmBean.populateRandomElements(mmBean.java:277)
at jsf.mmBean.<init>(mmBean.java:74)
at jsf.mmBean$Proxy$_$$_WeldClientProxy.<init>(mmBean$Proxy$_$$_WeldClientProxy.java)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:344)
at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:173)
... 63 more