jfinal操作mysql数据库空指针异常,希望可以让高手可以帮忙看一下
建表语句
CREATE TABLE url_manager( id INT PRIMARY KEY AUTO_INCREMENT, redirectURL VARCHAR(255) NOT NULL );
pom.xml内容
<dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal-undertow</artifactId> <version>1.6</version> </dependency> <dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>4.0</version> </dependency> <!--桥接器:告诉slf4j使用Log4j2 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.9</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
undertow.txt内容
undertow.devMode=true undertow.port=80 undertow.host=0.0.0.0 # 绝大部分情况不建议配置 context path undertow.contextPath=/alit-url-manager undertow.resourcePath = classpath:alit-url-manager #jdbc-mysql jdbc.url=jdbc:mysql://localhost/alit_url_manager jdbc.user=root jdbc.pswd=root
启动类代码
package com.alit.url.manager; import com.alit.url.manager.controller.UrlManagerController; import com.alit.url.manager.model.UrlManager; import com.jfinal.config.Constants; import com.jfinal.config.Handlers; import com.jfinal.config.Interceptors; import com.jfinal.config.JFinalConfig; import com.jfinal.config.Plugins; import com.jfinal.config.Routes; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.druid.DruidPlugin; import com.jfinal.server.undertow.UndertowServer; import com.jfinal.template.Engine; import lombok.extern.slf4j.Slf4j; @Slf4j public class UrlManagerApplication extends JFinalConfig { public static void main(String[] args) { long start = System.currentTimeMillis(); UndertowServer.start(UrlManagerApplication.class, 80, true); long end = System.currentTimeMillis(); System.out.println("启动完成,共使用了" + (end - start) + "ms"); } public void configConstant(Constants me) { me.setDevMode(true); } public void configRoute(Routes me) { me.add("/", UrlManagerController.class); boolean mappingSuperClass = me.getMappingSuperClass(); log.info("mappingSuperClass:" + mappingSuperClass); } public void configEngine(Engine me) { } public void configPlugin(Plugins me) { PropKit.use("undertow.txt"); String jdbcUrl = PropKit.get("jdbc.url"); String jdbcUser = PropKit.get("jdbc.user"); String jdbcPswd = PropKit.get("jdbc.pswd"); DruidPlugin dp = new DruidPlugin(jdbcUrl, jdbcUser, jdbcPswd); dp.start(); me.add(dp); ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); me.add(arp); arp.start(); arp.addMapping("url_manager", UrlManager.class); } public void configInterceptor(Interceptors me) { } public void configHandler(Handlers me) { } }
model类代码
@SuppressWarnings("serial") public class UrlManager extends Model<UrlManager> { // 强烈建议通过 static 修饰过的 dao 对象都要调用一次 dao() 方法,以免新手误用造成线程安全问题 public static final UrlManager dao = new UrlManager().dao(); }
测试类代码
public class UrlManagerTest { private UrlManager urlManager; @Before public void setUp() throws Exception { String[] args = {}; UrlManagerApplication.main(args); urlManager = new UrlManager(); } @Test public void save() { boolean save = urlManager.set("redirectURL", "http://www.baidu.com").save(); System.out.println(save); } }
出现的异常
java.lang.NullPointerException at com.jfinal.plugin.activerecord.Model.save(Model.java:542) at com.alit.url.manager.model.UrlManagerTest.save(UrlManagerTest.java:29) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
出现异常的代码
public boolean save() { filter(FILTER_BY_SAVE); Config config = _getConfig(); Table table = _getTable(); StringBuilder sql = new StringBuilder(); List<Object> paras = new ArrayList<Object>(); config.dialect.forModelSave(table, attrs, sql, paras); //这里
笔者调试了很长时间,也不知道错误出现了哪里
gitlab https://gitee.com/litongjava_admin/alit-url-manager
项目:JFinal
arp.addMapping("url_manager", UrlManager.class);
映射这种事情交给生成器去做,不要手动,下载首页的 jfinal demo for maven ,里头有生成器使用的示例代码,直接用上即可